-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_symmetry.py
155 lines (117 loc) · 5.02 KB
/
local_symmetry.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import torch
import tqdm
from abc import ABC, abstractmethod
from config import Config
from utils import rmse, in_lie_algebra
class Predictor(ABC):
optimizer = None
# alias for run
def __call__(self, x):
return self.run(x)
@abstractmethod
def run(self, x):
pass
@abstractmethod
def name(self):
pass
def loss(self, y_pred, y_true):
return rmse(y_pred, y_true)
# implement if coset discovery is needed
# same as `loss`, but does not collapse on first dimension
def batched_loss(self, y_pred, y_true):
raise NotImplemented()
def returns_logits(self):
return False
# some predictors can be given as fixed functions
def needs_training(self):
return True
class LocalTrainer:
def __init__(self, ff, predictor, basis, dataset, config: Config):
self.ff = ff
self.predictor = predictor
self.basis = basis
self.dataset = dataset
self.config = config
if config.debug:
torch.autograd.set_detect_anomaly(True)
torch.set_printoptions(precision=9, sci_mode=False)
def loader(self):
collate_fn = self.dataset.collate if hasattr(self.dataset, 'collate') else None
loader = torch.utils.data.DataLoader(self.dataset, batch_size=self.config.batch_size, collate_fn=collate_fn, shuffle=True)
return loader
def train_predictor(self, loader):
p_losses = []
if self.predictor.needs_training() and not self.config.reuse_predictor:
for xx, yy in tqdm.tqdm(loader):
xff = self.ff(xx)
yff = self.ff(yy)
y_pred = self.predictor.run(xff.regions(self.basis.in_rad))
# relying on basis for radius is ugly ...
# in rad since clipping is only needed for group basis training
y_true = yff.regions(self.basis.in_rad)
p_loss = self.predictor.loss(y_pred, y_true)
p_losses.append(float(p_loss.detach().cpu()))
self.predictor.optimizer.zero_grad()
p_loss.backward()
self.predictor.optimizer.step()
p_losses = np.mean(p_losses) if len(p_losses) else 0
if self.predictor.needs_training() and not self.config.reuse_predictor:
torch.save(self.predictor, "predictors/" + self.predictor.name() + '.pt')
return p_losses
def train(self):
loader = self.loader()
for e in range(self.config.epochs):
# train predictor
p_losses = self.train_predictor(loader)
# train basis
b_losses = []
b_reg = []
for xx, yy in tqdm.tqdm(loader):
xff = self.ff(xx)
yff = self.ff(yy)
b_loss = self.basis.step(xff, self.predictor, yff)
b_losses.append(float(b_loss))
reg = self.basis.regularization(e)
b_loss += reg
b_reg.append(float(reg))
self.basis.optimizer.zero_grad()
b_loss.backward()
self.basis.optimizer.step()
b_losses = np.mean(b_losses) if len(b_losses) else 0
b_reg = np.mean(b_reg) if len(b_reg) else 0
print("Discovered Basis \n", self.basis.summary())
print("Epoch", e, "Predictor loss", p_losses, "Basis loss", b_losses, "Basis reg", b_reg)
def discover_cosets(self, lie_algebra, q):
loader = self.loader()
for e in range(self.config.epochs):
# train predictor
p_losses = self.train_predictor(loader)
# train cosets
full_losses = []
b_losses = []
for xx, yy in tqdm.tqdm(loader):
xff = self.ff(xx)
yff = self.ff(yy)
b_loss_full = self.predictor.batched_loss(*self.basis.coset_step(xff, self.predictor))
full_losses.append(b_loss_full.cpu().detach().numpy())
b_loss = b_loss_full.mean()
b_losses.append(float(b_loss))
self.basis.optimizer.zero_grad()
b_loss.backward()
self.basis.optimizer.step()
b_losses = np.mean(b_losses) if len(b_losses) else 0
full_losses_avg = np.mean(full_losses, axis=0)
best = np.argmin(full_losses_avg)
print("Epoch", e, "Predictor loss", p_losses, "Best loss", full_losses_avg[best], "Best", self.basis.norm_cosets()[best].cpu().detach())
if e == self.config.epochs - 1:
print("Filtering duplicate cosets...")
inds = np.argsort(full_losses_avg)
final = []
for coset in self.basis.norm_cosets()[inds][:q]:
for curr in final:
if in_lie_algebra(curr @ torch.inverse(coset), lie_algebra):
break
else:
final.append(coset)
print("Final coset representatives", final)