-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
44 lines (33 loc) · 840 Bytes
/
basic_usage.py
File metadata and controls
44 lines (33 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Minimal example demonstrating hyrax usage.
"""
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset
from hyrax import DistributedTrainer
class TinyModel(nn.Module):
def __init__(self):
super().__init__()
self.layer = nn.Linear(10, 1)
def forward(self, x):
return self.layer(x)
# create dummy datasets
def create_dummy_dataset(seed):
torch.manual_seed(seed)
x = torch.randn(1000, 10)
y = torch.randn(1000, 1)
return TensorDataset(x, y)
# create three datasets
datasets = [
create_dummy_dataset(i)
for i in range(3)
]
# train
trainer = DistributedTrainer(
model=TinyModel,
datasets=datasets
)
results = trainer.train(epochs=20)
print("\nresults:")
for r in results:
print(f"worker {r['worker_id']}: loss = {r['final_loss']:.4f}")