test_training.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from typing import Optional
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. from sklearn.datasets import load_digits
  6. from test_utils.run_server import background_server
  7. from hivemind import RemoteExpert
  8. def test_training(port: Optional[int] = None, max_steps: int = 100, threshold: float = 0.9):
  9. dataset = load_digits()
  10. X_train, y_train = torch.tensor(dataset['data'], dtype=torch.float), torch.tensor(dataset['target'])
  11. with background_server(num_experts=2, device='cpu', hidden_dim=64) as (server_endpoint, _):
  12. expert1 = RemoteExpert('expert.0', server_endpoint)
  13. expert2 = RemoteExpert('expert.1', server_endpoint)
  14. model = nn.Sequential(expert2, nn.Tanh(), expert1, nn.Linear(64, 10))
  15. opt = torch.optim.SGD(model.parameters(), lr=0.05)
  16. for step in range(max_steps):
  17. opt.zero_grad()
  18. outputs = model(X_train)
  19. loss = F.cross_entropy(outputs, y_train)
  20. loss.backward()
  21. opt.step()
  22. accuracy = (outputs.argmax(dim=1) == y_train).float().mean().item()
  23. if accuracy >= threshold:
  24. break
  25. assert accuracy >= threshold, f"too small accuracy: {accuracy}"