-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
326 lines (272 loc) · 8.65 KB
/
train.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# NOTE: this is an old file
import logging
logger = logging.getLogger("train")
logger.setLevel(logging.INFO)
import torch
from torch.optim import AdamW
# Local imports
import cpn_model
import stats
import stim_model
import utils
RECENT_EN = None
def unroll(cpn, mike, en, din, trial_end, observer, retain_stim_grads=False, cuda=None):
"""
This runs a trial on a batch.
Args:
cpn: a CPN network (a torch Module)
mike: a Michaels modular RNN (a torch Module)
en: an EN network (a torch Module)
din: batch of inputs to mike (i.e. VGGNet features)
tensor (batch, time, feat_dim)
trial_end: tensor with 1/0 indicating if the current time step
is beyond the trial end. This is how we handle trials
of varying lengths.
observer: and observer.Observer. Used to observe mike's activity.
retain_stim_grads: bool indicating we should keep stim vector gradients
cuda: something that can be passed to a tensor.cuda()
"""
batch_size = din.shape[0]
steps = din.shape[1]
stims = []
preds = torch.zeros(batch_size, steps - 1, en.out_dim)
actuals = torch.zeros(batch_size, steps - 1, en.out_dim)
if cuda is not None:
preds = preds.cuda(cuda)
actuals = actuals.cuda(cuda)
for tidx in range(steps - 1):
obs = mike.observe(observer)
new_obs_cpn = torch.cat(obs, axis=1).detach()
new_obs_en = obs[0].detach()
# cpn recieves (obs, trial_end)
cpn_in = torch.cat((new_obs_cpn, trial_end[:, tidx, :]), axis=1)
# output is (batch_size, num_stim_channels)
new_stim = cpn(cpn_in)
assert new_stim.shape == (batch_size, mike.stimulus.num_stim_channels)
if retain_stim_grads:
new_stim.retain_grad()
stims.append(new_stim)
# en receives (obs, stims, trial_end)
en_in = torch.cat((new_obs_en, new_stim, trial_end[:, tidx, :]), axis=1)
cur_pred = en(en_in)
preds[:, tidx, :] = cur_pred[:, :]
# new_stim will be cloned in here, to prevent accidentally backprop-ing
# through the "brain", aka mike.
mike.stimulate(new_stim)
# Note that 'preds' lags 'actual' by a time step, hence
# 'pred' is a prediction of the actual en activity
mike_out = mike(din[:, tidx + 1, :].T)
actuals[:, tidx, :] = mike_out[:, :]
actuals = utils.trunc_to_trial_end(actuals, trial_end[:, :-1, :])
preds = utils.trunc_to_trial_end(preds, trial_end[:, :-1, :])
return actuals, preds, stims
def train_loop(
cpn,
mike,
en,
din,
dout,
trial_end,
labels,
observer,
loss_history,
epoch_type,
is_val=False,
retain_stim_grads=False,
cuda=None,
):
cpn.reset()
mike.reset()
en.reset()
actuals, preds, stims = unroll(
cpn,
mike,
en,
din,
trial_end,
observer,
retain_stim_grads=retain_stim_grads,
cuda=cuda,
)
if is_val:
loss_history.report_val_last_result(actuals, preds, dout)
else:
loss_history.report_by_result(epoch_type, actuals, preds, dout, labels)
return actuals, preds, stims
def train_en(
mike,
observer,
cpn,
data_loader,
loss_history,
en=None,
opt_en=None,
en_num_neurons=None,
cuda=None,
):
"""
mike: a Michaels modular RNN (a torch Module)
observer: and observer.Observer. Used to observe mike's activity.
cpn: a CPN network (a torch Module)
data_loader: a DataLoader which contains the training data we are
using.
loss_history: a stats.LossHistory
cuda: something that can be passed to a tensor.cuda()
"""
# the last EN we were working on training, for debugging
global RECENT_EN
obs_dim = observer.out_dim * 1
# Stim: mike.stimulus.num_stim_channels
# +1 for trial_end
en_in_dim = obs_dim + mike.stimulus.num_stim_channels + 1
if en is None:
en = stim_model.StimModelLSTM(
en_in_dim,
mike.output_dim,
num_neurons=en_num_neurons or (en_in_dim + 50),
activation_func=torch.nn.Tanh,
cuda=cuda,
)
assert opt_en is None
opt_en = AdamW(en.parameters(), lr=9e-3, weight_decay=0.04)
RECENT_EN = en
vl = torch.tensor(1.0)
checkpoint_eidx = 0
eidx = -1
while True:
for batch in data_loader:
din, trial_end, _, dout, labels = batch
eidx += 1
batch_size = din.shape[0]
opt_en.zero_grad()
remaining_loss = loss_history.recent_train_loss
if remaining_loss is None:
# Just some high-ish number for the first time
# through this function.
remaining_loss = 0.05
else:
remaining_loss = remaining_loss.item()
# Silly lr schedule; basically works
for p in opt_en.param_groups:
if vl.item() < 0.0007:
p["lr"] = 1e-4
elif vl.item() < 0.005:
p["lr"] = 3e-3
else:
p["lr"] = 4e-3
cpn.reset()
cpn_noise = cpn_model.CPNNoiseyLSTMCollection(
cpn,
noise_var=2 * remaining_loss,
white_noise_pct=0.3,
white_noise_var=6,
cuda=cuda,
)
cpn_noise.setup(batch_size)
train_loop(
cpn_noise,
mike,
en,
din,
dout,
trial_end,
labels,
observer,
loss_history,
stats.LossRecType.EN,
retain_stim_grads=False,
cuda=cuda,
)
# Update en
rl = loss_history.recent_pred_loss
rl.backward()
opt_en.step()
# Verify against the actual CPN
if (eidx % 10) == 0:
train_loop(
cpn,
mike,
en,
din,
dout,
trial_end,
labels,
observer,
loss_history,
stats.LossRecType.EN,
retain_stim_grads=False,
is_val=True,
cuda=cuda,
)
vl = loss_history.recent_pred_val_loss
loss_history.log(logger, "training en:")
if (
torch.isnan(vl)
or torch.isinf(vl)
or vl.item() > 1.5
or (eidx - checkpoint_eidx) > 5000
):
en = stim_model.StimModelLSTM(
en.in_dim,
en.out_dim,
num_neurons=en.num_neurons,
activation_func=en.activation_func_t,
cuda=cuda,
)
RECENT_EN = en
opt_en = AdamW(en.parameters(), lr=1e-3, weight_decay=0.04)
checkpoint_eidx = eidx
if (vl.item() < max(0.02 * remaining_loss, 0.0003) and eidx > 200) or (
eidx - checkpoint_eidx
) == 2000:
done = True
break
else:
done = False
if done:
break
opt_en.zero_grad()
return en, opt_en
def refine_en(
cpn,
mike,
en,
opt_en,
data_loader,
observer,
loss_history,
cuda=None,
):
for p in opt_en.param_groups:
p["lr"] = 1e-4
batch_size = din.shape[0]
while loss_history.recent_pred_loss > max(loss_history.recent_task_loss / 10, 6e-4):
for batch in data_loader:
din, trial_end, _, dout, labels = batch
cpn.reset()
cpn_noise = cpn_model.CPNNoiseyLSTMCollection(
cpn,
noise_var=0.002,
white_noise_pct=0.3,
white_noise_var=6,
cuda=cuda,
)
cpn_noise.setup(batch_size)
opt_en.zero_grad()
_ = train_loop(
cpn,
mike,
en,
din,
dout,
trial_end,
labels,
observer,
loss_history,
stats.LossRecType.EN,
cuda=cuda,
)
pred_loss = loss_history.recent_pred_loss
pred_loss.backward(inputs=list(en.parameters()))
loss_history.log(logger, "adjusting en:")
opt_en.step()