-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpn_epoch_cpn.py
276 lines (232 loc) · 8.71 KB
/
cpn_epoch_cpn.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import torch
from cpn_utils import CPNENStats, calc_pred_loss, calc_train_loss, EpochType
from experiment import lesion, utils
class CPNEpochCPN:
def __init__(self, en, opt_en, cpn, opt_cpn, cfg, model_path=None):
self.en = en
self.opt_en = opt_en
self.cpn = cpn
self.opt_cpn = opt_cpn
for p in opt_cpn.param_groups:
p["lr"] = 1e-3
if model_path is not None:
cpn.load_state_dict(torch.load(model_path))
self.cfg = cfg
self.preds = None
self.tidx = 0
# This tracks how many epochs we've been training the CPN
self.checkpoint_eidx = 0
self.recent_task_losses = []
self.recent_train_loss = 0.05
self.recent_pred_loss = 0.05
self.recent_train_val_loss = 0.05
# Weight for stim regularizer
# self.reg_stim_weight = 2e-8
self.reg_stim_weight = None
self.stims = []
if cfg.recover_after_lesion:
self.set_opt_lr = self.lr_sched_aggressive_refine3
self.cpn_epoch_max_len = 200
elif (
type(cfg.lesion_instance) is lesion.LesionType.outputs.value
and cfg.lesion_instance.start_idx == 0
and cfg.lesion_instance.end_idx == 50
):
# M1 is an odd beast...
self.set_opt_lr = self.lr_sched_aggressive_refine3
self.cpn_epoch_max_len = 100
else:
self.set_opt_lr = self.lr_sched_standard
self.cpn_epoch_max_len = 200
self.reset()
def reset(self):
self.preds = None
self.tidx = 0
self.stims = []
self.reset_models()
def reset_models(self):
self.en.reset()
self.cpn.reset()
self.opt_en.zero_grad()
self.opt_cpn.zero_grad()
def reset_period(self):
self.checkpoint_eidx = 0
self.recent_task_losses = []
def set_en(self, en, opt_en):
self.en = en
self.opt_en = opt_en
self.en.reset()
self.opt_en.zero_grad()
def ensure_preds(self, batch_size):
if self.preds is None:
self.preds = []
def forward(self, brain_data, loss_history, is_validation):
batch_size = brain_data[0].shape[0]
self.ensure_preds(batch_size)
cpn_in = torch.cat(brain_data, axis=1)
new_stim = self.cpn(cpn_in)
# en receives (obs, stims, trial_end)
new_obs_en = brain_data[0]
en_in = torch.cat((new_obs_en, new_stim, brain_data[-1]), axis=1)
cur_pred = self.en(en_in)
self.preds.append(cur_pred.unsqueeze(dim=1))
self.tidx += 1
self.stims.append(new_stim)
return new_stim
def feedback(self, actuals, targets, trial_end, loss_history, is_validation):
preds = torch.cat(self.preds, axis=1)
preds = utils.trunc_to_trial_end(preds, trial_end[:, :-1, :])
pred_loss = calc_pred_loss(preds, actuals)
train_loss = calc_train_loss(preds, targets)
if self.reg_stim_weight is not None:
# Regularization for stimulation applied
train_loss += self.reg_stim_weight * sum(
[torch.linalg.norm(s) for s in self.stims]
)
if is_validation:
self.recent_train_val_loss = train_loss.item()
else:
self.recent_train_loss = train_loss.item()
if not self.cfg.dont_train:
train_loss.backward(inputs=list(self.cpn.parameters()))
self.recent_pred_loss = pred_loss.item()
def lr_sched_aggressive_refine(self, rtl, eidx):
"""
Args:
rtl - recent training loss, which we use to determine the learning rate
"""
if rtl is None or rtl >= 0.005:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-3
elif rtl >= 0.004:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-4
elif rtl >= 0.002:
for p in self.opt_cpn.param_groups:
p["lr"] = 2e-6
else:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-6
def lr_sched_aggressive_refine2(self, rtl, eidx):
"""
Args:
rtl - recent training loss, which we use to determine the learning rate
"""
if rtl is None or rtl >= 0.004:
for p in self.opt_cpn.param_groups:
p["lr"] = 5e-4
elif rtl >= 0.003:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-5
elif rtl >= 0.002:
for p in self.opt_cpn.param_groups:
p["lr"] = 2e-6
else:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-6
def lr_sched_aggressive_refine3(self, rtl, eidx):
"""
Seems to work for M1 co-adapt.
Args:
rtl - recent training loss, which we use to determine the learning rate
"""
if rtl is None or eidx < 4000:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-3
elif rtl >= 0.008:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-3
elif rtl >= 0.006:
for p in self.opt_cpn.param_groups:
p["lr"] = 5e-4
elif rtl >= 0.005:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-5
elif rtl >= 0.004:
for p in self.opt_cpn.param_groups:
p["lr"] = 2e-6
else:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-6
def lr_sched_standard(self, rtl, eidx):
"""
Args:
rtl - recent training loss, which we use to determine the learning rate
"""
if rtl is None or rtl >= 0.008:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-3
elif rtl >= 0.006:
for p in self.opt_cpn.param_groups:
p["lr"] = 5e-5
elif rtl >= 0.002:
for p in self.opt_cpn.param_groups:
p["lr"] = 2e-6
else:
for p in self.opt_cpn.param_groups:
p["lr"] = 1e-6
def finish(self, loss_history, is_validation):
rtl = self.recent_train_loss
self.set_opt_lr(rtl, loss_history.eidx)
# Every 10 epochs let's validate/test
next_is_validation = not is_validation and (loss_history.eidx % 10) == 0
last_rec = loss_history.get_recent_record(-2)
pred_val_loss_out = float("nan")
if last_rec is not None:
last_user_data = last_rec.user_data
if last_user_data is not None:
pred_val_loss_out = last_user_data.pred_val_loss
rtl = loss_history.recent_task_loss
self.recent_task_losses.append(rtl)
# if (loss_history.max_pct_recov > 0.99 and not self.cfg.dont_train and
if (
loss_history.max_pct_recov > 0.99
and not self.cfg.dont_train
and loss_history.lesioned_loss > loss_history.healthy_loss
):
# The pct recov makes sense only in the typical case that lesions cause
# worse performance, due to the way it's calculated. This is always the case
# unless we are using a pre-recovered model.
we_are_done = True
en_is_ready = False
# For now: just run for awhile
elif self.cfg.dont_train and loss_history.eidx == 250000:
we_are_done = True
en_is_ready = False
else:
we_are_done = False
if self.cfg.dont_train:
en_is_ready = True
self.reset_period()
elif self.recent_pred_loss > max(rtl / 10, 6e-4):
en_is_ready = False
self.reset_period()
elif self.checkpoint_eidx >= self.cpn_epoch_max_len:
en_is_ready = False
self.reset_period()
elif self.checkpoint_eidx > 30:
num_reg = 0
for l in self.recent_task_losses[-30:]:
if l < rtl:
num_reg += 1
if num_reg > 15:
en_is_ready = False
self.reset_period()
else:
en_is_ready = True
self.opt_cpn.step()
self.checkpoint_eidx += 1
else:
en_is_ready = True
self.opt_cpn.step()
self.checkpoint_eidx += 1
user_data = CPNENStats(
"cpn",
EpochType.CPN,
self.recent_train_loss,
self.recent_train_val_loss,
self.recent_pred_loss,
pred_val_loss_out,
)
self.reset()
return we_are_done, next_is_validation, en_is_ready, user_data