-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
416 lines (327 loc) · 17.4 KB
/
model.py
File metadata and controls
416 lines (327 loc) · 17.4 KB
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
"""
Nicolas Masse 2017
Contributions from Gregory Grant, Catherine Lee
"""
import tensorflow as tf
import numpy as np
import stimulus
import analysis
from parameters import *
import os, sys
import matplotlib.pyplot as plt
import matplotlib as mpl
import pickle
# Ignore "use compiled version of TensorFlow" errors
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
print('Using EI Network:\t', par['EI'])
print('Synaptic configuration:\t', par['synapse_config'], "\n")
"""
Model setup and execution
"""
class Model:
def __init__(self, input_data, target_data, mask):
# Load the input activity, the target data, and the training mask for this batch of trials
self.input_data = tf.unstack(input_data, axis=1)
self.target_data = tf.unstack(target_data, axis=1)
self.mask = tf.unstack(mask, axis=0)
# Load the initial hidden state activity to be used at the start of each trial
self.hidden_init = tf.constant(par['h_init'])
# Load the initial synaptic depression and facilitation to be used at the start of each trial
self.synapse_x_init = tf.constant(par['syn_x_init'])
self.synapse_u_init = tf.constant(par['syn_u_init'])
# Build the TensorFlow graph
self.run_model()
# Train the model
self.optimize()
def run_model(self):
"""
Run the reccurent network
History of hidden state activity stored in self.hidden_state_hist
"""
self.rnn_cell_loop(self.input_data, self.hidden_init, self.synapse_x_init, self.synapse_u_init)
with tf.variable_scope('output'):
# W_out = tf.get_variable('W_out', initializer = par['w_out0'], trainable=True)
# b_out = tf.get_variable('b_out', initializer = par['b_out0'], trainable=True)
W_out = tf.get_variable('W_out', initializer = par['weights_trained']['w_out'], trainable=True)
b_out = tf.get_variable('b_out', initializer = par['weights_trained']['b_out'], trainable=True)
"""
Network output
Only use excitatory projections from the RNN to the output layer
"""
self.y_hat = [tf.matmul(tf.nn.relu(W_out),h)+b_out for h in self.hidden_state_hist]
def rnn_cell_loop(self, x_unstacked, h, syn_x, syn_u):
"""
Initialize weights and biases
"""
with tf.variable_scope('rnn_cell'):
# W_in = tf.get_variable('W_in', initializer = par['w_in0'], trainable=True)
# W_rnn = tf.get_variable('W_rnn', initializer = par['w_rnn0'], trainable=True)
# b_rnn = tf.get_variable('b_rnn', initializer = par['b_rnn0'], trainable=True)
W_in = tf.get_variable('W_in', initializer = par['weights_trained']['w_in'], trainable=True)
W_rnn = tf.get_variable('W_rnn', initializer = par['weights_trained']['w_rnn'], trainable=True)
b_rnn = tf.get_variable('b_rnn', initializer = par['weights_trained']['b_rnn'], trainable=True)
self.W_ei = tf.constant(par['EI_matrix'])
self.hidden_state_hist = []
self.syn_x_hist = []
self.syn_u_hist = []
"""
Loop through the neural inputs to the RNN, indexed in time
"""
for rnn_input in x_unstacked:
h, syn_x, syn_u = self.rnn_cell(rnn_input, h, syn_x, syn_u)
self.hidden_state_hist.append(h)
self.syn_x_hist.append(syn_x)
self.syn_u_hist.append(syn_u)
def rnn_cell(self, rnn_input, h, syn_x, syn_u):
"""
Main computation of the recurrent network
"""
with tf.variable_scope('rnn_cell', reuse=True):
W_in = tf.get_variable('W_in')
W_rnn = tf.get_variable('W_rnn')
b_rnn = tf.get_variable('b_rnn')
if par['EI']:
# ensure excitatory neurons only have postive outgoing weights,
# and inhibitory neurons have negative outgoing weights
W_rnn_effective = tf.matmul(tf.nn.relu(W_rnn), self.W_ei)
else:
W_rnn_effective = W_rnn_drop
"""
Update the synaptic plasticity paramaters
"""
if par['synapse_config'] == 'std_stf':
# implement both synaptic short term facilitation and depression
syn_x += par['alpha_std']*(1-syn_x) - par['dt_sec']*syn_u*syn_x*h
syn_u += par['alpha_stf']*(par['U']-syn_u) + par['dt_sec']*par['U']*(1-syn_u)*h
syn_x = tf.minimum(np.float32(1), tf.nn.relu(syn_x))
syn_u = tf.minimum(np.float32(1), tf.nn.relu(syn_u))
h_post = syn_u*syn_x*h
elif par['synapse_config'] == 'std':
# implement synaptic short term derpression, but no facilitation
# we assume that syn_u remains constant at 1
syn_x += par['alpha_std']*(1-syn_x) - par['dt_sec']*syn_x*h
syn_x = tf.minimum(np.float32(1), tf.nn.relu(syn_x))
syn_u = tf.minimum(np.float32(1), tf.nn.relu(syn_u))
h_post = syn_x*h
elif par['synapse_config'] == 'stf':
# implement synaptic short term facilitation, but no depression
# we assume that syn_x remains constant at 1
syn_u += par['alpha_stf']*(par['U']-syn_u) + par['dt_sec']*par['U']*(1-syn_u)*h
syn_u = tf.minimum(np.float32(1), tf.nn.relu(syn_u))
h_post = syn_u*h
else:
# no synaptic plasticity
h_post = h
"""
Update the hidden state
Only use excitatory projections from input layer to RNN
All input and RNN activity will be non-negative
"""
h = tf.nn.relu(h*(1-par['alpha_neuron'])
+ par['alpha_neuron']*(tf.matmul(tf.nn.relu(W_in), tf.nn.relu(rnn_input))
+ tf.matmul(W_rnn_effective, h_post) + b_rnn)
+ tf.random_normal([par['n_hidden'], par['batch_train_size']], 0, par['noise_rnn'], dtype=tf.float32))
return h, syn_x, syn_u
def optimize(self):
"""
Calculate the loss functions and optimize the weights
perf_loss = [mask*tf.reduce_mean(tf.square(y_hat-desired_output),axis=0)
for (y_hat, desired_output, mask) in zip(self.y_hat, self.target_data, self.mask)]
"""
"""
cross_entropy
"""
perf_loss = [mask*tf.nn.softmax_cross_entropy_with_logits_v2(logits = y_hat, labels = desired_output, dim=0) \
for (y_hat, desired_output, mask) in zip(self.y_hat, self.target_data, self.mask)]
# L2 penalty term on hidden state activity to encourage low spike rate solutions
spike_loss = [par['spike_cost']*tf.reduce_mean(tf.square(h), axis=0) for h in self.hidden_state_hist]
with tf.variable_scope('rnn_cell', reuse = True):
W_in = tf.get_variable('W_in')
W_rnn = tf.get_variable('W_rnn')
with tf.variable_scope('output', reuse = True):
W_out = tf.get_variable('W_out')
self.wiring_loss = tf.reduce_sum(tf.nn.relu(W_in)) + tf.reduce_sum(tf.nn.relu(W_rnn)) + tf.reduce_sum(tf.nn.relu(W_out))
self.wiring_loss *= par['wiring_cost']
self.perf_loss = tf.reduce_mean(tf.stack(perf_loss, axis=0))
self.spike_loss = tf.reduce_mean(tf.stack(spike_loss, axis=0))
self.loss = self.perf_loss + self.spike_loss + self.wiring_loss
opt = tf.train.AdamOptimizer(learning_rate = par['learning_rate'])
grads_and_vars = opt.compute_gradients(self.loss)
"""
Apply any applicable weights masks to the gradient and clip
"""
capped_gvs = []
for grad, var in grads_and_vars:
if var.name == "rnn_cell/W_rnn:0":
grad *= par['w_rnn_mask']
print('Applied weight mask to w_rnn.')
elif var.name == "output/W_out:0":
grad *= par['w_out_mask']
print('Applied weight mask to w_out.')
if not str(type(grad)) == "<class 'NoneType'>":
capped_gvs.append((tf.clip_by_norm(grad, par['clip_max_grad_val']), var))
self.train_op = opt.apply_gradients(capped_gvs)
def main(gpu_id = None):
if gpu_id is not None:
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id
"""
Reset TensorFlow before running anything
"""
tf.reset_default_graph()
"""
Create the stimulus class to generate trial paramaters and input activity
"""
stim = stimulus.Stimulus()
f = pickle.load(open('./savedir/var_pulses_8_cue_off.pkl', 'rb'))
par['weights_trained'] = f['weights']
update_parameters(f['parameters'])
n_input, n_hidden, n_output = par['shape']
N = par['batch_train_size'] # trials per iteration, calculate gradients after batch_train_size
"""
Define all placeholder
"""
mask = tf.placeholder(tf.float32, shape=[par['num_time_steps'], par['batch_train_size']])
x = tf.placeholder(tf.float32, shape=[n_input, par['num_time_steps'], par['batch_train_size']]) # input data
y = tf.placeholder(tf.float32, shape=[n_output, par['num_time_steps'], par['batch_train_size']]) # target data
config = tf.ConfigProto()
#config.gpu_options.allow_growth=True
# enter "config=tf.ConfigProto(log_device_placement=True)" inside Session to check whether CPU/GPU in use
with tf.Session(config=config) as sess:
device = '/cpu:0' if gpu_id is None else '/gpu:0'
with tf.device(device):
model = Model(x, y, mask)
init = tf.global_variables_initializer()
sess.run(init)
# keep track of the model performance across training
model_performance = {'accuracy': [], 'pulse_accuracy': [], 'loss': [], 'perf_loss': [], 'spike_loss': [], 'trial': []}
for i in range(par['num_iterations']):
# generate batch of batch_train_size
trial_info = stim.generate_trial(analysis = False,num_fixed=0,var_delay=par['var_delay'],var_resp_delay=par['var_resp_delay'],var_num_pulses=par['var_num_pulses'])
if not par['var_num_pulses']:
onset = np.array([np.unique(np.array(trial_info['timeline']))[-2*p-2] for p in range(par['num_pulses'])][::-1])
pulse_masks = np.array([np.zeros((par['num_time_steps'], par['batch_train_size']),dtype=np.float32)] * par['num_pulses'])
for p in range(par['num_pulses']):
pulse_masks[p,onset[p]+par['mask_duration']//par['dt']:onset[p]+par['sample_time']//par['dt'],:] = 1
"""
Run the model
"""
_, loss, perf_loss, spike_loss, y_hat, state_hist, syn_x_hist, syn_u_hist = \
sess.run([model.train_op, model.loss, model.perf_loss, model.spike_loss, model.y_hat, \
model.hidden_state_hist, model.syn_x_hist, model.syn_u_hist], {x: trial_info['neural_input'], \
y: trial_info['desired_output'], mask: trial_info['train_mask']})
accuracy = analysis.get_perf(trial_info['desired_output'], y_hat, trial_info['train_mask'])
pulse_accuracy = []
if not par['var_num_pulses']:
for p in range(par['num_pulses']):
pulse_accuracy.append(analysis.get_perf(trial_info['desired_output'], y_hat, pulse_masks[p]))
model_performance = append_model_performance(model_performance, accuracy, pulse_accuracy, loss, perf_loss, spike_loss, (i+1)*N)
"""
Save the network model and output model performance to screen
"""
if i%par['iters_between_outputs']==0 and i > 0:
print_results(i, N, perf_loss, spike_loss, state_hist, accuracy)
if i%5000 == 0:
weights = eval_weights()
syn_x_stacked = np.stack(syn_x_hist, axis=1)
syn_u_stacked = np.stack(syn_u_hist, axis=1)
h_stacked = np.stack(state_hist, axis=1)
trial_time = np.arange(0,h_stacked.shape[1]*par['dt'], par['dt'])
mean_h = np.mean(np.mean(h_stacked,axis=2),axis=1)
results = {
'model_performance': model_performance,
'parameters': par,
'weights': weights,
'trial_time': trial_time,
'mean_h': mean_h,
'timeline': trial_info['timeline']}
pickle.dump(results, open(par['save_dir'] + par['save_fn'], 'wb') )
if accuracy > 0.995:
weights = eval_weights()
syn_x_stacked = np.stack(syn_x_hist, axis=1)
syn_u_stacked = np.stack(syn_u_hist, axis=1)
h_stacked = np.stack(state_hist, axis=1)
trial_time = np.arange(0,h_stacked.shape[1]*par['dt'], par['dt'])
mean_h = np.mean(np.mean(h_stacked,axis=2),axis=1)
results = {
'model_performance': model_performance,
'parameters': par,
'weights': weights,
'trial_time': trial_time,
'mean_h': mean_h,
'timeline': trial_info['timeline']}
pickle.dump(results, open(par['save_dir'] + par['save_fn'], 'wb') )
for b in range(10):
plot_list = [trial_info['desired_output'][:,:,b], softmax(np.array(y_hat)[:,:,b].T-np.max(np.array(y_hat)[:,:,b].T))]
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(7,7))
j = 0
for ax in axes.flat:
im = ax.imshow(plot_list[j], aspect='auto')
j += 1
cax,kw = mpl.colorbar.make_axes([ax for ax in axes.flat])
plt.colorbar(im, cax=cax, **kw)
plt.savefig("./savedir/output_"+str(par['num_pulses'])+"pulses_iter_"+str(i)+"_"+str(b)+".png")
plt.close()
plt.imshow(trial_info['neural_input'][:,:,b])
plt.savefig("./savedir/input_"+str(par['num_pulses'])+"pulses_iter_"+str(i)+"_"+str(b)+".png")
plt.close()
break
"""
Save model, analyze the network model and save the results
"""
#save_path = saver.save(sess, par['save_dir'] + par['ckpt_save_fn'])
if par['analyze_model']:
weights = eval_weights()
syn_x_stacked = np.stack(syn_x_hist, axis=1)
syn_u_stacked = np.stack(syn_u_hist, axis=1)
h_stacked = np.stack(state_hist, axis=1)
trial_time = np.arange(0,h_stacked.shape[1]*par['dt'], par['dt'])
mean_h = np.mean(np.mean(h_stacked,axis=2),axis=1)
results = {
'model_performance': model_performance,
'parameters': par,
'weights': weights,
'trial_time': trial_time,
'mean_h': mean_h,
'timeline': trial_info['timeline']}
pickle.dump(results, open(par['save_dir'] + par['save_fn'], 'wb') )
#x = pickle.load(open(par['save_dir'] + par['save_fn'], 'rb'))
#analysis.analyze_model(x, trial_info, y_hat, state_hist, syn_x_hist, syn_u_hist, model_performance, weights, analysis = False, test_mode_pulse=False, pulse=0, test_mode_delay=False, stim_num=0,\
#simulation = True, cut = False, lesion = False, tuning = True, decoding = True, load_previous_file = False, save_raw_data = False)
# Generate another batch of trials with test_mode = True (sample and test stimuli
# are independently drawn), and then perform tuning and decoding analysis
# trial_info = stim.generate_trial(test_mode = True)
# y_hat, state_hist, syn_x_hist, syn_u_hist = \
# sess.run([model.y_hat, model.hidden_state_hist, model.syn_x_hist, model.syn_u_hist], \
# {x: trial_info['neural_input'], y: trial_info['desired_output'], mask: trial_info['train_mask']})
# analysis.analyze_model(trial_info, y_hat, state_hist, syn_x_hist, syn_u_hist, model_performance, weights, \
# simulation = False, lesion = False, tuning = par['analyze_tuning'], decoding = True, load_previous_file = True, save_raw_data = False)
def softmax(x):
return np.exp(x)/np.sum(np.exp(x), axis=0)
def append_model_performance(model_performance, accuracy, pulse_accuracy, loss, perf_loss, spike_loss, trial_num):
model_performance['accuracy'].append(accuracy)
model_performance['pulse_accuracy'].append(pulse_accuracy)
model_performance['loss'].append(loss)
model_performance['perf_loss'].append(perf_loss)
model_performance['spike_loss'].append(spike_loss)
model_performance['trial'].append(trial_num)
return model_performance
def eval_weights():
with tf.variable_scope('rnn_cell', reuse=True):
W_in = tf.get_variable('W_in')
W_rnn = tf.get_variable('W_rnn')
b_rnn = tf.get_variable('b_rnn')
with tf.variable_scope('output', reuse=True):
W_out = tf.get_variable('W_out')
b_out = tf.get_variable('b_out')
weights = {
'w_in' : W_in.eval(),
'w_rnn' : W_rnn.eval(),
'w_out' : W_out.eval(),
'b_rnn' : b_rnn.eval(),
'b_out' : b_out.eval()
}
return weights
def print_results(iter_num, trials_per_iter, perf_loss, spike_loss, state_hist, accuracy):
print('Iter. {:4d}'.format(iter_num) + ' | Accuracy {:0.4f}'.format(accuracy) +
' | Perf loss {:0.4f}'.format(perf_loss) + ' | Spike loss {:0.4f}'.format(spike_loss) +
' | Mean activity {:0.4f}'.format(np.mean(state_hist)))