-
Notifications
You must be signed in to change notification settings - Fork 15
/
SNNTrainRoutine.py
378 lines (270 loc) · 10.7 KB
/
SNNTrainRoutine.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
'''
© Author: Saunak Saha
Iowa State University
'''
import numpy as np
import pandas as pd
import random
import time
import sys
import os
from brian2 import *
import matplotlib
#matplotlib.use('agg')
from scipy.misc import toimage
from matplotlib import pylab, pyplot
matplotlib.pyplot.switch_backend('agg')
from matplotlib.image import imread
from matplotlib.backends.backend_pdf import PdfPages
prefs.codegen.target = 'numpy'
def trainDataLoad(trainfile):
trainData = pd.read_csv(trainfile, header=None)
trainImg = (trainData.iloc[:,1:]).values
return trainImg
def weightLoad(weightfile):
weights = pd.read_csv(weightfile, header = None)
weights = weights.values
return weights
def paramLoad(parameterfile):
f = open(parameterfile,"r")
lines = f.readlines()
params = []
for x in lines:
params.append(x.split()[2])
f.close()
return params
def stimulusGenerator(examples_start, examples_end, rate_divider):
stimulus = TimedArray(np.vstack([[trainImg[c,:], trainImg[c,:], trainImg[c,:],
trainImg[c,:], trainImg[c,:], trainImg[c,:],
trainImg[c,:], np.zeros(N_Ip), np.zeros(N_Ip),
np.zeros(N_Ip)] for c in range(examples_start, examples_end)]), dt=50*ms)
Ip = PoissonGroup(N_Ip, rates = '(stimulus(t,i)/rate_divider)*Hz')
SpikeMon=SpikeMonitor(Ip)
SpikeNet= Network(Ip, SpikeMon)
SpikeNet.run(duration)
spikes_i=SpikeMon.i
spikes_t=SpikeMon.t
return (spikes_i, spikes_t)
def NeuronGroups(N_exc, tau_exc, Vth_exc, Vr_exc, Ref_exc,
N_inh, tau_inh, Vth_inh, Vr_inh, Ref_inh,
tau_theta, del_theta, spikes_i, spikes_t):
#Input Layer
SGG= SpikeGeneratorGroup(N_Ip, spikes_i, spikes_t)
#Excitatory Layer
eqs_exc = '''
dv/dt = (-v)/tau_exc: volt (unless refractory)
dtheta/dt = (-theta)/tau_theta: volt
'''
reset = '''
v= Vr_exc
theta+=del_theta
'''
Exc = NeuronGroup(N_exc, eqs_exc, threshold='v>(Vth_exc+theta)', reset=reset, refractory=Ref_exc, method='euler')
StateMon_exc = StateMonitor(Exc, ('v','theta'), record=False)
SpikeMon_exc = SpikeMonitor(Exc)
#Inhibitory Layer
eqs_inh = '''
dv/dt = (-v)/tau_inh: volt (unless refractory)
'''
Inh = NeuronGroup(N_inh, eqs_inh, threshold='v>Vth_inh', reset='v=Vr_inh', refractory=Ref_inh, method='euler')
StateMon_inh = StateMonitor(Inh, 'v', record=False)
return (SGG, Exc, Inh, StateMon_exc, StateMon_inh)
def SynapseGroups(N_exc,taupre, taupost, Apre, Apost, wmax, wmin, nu_pre, nu_post,
weights, we2i, wi2e):
#Delays
minDelay_S1 = 0*ms
maxDelay_S1 = 10*ms
delDelay_S1 = maxDelay_S1-minDelay_S1
minDelay_S2 = 0*ms
maxDelay_S2 = 5*ms
delDelay_S2 = maxDelay_S2-minDelay_S2
minDelay_S3 = 0*ms
maxDelay_S3 = 1*ms
delDelay_S3 = maxDelay_S3-minDelay_S3
#Input-Excitatory
S1=Synapses(SGG, Exc, '''
w : volt
dapre/dt = -apre/taupre : volt (event-driven)
dapost/dt = -apost/taupost : volt (event-driven)
''',
on_pre='''
v_post += w
apre =Apre*mV
w = clip(w+nu_pre*apost,0,wmax)
''',
on_post='''
apost =Apost*mV
w = clip(w+nu_post*apre,0,wmax)
''')
S1.connect(True)
S1.delay = 'minDelay_S1+rand()*delDelay_S1'
for neuron in range(0,N_exc):
S1.w[:,neuron] = (weights[neuron])*volt
StateMon_S1 = StateMonitor(S1, ['w', 'apre', 'apost'], record=S1[400,90])
#Excitatory-Inhibitory
S2 = Synapses(Exc, Inh, 'w : volt', on_pre = 'v_post+=w')
S2.connect(j ='i')
S2.w = we2i
S2.delay = 'minDelay_S2+rand()*delDelay_S2'
#Inhibtory-Excitatory
S3 = Synapses(Inh, Exc, 'w : volt', on_pre = 'v_post-=w')
S3.connect(condition ='j!=i')
S3.w = wi2e
S3.delay = 'minDelay_S3+rand()*delDelay_S3'
return (S1, S2, S3, StateMon_S1)
def SSMonitors(StateMonitor_exc, StateMonitor_inh, StateMonitor_S1):
figure(figsize = (20,20))
#for m in range(0,1):
#subplot(N_exc,1,m+1)
subplot(2,1,1)
plot(StateMon_exc.t/ms,StateMon_exc.v[56]/mV, 'C1')
plot(StateMon_inh.t/ms,StateMon_inh.v[56]/mV, 'C2')
plot(StateMon_exc.t/ms, StateMon_exc.theta[56]/mV, 'C3')
subplot(2,1,2)
plot(StateMon_exc.t/ms,StateMon_exc.v[57]/mV, 'C1')
plot(StateMon_inh.t/ms,StateMon_inh.v[57]/mV, 'C2')
plot(StateMon_exc.t/ms, StateMon_exc.theta[57]/mV, 'C3')
return
def RFMonitor(N_exc,S1,cmap):
grid = np.zeros((N_exc,28,28))
for x in range(0,N_exc):
grid[x] = np.array(S1.w[:,x].reshape(28,28))
fig1, axes = pyplot.subplots(nrows=10, ncols=10, sharex = 'col', sharey = 'row', figsize=(8,8))
for x in range(0,10):
for y in range(0,10):
im = axes[x,y].imshow(grid[(x*10)+y], interpolation ='none', aspect = 'auto', cmap=cmap)
fig1.colorbar(im, ax = axes.ravel().tolist())
return fig1
def SaveWeights(N_exc):
trainedWeights=np.zeros((N_exc,784))
for m in range(0,N_exc):
trainedWeights[m]=np.array(S1.w[:,m])
if os.path.exists('trainedWeights.csv'):
os.remove('trainedWeights.csv')
np.savetxt('trainedWeights.csv', trainedWeights, delimiter=",")
return
#-----------------------------------------------------------------
# Data Loading and Processing
#-----------------------------------------------------------------
start_time = time.time()
trainImg = trainDataLoad('mnist_train.csv')
print("--- Data Load Time %s (s) ---" % (time.time() - start_time))
start_time_1 = time.time()
examples_start = int(sys.argv[1])
examples_end = int(sys.argv[2])
num_examples = (examples_end - examples_start)
duration = (350+150)*num_examples*ms
Mode = 0
simNumber = 0
sim_Mode = sys.argv[4]
if (sim_Mode.lower() == "dse"):
Mode = 1
paramName = sys.argv[5]
simNumber = int(sys.argv[6])
print("--- Entering Design Space Exploration Mode ---")
elif (sim_Mode.lower() == "standalone"):
Mode = 2
simNumber = 1
print("--- Entering Standalone Mode ---")
else:
print("ERROR: --- Invalid Simulation Mode ---")
start_point = sys.argv[3]
N_Ip=784
#-----------------------------------------------------------------
# Simulation starts here
#-----------------------------------------------------------------
start_time = time.time()
if os.path.exists("RF_results.pdf"):
os.remove("RF_results.pdf")
figfile = PdfPages('RF_results.pdf')
for simCount in range(0,simNumber):
start_time = time.time()
if (Mode == 1):
params = paramLoad('parameters_'+paramName+'_'+str(simCount)+'.txt')
elif (Mode == 2):
params = paramLoad('parameters_default.txt')
else:
break
#-----------------------------------------------------------------
# Stimulus generation
#-----------------------------------------------------------------
rate_divider = float(params[0])
spikes_i, spikes_t = stimulusGenerator(examples_start, examples_end, rate_divider)
#-----------------------------------------------------------------
# Neuron Parameters
#-----------------------------------------------------------------
#Excitatory
N_exc = int(params[1])
tau_exc = float(params[2])*ms
Vth_exc = float(params[3])*mV
Vr_exc = float(params[4])*mV
Ref_exc = float(params[5])*ms
#Inhibitory
N_inh = float(params[6])
tau_inh = float(params[7])*ms
Vth_inh = float(params[8])*mV
Vr_inh = float(params[9])*mV
Ref_inh = float(params[10])*ms
#Homeostasis
tau_theta = float(params[11])*ms
del_theta = float(params[12])*mV
#Build Neuron Groups with parameters
SGG,Exc,Inh,StateMon_exc, StateMon_inh = NeuronGroups(N_exc, tau_exc, Vth_exc, Vr_exc, Ref_exc,
N_inh, tau_inh, Vth_inh, Vr_inh, Ref_inh,
tau_theta, del_theta, spikes_i, spikes_t)
#-----------------------------------------------------------------
# Synapse Parameters
#-----------------------------------------------------------------
#STDP
taupre = float(params[13])*ms
taupost = float(params[14])*ms
Apre = float(params[15])
Apost = float(params[16])
wmax = float(params[17])*mV
wmin = float(params[18])*mV
nu_pre = float(params[19])
nu_post = float(params[20])
#Ip - Excitatory
weights = np.zeros((N_exc,784))
if (start_point.lower() == "continue"):
if os.path.exists('trainedWeights.csv'):
weights = weightLoad('trainedWeights.csv')
else:
weights.fill(1e-4)
elif (start_point.lower() == "start"):
weights.fill(1e-4)
else:
print("ERROR: --- Invalid Start Mode ---")
break
#Excitatory - Inhibitory
we2i = float(params[21])*mV
wi2e = float(params[22])*mV
#Build Synapse Groups with parameters
S1, S2, S3, StateMon_S1 = SynapseGroups(N_exc, taupre, taupost, Apre, Apost, wmax, wmin, nu_pre, nu_post,
weights, we2i, wi2e)
#-----------------------------------------------------------------
# Build Network
#-----------------------------------------------------------------
SNNet = Network(SGG,Exc,Inh,
S1,S2,S3,
StateMon_exc, StateMon_inh, StateMon_S1)
SNNet.run(duration)
#-----------------------------------------------------------------
# Weights/Receptive-Fields
#-----------------------------------------------------------------
figure = RFMonitor(N_exc,S1,'YlOrRd')
if(sim_Mode.lower() == "dse"):
figure.suptitle(paramName+' = value '+str(simCount))
if(sim_Mode.lower() == "standalone"):
figure.suptitle('All current default values')
figfile.savefig(figure)
#-----------------------------------------------------------------
# Saving Trained Weights
#-----------------------------------------------------------------
SaveWeights(N_exc)
if(sim_Mode.lower() == "dse"):
os.rename('trainedWeights.csv', 'trainedWeights_'+paramName+'_'+str(simCount)+'.csv')
if(sim_Mode.lower() == "dse"):
print("--- Simulation %s completed in %s (s) ---" % (simCount, (time.time() - start_time)))
figfile.close()
print("--- Total exec time: %s (s) ---" % (time.time() - start_time_1))