-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_labels_torch.py
74 lines (51 loc) · 1.43 KB
/
train_labels_torch.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import torch
from utils import *
from models_torch import *
from datasets import *
from groups import *
# # torch.manual_seed(42)
torch.cuda.empty_cache()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('Using device: ', device)
# device = 'cpu'
"""
Parameters of te model
"""
num_ep = 50 #number of epochs
batch_size = 4
weight = 10. #coeffcient of regularization
loginterval = 1
noise = 0.
path = './test/'
"""
Initialize dataset
"""
dset = label_dset(path, noise)
train_loader = torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=True)
dim = dset.__getitem__(0)[0].shape[-1]
irrep_dims = [1] * dim
"""
Initialize model and optimizer
"""
model = spectral_net(dim, irrep_dims).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
"""
Training loop
"""
def train(epoch, data_loader):
for batch_idx, (x, y) in enumerate(data_loader):
model.train()
optimizer.zero_grad()
x = x.to(device)
y = y.to(device)
loss = model.loss(x, y).mean()
reg = model.reg()
tot_loss = weight * reg + loss
tot_loss.backward()
optimizer.step()
if batch_idx % 10 == 0:
print(f"Epoch: {epoch}, Batch: {batch_idx} of {len(data_loader)} Loss: {loss:.3} Reg: {reg:.3}")
if __name__ == "__main__":
for i in range(1, num_ep + 1):
print(f'Epoch {i}')
train(i, train_loader)