-
Notifications
You must be signed in to change notification settings - Fork 4
/
MNIST_SNAS_v1.py
305 lines (248 loc) · 10.5 KB
/
MNIST_SNAS_v1.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
# -*- coding: utf-8 -*-
"""
The Neural Structure Search (NAS) of large scale Liquid State Machine
(LSM) for MNIST. The optimization method adopted
here is CMA-ES, BO and Gaussian process assisted CMA-ES.
:Author: Yan Zhou
:License: BSD 3-Clause, see LICENSE file.
Requirement
=======
Numpy
Pandas
Brian2
Usage
=======
Citation
=======
"""
from Brian2_scripts.sim_brian_paper.sim_brian_paper_SNAS.src import *
from functools import partial
from multiprocessing import Pool
from brian2 import *
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
warnings.filterwarnings("ignore")
prefs.codegen.target = "numpy"
start_scope()
np.random.seed(100)
data_path = '../../../Data/MNIST_data/'
###################################
# -----simulation parameter setting-------
coding_n = 3
MNIST_shape = (1, 784)
coding_duration = 30
duration = coding_duration * MNIST_shape[0]
F_train = 0.05
F_validation = 0.00833333
F_test = 0.05
Dt = defaultclock.dt = 1 * ms
# -------class initialization----------------------
function = MathFunctions()
base = BaseFunctions()
readout = Readout()
MNIST = MNIST_classification(MNIST_shape, duration)
# -------data initialization----------------------
MNIST.load_Data_MNIST_all(data_path)
df_train_validation = MNIST.select_data(F_train + F_validation, MNIST.train)
df_train, df_validation = train_test_split(df_train_validation, test_size=F_validation / (F_validation + F_train),
random_state=42)
df_test = MNIST.select_data(F_test, MNIST.test)
df_en_train = MNIST.encoding_latency_MNIST(MNIST._encoding_cos_rank_ignore_0, df_train, coding_n)
df_en_validation = MNIST.encoding_latency_MNIST(MNIST._encoding_cos_rank_ignore_0, df_validation, coding_n)
df_en_test = MNIST.encoding_latency_MNIST(MNIST._encoding_cos_rank_ignore_0, df_test, coding_n)
data_train_s, label_train = MNIST.get_series_data_list(df_en_train, is_group=True)
data_validation_s, label_validation = MNIST.get_series_data_list(df_en_validation, is_group=True)
data_test_s, label_test = MNIST.get_series_data_list(df_en_test, is_group=True)
# -------get numpy random state------------
np_state = np.random.get_state()
############################################
# ---- define network run function----
def run_net(inputs, **parameter):
"""
run_net(inputs, parameter)
Parameters = [R, p_inE/I, f_in, f_EE, f_EI, f_IE, f_II, tau_ex, tau_inh]
----------
"""
# ---- set numpy random state for each run----
np.random.set_state(np_state)
# -----parameter setting-------
n_ex = 1600
n_inh = int(n_ex / 4)
n_input = MNIST_shape[1] * coding_n
n_read = n_ex + n_inh
R = parameter['R']
f_in = parameter['f_in']
f_EE = parameter['f_EE']
f_EI = parameter['f_EI']
f_IE = parameter['f_IE']
f_II = parameter['f_II']
A_EE = 60 * f_EE
A_EI = 60 * f_EI
A_IE = 60 * f_IE
A_II = 60 * f_II
A_inE = 60 * f_in
A_inI = 60 * f_in
tau_ex = parameter['tau_ex'] * coding_duration
tau_inh = parameter['tau_inh'] * coding_duration
tau_read = 30
p_inE = parameter['p_in'] * 0.1
p_inI = parameter['p_in'] * 0.1
# ------definition of equation-------------
neuron_in = '''
I = stimulus(t,i) : 1
'''
neuron = '''
tau : 1
dv/dt = (I-v) / (tau*ms) : 1 (unless refractory)
dg/dt = (-g)/(3*ms) : 1
dh/dt = (-h)/(6*ms) : 1
I = (g+h)+13.5: 1
x : 1
y : 1
z : 1
'''
neuron_read = '''
tau : 1
dv/dt = (I-v) / (tau*ms) : 1
dg/dt = (-g)/(3*ms) : 1
dh/dt = (-h)/(6*ms) : 1
I = (g+h): 1
'''
synapse = '''
w : 1
'''
on_pre_ex = '''
g+=w
'''
on_pre_inh = '''
h-=w
'''
# -----Neurons and Synapses setting-------
Input = NeuronGroup(n_input, neuron_in, threshold='I > 0', method='euler', refractory=0 * ms,
name='neurongroup_input')
G_ex = NeuronGroup(n_ex, neuron, threshold='v > 15', reset='v = 13.5', method='euler', refractory=3 * ms,
name='neurongroup_ex')
G_inh = NeuronGroup(n_inh, neuron, threshold='v > 15', reset='v = 13.5', method='euler', refractory=2 * ms,
name='neurongroup_in')
G_readout = NeuronGroup(n_read, neuron_read, method='euler', name='neurongroup_read')
S_inE = Synapses(Input, G_ex, synapse, on_pre=on_pre_ex, method='euler', name='synapses_inE')
S_inI = Synapses(Input, G_inh, synapse, on_pre=on_pre_ex, method='euler', name='synapses_inI')
S_EE = Synapses(G_ex, G_ex, synapse, on_pre=on_pre_ex, method='euler', name='synapses_EE')
S_EI = Synapses(G_ex, G_inh, synapse, on_pre=on_pre_ex, method='euler', name='synapses_EI')
S_IE = Synapses(G_inh, G_ex, synapse, on_pre=on_pre_inh, method='euler', name='synapses_IE')
S_II = Synapses(G_inh, G_inh, synapse, on_pre=on_pre_inh, method='euler', name='synapses_I')
S_E_readout = Synapses(G_ex, G_readout, 'w = 1 : 1', on_pre=on_pre_ex, method='euler')
S_I_readout = Synapses(G_inh, G_readout, 'w = 1 : 1', on_pre=on_pre_inh, method='euler')
# -------initialization of neuron parameters----------
G_ex.v = '13.5+1.5*rand()'
G_inh.v = '13.5+1.5*rand()'
G_readout.v = '0'
G_ex.g = '0'
G_inh.g = '0'
G_readout.g = '0'
G_ex.h = '0'
G_inh.h = '0'
G_readout.h = '0'
G_ex.tau = tau_ex
G_inh.tau = tau_inh
G_readout.tau = tau_read
[G_ex, G_in] = base.allocate([G_ex, G_inh], 10, 10, 20)
# -------initialization of network topology and synapses parameters----------
S_inE.connect(condition='j<0.3*N_post', p=p_inE)
S_inI.connect(condition='j<0.3*N_post', p=p_inI)
S_EE.connect(condition='i != j', p='0.3*exp(-((x_pre-x_post)**2+(y_pre-y_post)**2+(z_pre-z_post)**2)/R**2)')
S_EI.connect(p='0.2*exp(-((x_pre-x_post)**2+(y_pre-y_post)**2+(z_pre-z_post)**2)/R**2)')
S_IE.connect(p='0.4*exp(-((x_pre-x_post)**2+(y_pre-y_post)**2+(z_pre-z_post)**2)/R**2)')
S_II.connect(condition='i != j', p='0.1*exp(-((x_pre-x_post)**2+(y_pre-y_post)**2+(z_pre-z_post)**2)/R**2)')
S_E_readout.connect(j='i')
S_I_readout.connect(j='i+n_ex')
S_inE.w = function.gamma(A_inE, S_inE.w.shape)
S_inI.w = function.gamma(A_inI, S_inI.w.shape)
S_EE.w = function.gamma(A_EE, S_EE.w.shape)
S_IE.w = function.gamma(A_IE, S_IE.w.shape)
S_EI.w = function.gamma(A_EI, S_EI.w.shape)
S_II.w = function.gamma(A_II, S_II.w.shape)
S_EE.pre.delay = '1.5*ms'
S_EI.pre.delay = '0.8*ms'
S_IE.pre.delay = '0.8*ms'
S_II.pre.delay = '0.8*ms'
# ------create network-------------
net = Network(collect())
net.store('init')
# ------run network-------------
stimulus = TimedArray(inputs[0], dt=Dt)
net.run(duration * Dt)
states = net.get_states()['neurongroup_read']['v']
net.restore('init')
return (states, inputs[1])
@Timelog
@AddParaName
def parameters_search(**parameter):
# ------parallel run for train-------
states_train_list = pool.map(partial(run_net, **parameter), [(x) for x in zip(data_train_s, label_train)])
# ------parallel run for validation-------
states_validation_list = pool.map(partial(run_net, **parameter),
[(x) for x in zip(data_validation_s, label_validation)])
# ----parallel run for test--------
states_test_list = pool.map(partial(run_net, **parameter), [(x) for x in zip(data_test_s, label_test)])
# ------Readout---------------
states_train, states_validation, states_test, _label_train, _label_validation, _label_test = [], [], [], [], [], []
for train in states_train_list:
states_train.append(train[0])
_label_train.append(train[1])
for validation in states_validation_list:
states_validation.append(validation[0])
_label_validation.append(validation[1])
for test in states_test_list:
states_test.append(test[0])
_label_test.append(test[1])
states_train = (MinMaxScaler().fit_transform(np.asarray(states_train))).T
states_validation = (MinMaxScaler().fit_transform(np.asarray(states_validation))).T
states_test = (MinMaxScaler().fit_transform(np.asarray(states_test))).T
score_train, score_validation, score_test = readout.readout_sk(states_train, states_validation, states_test,
np.asarray(_label_train),
np.asarray(_label_validation),
np.asarray(_label_test), solver="lbfgs",
multi_class="multinomial")
# ----------show results-----------
print('parameters %s' % parameter)
print('Train score: ', score_train)
print('Validation score: ', score_validation)
print('Test score: ', score_test)
return 1 - score_validation, 1 - score_test, 1 - score_train, parameter
##########################################
# -------optimizer settings---------------
if __name__ == '__main__':
core = 8
pool = Pool(core)
parameters = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
bounds = {'R': (0.0001, 1), 'p_in': (0.0001, 1), 'f_in': (0.0001, 1), 'f_EE': (0.0001, 1), 'f_EI': (0.0001, 1),
'f_IE': (0.0001, 1), 'f_II': (0.0001, 1), 'tau_ex': (0.0001, 1), 'tau_inh': (0.0001, 1)}
parameters_search.func.keys = list(bounds.keys())
LHS_path = './LHS_MNIST.dat'
SNAS = 'SAES'
# -------parameters search---------------
if SNAS == 'BO':
optimizer = BayesianOptimization_(
f=parameters_search,
pbounds=bounds,
random_state=np.random.RandomState(),
)
logger = bayes_opt.observer.JSONLogger(path="./BO_res_MNIST.json")
optimizer.subscribe(bayes_opt.event.Events.OPTMIZATION_STEP, logger)
optimizer.minimize(
LHS_path=LHS_path,
init_points=50,
is_LHS=True,
n_iter=250,
acq='ei',
opt=optimizer.acq_min_DE,
)
elif SNAS == 'SAES':
saes = SAES(parameters_search, 'ei', parameters, 0.5,
**{'ftarget': -1e+3, 'bounds': bounds, 'maxiter': 500,'tolstagnation': 500})
saes.run_best_strategy(50, 1, 2, LHS_path=LHS_path)
elif SNAS == 'CMA':
res = cma.fmin(parameters_search, parameters, 0.5,
options={'ftarget': -1e+3, 'maxiter': 30,
'bounds': np.array([list(x) for x in list(bounds.values())]).T.tolist()})