-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_natural_mov.py
425 lines (349 loc) · 13.8 KB
/
main_natural_mov.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
'''
Author: GhMa
Date: 2023-03-10 15:30:43
LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
LastEditTime: 2023-04-27 22:16:40
'''
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.utils.tensorboard import SummaryWriter
import os
import uuid
import argparse
import glob
import shutil
import importlib
from models import *
from crits import *
from utils import progress_bar, visualize_grad_norms, visualize_weight_norms
from utils import Monitor, visualize_spiking_rates
from dataset_utils import prepare_natural_movie
import torchvision
import torchvision.transforms as transforms
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
COLOR_PURPLE = "\033[1;35m"
COLOR_CYAN = "\033[1;36m"
COLOR_END = '\033[0m'
parser = argparse.ArgumentParser(description='SNN Exps.')
parser.add_argument('--lr', default=0.0005, type=float, help='learning rate')
parser.add_argument('--epochs', default=64, type=int, help='#epoch')
parser.add_argument('--minibatch', default=64, type=int,
help='mini-batch size')
# FOR NEURON
parser.add_argument('--neuron', default='LIF', type=str, help='neuron type')
parser.add_argument('--threshold', default=1.0, type=float,
help='spiking thresh')
parser.add_argument(
'--tau', default=2.0, type=float,
help='initial tau, membrane potential decay constant'
)
parser.add_argument(
'--sigma', default=0.2, type=float,
help='std of p_epsilon, specifically for Noisy LIF'
)
# FOR NETWORK ARCH.
parser.add_argument(
'--x_dim', default=90, type=int,
help='input width. The width-height ratio is assumed to be 1. '
)
parser.add_argument(
'--h_dim', default=64, type=int, help='recurrence hidden state dim.'
)
parser.add_argument(
'--z_dim', default=64, type=int, help='vae latent var. dim.')
parser.add_argument(
'--neuron_dim', default=1, type=int, help='readout neuron dim')
parser.add_argument('--fullsnn', default=1, type=int, help='model type')
parser.add_argument('--model', default='tecos', type=str, help='model type')
# FOR SURROGATE GRADIENT FUNC
parser.add_argument('--alpha', default=1.0, type=float,
help='surrogate grad func scale hyperparameter')
# Beta-VAE, the lagrangian multiplier
parser.add_argument('--beta', default=1.0, type=float,
help='beta for beta-vae, 1 for vanilla vae')
# Vanilla regularizers
parser.add_argument('--weight_decay', default=0.0, type=float,
help='lagrangian factor of L2 norm term')
# Other opt. settings
parser.add_argument(
'--grad_clip_thresh', default=20, type=float,
help='gradient clipping threshold for stable training.'
)
parser.add_argument('--temperature', default=1.0, type=float, help='temperature')
parser.add_argument('--seed', default=1000, type=int, help='random seed')
parser.add_argument('--workers', default=8, type=int, help='#threads')
parser.add_argument('--optim', default='adam', type=str, help='optimizer')
parser.add_argument('--scheduler', default='cos', type=str, help='lr scheduler')
# Experimental functions.
parser.add_argument('--init', default='uniform', type=str,
help="specified weight initialization method. ")
parser.add_argument('--plot_spike_rate', default=False, type=bool,
help="plot spike rate or not. ")
#
parser.add_argument('--resume', '-r', action='store_true',
help='resume from checkpoint')
parser.add_argument(
'--debug', action='store_true',
help='if debug is true, formal log files will not be created'
)
# dataset specification
parser.add_argument(
'--exp', default=1, type=int,
help='which exp. They use two different retinas in exp 1 and exp 2.'
)
parser.add_argument('--mov', default=1, type=int, help='which movie.')
parser.add_argument('--stim_hist', default=0, type=int, help='stimuli history len')
args = parser.parse_args()
print(COLOR_PURPLE+'==> Natural Movie dataset: Exp {}, Movie {}'.format(args.exp, args.mov))
# Set readout neuron dim
if args.exp == 1:
args.neuron_dim = 38
elif args.exp == 2:
args.neuron_dim = 49
##############################################################
basic_neuron = importlib.import_module('models.' + args.neuron).Neuron
os.environ['PYTHONHASHSEED'] = str(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_loss = 9999
start_epoch = 0
trainloader, testloader, stim_his, vis_trainloader, vis_testloader = prepare_natural_movie(
args,
stim_hist=args.stim_hist
)
print(COLOR_CYAN+'==> Building model..')
if args.model == 'tecos':
print('==> TeCoS-LVM {} full'.format(args.neuron)+COLOR_END)
net = TemporalConditioningSpikingLVM(
x_dim=[args.x_dim, args.x_dim],
h_dim=args.h_dim,
z_dim=args.z_dim,
neuron_dim=args.neuron_dim,
n_layers=1,
device=device,
spiking_neuron=basic_neuron,
threshold=args.threshold, # for neuron
tau=args.tau, # for neuron
alpha=args.alpha, # for neuron
sigma=args.sigma, # for Noisy neuron
)
net = net.to(device)
if device == 'cuda':
cudnn.benchmark = True
total_params = sum(param.numel() for param in net.parameters() if param.requires_grad)
print('==> parameter count: {}'.format(total_params))
optimizer = optim.Adam(
net.parameters(),
lr=args.lr,
weight_decay=args.weight_decay
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=args.epochs
)
def train(epoch, writer, scheduler, args):
print('\nEpoch: %d' % epoch)
net.train()
train_loss_total_all = 0
train_loss_kld_all = 0
train_loss_recon_all = 0
for batch_idx, (stimuli, response, fr) in enumerate(trainloader):
stimuli, response = stimuli.to(device), response.to(device)
stimuli = stimuli.transpose(0, 1)
response = response.transpose(0, 1)
fr = fr.transpose(0, 1).to(device)
stimuli = stimuli.unsqueeze(2)
timesteps = stimuli.size(0)
batch_size = stimuli.size(1)
optimizer.zero_grad()
net.reset()
kld_loss, recon_loss, _, [pred, _] = net(stimuli, response, fr)
kld_loss /= batch_size
recon_loss /= batch_size
loss_total = recon_loss + args.beta * kld_loss
loss_total.backward()
nn.utils.clip_grad_norm_(net.parameters(), args.grad_clip_thresh)
optimizer.step()
train_loss_total_all += loss_total.item()
train_loss_kld_all += kld_loss.item()
train_loss_recon_all += recon_loss.item()
progress_bar(
batch_idx,
len(trainloader),
'Loss_total: %.5f | Loss_kld: %.5f | Loss_recon: %.5f' % (
loss_total.item(), kld_loss.item(), recon_loss.item()
)
)
writer.add_scalar(
'loss_total / train', train_loss_total_all / (batch_idx+1), epoch
)
writer.add_scalar(
'loss_kld / train', train_loss_kld_all / (batch_idx+1), epoch
)
writer.add_scalar(
'loss_recon / train', train_loss_recon_all / (batch_idx+1), epoch
)
net.eval()
with torch.no_grad():
t_counter = 0
t_counter_max = 450 # 15 s
pred_len = 15 # 1
if args.model == 'swspikinglvm':
pred_len = 1
prediction = torch.zeros(t_counter_max, response.size(-1))
true_response = torch.zeros(t_counter_max, response.size(-1))
for batch_idx, (stimuli, response, fr) in enumerate(vis_trainloader):
if t_counter >= t_counter_max:
break
if not t_counter % pred_len:
stimuli, response = stimuli.to(device), response.to(device)
stimuli = stimuli.transpose(0, 1)
response = response.transpose(0, 1)
fr = fr.transpose(0, 1).to(device)
stimuli = stimuli.unsqueeze(2)
batch_size = stimuli.size(1)
true_response[
t_counter: t_counter+pred_len
] = response.squeeze()[-pred_len:]
net.reset()
kld_loss, recon_loss, _, [pred, _] = net(stimuli, response, fr)
prediction[
t_counter: t_counter+pred_len
] = pred.squeeze()[-pred_len:]
t_counter += 1
writer.add_image('response train / true', true_response.T.unsqueeze(0), epoch)
writer.add_image('response train / prediction', prediction.T.unsqueeze(0), epoch)
def test(epoch, writer, path):
global best_loss
net.eval()
test_loss_total_all = 0
test_loss_kld_all = 0
test_loss_recon_all = 0
test_cc_all = 0
with torch.no_grad():
for batch_idx, (stimuli, response, fr) in enumerate(testloader):
stimuli, response = stimuli.to(device), response.to(device)
stimuli = stimuli.transpose(0, 1)
response = response.transpose(0, 1)
fr = fr.transpose(0, 1).to(device)
stimuli = stimuli.unsqueeze(2)
net.reset()
timesteps = stimuli.size(0)
batch_size = stimuli.size(1)
kld_loss, recon_loss, _, [pred, _] = net(stimuli, response, fr)
kld_loss /= batch_size
recon_loss /= batch_size
loss_total = recon_loss + args.beta * kld_loss
test_loss_total_all += loss_total.item()
test_loss_kld_all += kld_loss.item()
test_loss_recon_all += recon_loss.item()
progress_bar(
batch_idx, len(testloader),
'Loss_total: %.5f | Loss_kld: %.5f | Loss_recon: %.5f' % (
loss_total.item(), kld_loss.item(), recon_loss.item()
)
)
test_loss_total = test_loss_total_all / (batch_idx+1)
test_loss_recon_ = test_loss_recon_all / (batch_idx+1)
writer.add_scalar('loss_total / test', test_loss_total, epoch)
writer.add_scalar('loss_kld / test', test_loss_kld_all / (batch_idx+1), epoch)
writer.add_scalar(
'loss_recon / test', test_loss_recon_all / (batch_idx+1),
epoch
)
with torch.no_grad():
t_counter = 0
t_counter_max = 450 # 15 s
pred_len = 15 # 1
if args.model == 'swspikinglvm':
pred_len = 1
prediction = torch.zeros(t_counter_max, response.size(-1))
true_response = torch.zeros(t_counter_max, response.size(-1))
for batch_idx, (stimuli, response, fr) in enumerate(vis_trainloader):
if t_counter >= t_counter_max:
break
if not t_counter % pred_len:
stimuli, response = stimuli.to(device), response.to(device)
stimuli = stimuli.transpose(0, 1)
response = response.transpose(0, 1)
fr = fr.transpose(0, 1).to(device)
stimuli = stimuli.unsqueeze(2)
true_response[
t_counter: t_counter+pred_len
] = response.squeeze()[-pred_len:]
net.reset()
kld_loss, recon_loss, _, [pred, _] = net(stimuli, response, fr)
prediction[
t_counter: t_counter+pred_len
] = pred.squeeze()[-pred_len:]
t_counter += 1
writer.add_image('response test / true', true_response.T.unsqueeze(0), epoch)
writer.add_image('response test / prediction', prediction.T.unsqueeze(0), epoch)
if test_loss_recon_ < best_loss:
print('Saving..')
state = {
'net': net.state_dict(),
'loss': test_loss_recon_,
'epoch': epoch,
}
torch.save(
state,
os.path.join(path, 'best.pth')
)
best_loss = test_loss_recon_
if __name__ == '__main__':
uid = uuid.uuid4().hex
path = os.path.join(
'./results/logs/NatMov-exp_{}-mov_{}-type_{}-neuron_{}-optim_{}-sch_{}-lr_{}-bs_{}-vth_{}-tau_{}-alpha{}-sigma_{}-xdim_{}-hdim_{}-zdim_{}-beta_{}-wd_{}'.format(
int(args.exp),
int(args.mov),
str(args.model),
str(args.neuron),
str(args.optim),
str(args.scheduler),
float(args.lr),
int(args.minibatch),
float(args.threshold),
float(args.tau),
float(args.alpha),
float(args.sigma),
int(args.x_dim),
int(args.h_dim),
int(args.z_dim),
float(args.beta),
float(args.weight_decay)
),
str(uid)
)
if args.debug:
writer = SummaryWriter('./')
else:
if not os.path.isdir(path):
os.makedirs(path)
script_path = os.path.join(path, 'scripts')
if not os.path.isdir(script_path):
os.makedirs(script_path)
writer = SummaryWriter(path)
files = list(glob.iglob(os.path.join('./', '*.sh'))) + \
list(glob.iglob(os.path.join('./', '*.py'))) + \
list(glob.iglob(os.path.join('./models', '*.py')))
for file in files:
if not os.path.isfile(file):
continue
shutil.copy2(
file, os.path.join(
script_path, file.replace('models/', 'models_')
)
)
for epoch in range(start_epoch, start_epoch+args.epochs):
train(epoch, writer, scheduler, args)
test(epoch, writer, path)
scheduler.step()
print('done')