-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_torch.py
98 lines (69 loc) · 2 KB
/
train_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
std = 1.
loginterval = 1
noise = 0.
"""
Initialize group
"""
group = dihedral(3)
group.check_dims()
"""
Initialize dataset
"""
dset = group_dset(group, std, noise)
train_loader = torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=True)
"""
Initialize model and optimizer
"""
model = spectral_net(group.order, group.irrep_dims).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
"""
Initialize Cayley table
"""
perms = perm_matrices(group.order)
cayley_true = group.cayley_table
print(cayley_true)
"""
Training loop
"""
def train(epoch, data_loader):
cayley = model.get_table().cpu().numpy()
cayley_score = perm_frobenius(cayley_true, cayley, perms, group.order)
print(f"Epoch: {epoch}, Cayley score: {cayley_score:.3}")
print(cayley)
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)
# cayley = model.get_table()
# cayley_score = perm_frobenius(cayley_true, cayley, perms)
# outfile = open(f'./accuracy_results_{noise}.txt', 'a+')
# outfile.write(str(cayley_score.item())[:4] + '\n')
# outfile.close()