-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWTA.py
176 lines (132 loc) · 5.61 KB
/
WTA.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
import bindsnet, pickle, torch, numpy as np, cv2, os, glob
from tqdm import tqdm
from torchvision import transforms
device = 'cuda'
gpu = True
files = glob.glob('debug/*.jpg', recursive=True)
for f in files:
os.remove(f)
poisson_intensity = 100.0
batch_size = 256
display_step = 512 // batch_size
nb_hidden = 1000
out_width = 30
nb_output = out_width * out_width
LR = 1e-3 # 1e-6
pat_LR = 2e-3
norm_speed = np.power(0.01, 32.0 / batch_size)
sim_duration = 50
inh_level = -1.0
c1_wmax = 2.0
c3_wmax = 2.0
print('Building network...')
model = bindsnet.network.Network(dt=1, batch_size=batch_size, learning=True)
m_input = bindsnet.network.nodes.Input(784, shape=(1, 784), traces=True)
m_hidden = bindsnet.network.nodes.LIFNodes(nb_hidden, traces=True, tc_decay=500)
m_output = bindsnet.network.nodes.LIFNodes(nb_output, traces=True, tc_decay=500)
model.add_layer(m_input, 'Input')
model.add_layer(m_hidden, 'Hidden')
model.add_layer(m_output, 'Output')
w_inh = np.zeros((nb_output, nb_output), dtype=np.float32)
for i in range(nb_output):
x_i = i%out_width
y_i = i//out_width
for j in range(nb_output):
x_j = j%out_width
y_j = j//out_width
dx = x_i - x_j
dy = y_i - y_j
d = np.sqrt(dx*dx + dy*dy)
w_inh[i, j] = 1 - 0.15*d
w_inh[w_inh<0] = 0
w_inh *= inh_level
np.fill_diagonal(w_inh, 0.0)
print('Avg inh=', np.mean(w_inh))
my_w = np.random.normal(loc=0, scale=c1_wmax*0.5, size=(784, nb_hidden))
con1 = bindsnet.network.topology.Connection(source=m_input, target=m_hidden, nu=[c1_wmax*LR, c1_wmax*LR], update_rule=bindsnet.learning.PostPre, w=torch.Tensor(my_w), wmin=-c1_wmax, wmax=c1_wmax, weight_decay=0.00001, reduction=torch.mean)
my_w = np.random.normal(loc=0, scale=c3_wmax*0.5, size=(nb_hidden, nb_output))
con3 = bindsnet.network.topology.Connection(source=m_hidden, target=m_output, nu=[c3_wmax*LR, c3_wmax*LR], update_rule=bindsnet.learning.PostPre, w=torch.Tensor(my_w), wmin=-c3_wmax, wmax=c3_wmax, weight_decay=0.00001, reduction=torch.mean)
con4 = bindsnet.network.topology.Connection(source=m_output, target=m_output, nu=[0.0, 0.0], update_rule=bindsnet.learning.NoOp, w=torch.Tensor(w_inh), wmin=-1000.0, wmax=1000.0, reduction=torch.sum)
model.add_connection(con1, source='Input', target='Hidden')
model.add_connection(con3, source='Hidden', target='Output')
model.add_connection(con4, source='Output', target='Output')
print(bindsnet.analysis.visualization.summary(model))
out_spikes = bindsnet.network.monitors.Monitor(model.layers['Output'], state_vars=["s"], time=sim_duration)
model.add_monitor(out_spikes, name="out_spikes")
model.to(device)
# Load MNIST data.
train_dataset = bindsnet.datasets.MNIST(
bindsnet.encoding.PoissonEncoder(time=sim_duration, dt=1),
None,
root=os.path.join("data", "MNIST"),
download=True,
train=True,
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Lambda(lambda x: x * poisson_intensity)]
),
)
# Create a dataloader to iterate and batch data
dataloader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True, num_workers=16, pin_memory=gpu
)
acc = 0.0
best_acc = 0.0
def first_spikes(s):
nbt = s.shape[0]
nbn = s.shape[1]
time_exp = 0.05
first = np.full(nbn, nbt, dtype=np.float32)
for i in range(nbn):
for j in range(nbt):
if s[j, i]:
first[i] = j
break
return np.exp(-first * time_exp)
patterns = np.zeros(shape=(10, nb_output), dtype=np.float32)
for step, batch in enumerate(tqdm(dataloader)):
i = batch["encoded_image"].view(batch_size, sim_duration, 1, 784)
i = i.permute(1, 0, 2, 3)
inputs = {"Input": i}
if gpu:
inputs = {k: v.cuda() for k, v in inputs.items()}
label = batch["label"]
model.reset_state_variables()
model.run(inputs=inputs, time=sim_duration, input_time_dim=1)
spike_record = out_spikes.get("s").squeeze().to('cpu').numpy().astype(np.float32)
for e in range(batch_size):
spike_sum = first_spikes(spike_record[:, e, :])
best = 10000000.0
result = -1
for i in range(10):
d = np.sum(np.square(patterns[i, :] - spike_sum))
if d < best:
best = d
result = i
if result == label[e]:
acc = acc*0.995 + 0.005
else:
acc = acc*0.995
patterns[label[e], :] = (1.0 - pat_LR) * patterns[label[e], :] + pat_LR * spike_sum
if step % display_step==0:
if acc>best_acc:
best_acc = acc
print('\n accuracy=', 100.0*acc, '%', 'best=', 100.0*best_acc)
print('w1=', torch.mean(torch.abs(con1.w)).to('cpu').numpy(), 'w3=', torch.mean(torch.abs(con3.w)).to('cpu').numpy())
print('avg time=', np.mean(spike_sum), ' fast =', np.max(spike_sum), ' slow', np.min(spike_sum))
img = 0.0 + 5.0 * 128.0 * spike_sum
img = np.reshape(img, (out_width, out_width))
img = cv2.resize(img, (500, 500), interpolation=cv2.INTER_NEAREST)
cv2.imwrite('debug/rates'+str(int(label[e]))+'.jpg', img)
for i in range(10):
img = 0.0 + 5.0 * 128.0 * patterns[i]
img = np.reshape(img, (out_width, out_width))
img = cv2.resize(img, (500, 500), interpolation=cv2.INTER_NEAREST)
cv2.imwrite('debug/pattern'+str(i)+'.jpg', img)
# slow re-normalizing
if True:
m1 = torch.mean(torch.abs(con1.w))
m3 = torch.mean(torch.abs(con3.w))
ratio1 = (1.0 - norm_speed) + norm_speed * 0.5 * con1.wmax / m1
ratio3 = (1.0 - norm_speed) + norm_speed * 0.5 * con3.wmax / m3
con1.w *= ratio1
con3.w *= ratio3