-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp0.py
262 lines (178 loc) · 7.05 KB
/
exp0.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
from brian2 import *
from neurons import IF, IF_m
from neurons import Vr, Vt, tau
from synapses import model_stdp, action_prespike_stdp, action_postspike_stdp
from synapses import model_mstdp, action_prespike_mstdp, action_postspike_mstdp
from synapses import model_mstdpet, action_prespike_mstdpet, action_postspike_mstdpet
# from synapses import Apre, Apost, taupre, taupost, tauz
# from synapses import gamma0, gamma1, gamma2
from utils import generate_input_spikes, plot_rates
import matplotlib.pyplot as plt
import os
# np.set_printoptions(threshold=np.nan)
# set_device('cpp_standalone', build_on_run=False)
Apre = 1
Apost = -1
taupre = 20*ms
taupost = 20*ms
tauz = 25*ms
gamma0 = 0.2 * mV
gamma1 = 0.1 * mV
gamma2 = 0.8 * mV
class Experiment0:
def __init__(self, args):
if args.seed is not None:
np.random.seed(args.seed)
seed(args.seed)
print(" Info: Seed set to {}".format(args.seed))
else:
print(" Warning: Consider setting seed to ensure reproducibility")
defaultclock.dt = 1*ms
print("Experiment 0: Learing XOR with rate coded input")
self.args = args
self.define_network()
self.restore_model()
if args.test == 0:
self.train()
else:
self.test()
# device.build(directory='bin', compile=True, run=True, debug=False)
def define_network(self):
self.ilayer = SpikeGeneratorGroup(60, [], []*ms)
self.hlayer = NeuronGroup(60, IF_m, threshold='v>Vt', reset='v=Vr', method='linear')
self.olayer = NeuronGroup(1, IF_m, threshold='v>Vt', reset='v=Vr', method='linear')
if self.args.rule == "mstdp":
self.sih = Synapses(self.ilayer, self.hlayer, model=model_mstdp, on_pre=action_prespike_mstdp, on_post=action_postspike_mstdp)
self.sho = Synapses(self.hlayer, self.olayer, model=model_mstdp, on_pre=action_prespike_mstdp, on_post=action_postspike_mstdp)
else:
self.sih = Synapses(self.ilayer, self.hlayer, model=model_mstdpet, on_pre=action_prespike_mstdpet, on_post=action_postspike_mstdpet)
self.sho = Synapses(self.hlayer, self.olayer, model=model_mstdpet, on_pre=action_prespike_mstdpet, on_post=action_postspike_mstdpet)
self.sih.connect()
self.sho.connect()
self.smon_sih = StateMonitor(self.sih, 'w', record=True)
self.smon_sho = StateMonitor(self.sho, 'w', record=True)
# self.smon_hlayer = StateMonitor(self.hlayer, 'v', record=True)
self.smon_olayer = StateMonitor(self.olayer, 'v', record=True)
# self.kmon_ilayer = SpikeMonitor(self.ilayer)
self.kmon_hlayer = SpikeMonitor(self.hlayer)
self.kmon_olayer = SpikeMonitor(self.olayer)
self.network = Network(
self.ilayer,
self.hlayer,
self.olayer,
self.sih,
self.sho,
self.kmon_olayer,
)
if self.args.verbose:
self.network.add(
self.smon_sih,
self.smon_sho,
# self.smon_hlayer,
self.smon_olayer,
self.kmon_hlayer,
# self.kmon_ilayer,
# self.smon_olayer_r,
)
self.hlayer.v = Vr
self.olayer.v = Vr
self.hlayer.r = 1
self.olayer.r = 1
self.set_synapse_bounds()
def set_synapse_bounds(self):
self.sih.wmin = -5*mV
self.sih.wmax = 5*mV
self.sho.wmin = -5*mV
self.sho.wmax = 5*mV
# self.sih.wmin = 0*mV
# self.sho.wmin = 0*mV
# set half inhibitory and half excitatory for first layer
inhib = np.random.choice(30, 15, replace=False)
inhib = np.concatenate([inhib, 30 + np.random.choice(30, 15, replace=False)], axis=0)
extit = np.array( list(set(np.arange(60)).difference(set(inhib))) )
self.sih.wmin[extit, :] = 0*mV
self.sih.wmax[inhib, :] = 0*mV
def initialize_weights(self):
print("Initializing weights at random")
# w1 = np.random.uniform(size=60*60)
# w2 = np.random.uniform(size=60*1)
w1 = (np.clip(np.random.randn(60*60), -1, 1)+1)/2
w2 = (np.clip(np.random.randn(60*1), -1, 1)+1)/2
self.sih.w = (self.sih.wmax-self.sih.wmin) * w1 + self.sih.wmin
self.sho.w = (self.sho.wmax-self.sho.wmin) * w2 + self.sho.wmin
def set_plasticity(self, plasticity=True):
self.sih.plastic = plasticity
self.sho.plastic = plasticity
# self.sih.plastic = False
# self.sho.plastic = False
def restore_model(self):
if os.path.isfile(self.args.model):
self.network.restore(filename=self.args.model)
else:
self.initialize_weights()
self.save_model()
def save_model(self):
self.network.store(filename=self.args.model)
print("Saved Model in {}".format(self.args.model))
def train(self):
# import pdb; pdb.set_trace()
print("Training")
self.set_plasticity(True)
# self.set_plasticity(False)
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
order = np.arange(x.shape[0])
try:
for i in range(1, self.args.nepochs+1):
print("Epoch: {eno:d}".format(eno=i))
np.random.shuffle(order)
for j, index in enumerate(order):
indices, times = generate_input_spikes(30*np.sum(x[index]), 40*Hz, 500*ms)
times += self.network.t
# times += (i-1)*2000*ms + j*500*ms
if (x[index] == [0, 1]).all():
indices += 30
# if self.args.verbose:
# print(" .{} Spike Range: {}/{}".format(j, np.min(times) if times.size != 0 else None, np.max(times) if times.size != 0 else None))
# print(" .{} Indices Range: {}/{}".format(j, np.min(indices) if indices.size != 0 else None, np.max(indices) if indices.size != 0 else None))
# import pdb; pdb.set_trace()
self.ilayer.set_spikes(indices, times, sorted=True)
self.hlayer.r = y[index]*2-1
self.olayer.r = y[index]*2-1
self.network.run(500*ms)
if self.args.verbose:
# print("Total Spikes: {} {} {}".format(self.kmon_ilayer.num_spikes, self.kmon_hlayer.num_spikes, self.kmon_olayer.num_spikes))
print("Total Spikes: {} {}".format(self.kmon_hlayer.num_spikes, self.kmon_olayer.num_spikes))
print("Max/Min Weights: {}/{} {}/{}".format(np.min(self.smon_sih.w[:, -1]), np.max(self.smon_sih.w[:, -1]), np.min(self.smon_sho.w[:, -1]), np.max(self.smon_sho.w[:, -1])))
# print("H Voltage:", np.mean(self.smon_hlayer.v[:, -20000:]))
print("Output Voltage:", np.mean(self.smon_olayer.v[0, -20000:]))
# import pdb; pdb.set_trace()
if i % self.args.nepochs_per_save == 0:
self.save_model()
except KeyboardInterrupt as e:
print("Training Interrupted. Refer to model saved in {}".format(self.args.model))
def test(self):
# import pdb; pdb.set_trace()
print("Testing")
self.set_plasticity(False)
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
rate = np.zeros((self.args.nepochs, y.shape[0]))*Hz
for q in range(self.args.nepochs):
# print("Run {}".format(q))
for j, _input in enumerate(x):
start = self.network.t
indices, times = generate_input_spikes(30*np.sum(_input), 40*Hz, 500*ms)
times += start
if (_input == [0, 1]).all():
indices += 30
self.ilayer.set_spikes(indices, times)
self.network.run(500*ms)
end = self.network.t
# import pdb; pdb.set_trace()
spikes = self.kmon_olayer.t
spikes = spikes[spikes <= end]
spikes = spikes[start <= spikes]
rate[q, j] = spikes.size / (500*ms)
print("Rate", rate[q])
plot_rates(rate, "outputs/exp{}_{}_rates.png".format(0, self.args.rule))