-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTempotron.m
433 lines (394 loc) · 15.6 KB
/
Tempotron.m
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
clear all; clc; clf;
close all; %% close all figure windows
data = importdata('Iris_train.txt');
seq = randperm(length(data)); %% random sequence from 1 to length of data
data = data(seq,:); %% random sort the data
feature = data(:,1:4);
target = data(:,5);
neurons = 48;
%% normalization
for i = 1:4
feature(:,i) = (feature(:,i)-min(feature(:,i)))/(max(feature(:,i))-min(feature(:,i)));
end
%% input value = gaussian function x-axis index, thus convert decimal to integer
input = feature*10000; % Match the index of Gaussian receptive field matrix
input = fix(input); % exclude the value originally below 4th decimal point
% Avoid the "1" value to be gaussian matrix index(should be positive integer)
for i = 1:size(input,1)
for j = 1:size(input,2)
if input(i,j) == 0
input(i,j) = 1;
end
end
end
%% Encoding
% Single input variable encoded by 12 GRF neurons
InputSpike = zeros(length(input),neurons); % Training samples, 48 activate values(spike times)
for i = 1:length(input)
InputSpike(i,:) = GRF(input(i,:)); % input each variables to Gaussian receptive field
end
% Ignore the activate value < 0.1 which is not able to simulate a spike
for i = 1:length(InputSpike)
for j = 1:4
if InputSpike(i,j) < 0.1
InputSpike(i,j) = 0;
end
end
end
InputSpike = 100*(1 - InputSpike); %% linear transform to spike time: t = 1 for train
% = 0, t = 100 for the maximum value
InputSpike = round(InputSpike); % spike times with 1 ms precision
for i = 1:size(InputSpike,1) % Adjust t = 0 firing to t = 1
for j = 1:size(InputSpike,2)
if InputSpike(i,j) == 0
InputSpike(i,j) = 1;
end
end
end
%% take 120 for train data, 30 for test data
test = InputSpike(101:150,:);
test_target = target(101:150);
train_target = target(1:100);
InputSpike = InputSpike(1:100,:); %% set 'train' = 'InputSpike'
%% Temporal learning
iteration = 50;
V = zeros(3,100);
V_rest = 0;
weight = rand(neurons,3);
threshold = 1; % set up V threshold: try and error
T = 100; %% time encoding window
firing_time = zeros(1,neurons); %% time-to-first-spike for 48 neurons
Response = zeros(1,neurons); %% K(t) response value array
lr = 0.005;
Tou_m = 1.5;
Time = 1:1:T;
correct_rate = zeros(1,iteration);
for Iterate = 1:iteration %% Run 100 times
%% Test the correct rate each time
correct = 0;
for test_sample = 1:size(test,1)
t_max = 100*ones(1,3); %% default: all neuron reach max V at the end
Max_state = zeros(1,3); % strongest state of each output neuron
% In each iteration, T = 100ms
for t = 1:T
for neuron = 1:neurons %% Response function for 48 neurons at time t
Response(neuron) = K(t,test(test_sample,neuron));
end
% Calculate PSP
for j = 1:3
V(j,t) = Response*weight(:,j) + V_rest;
end
end
%% find t_max: first index that V cross threshold
for j = 1:3
for timing = 1:T
if V(j,timing) >= threshold
t_max(j) = timing;
Max_state(j) = V(j,timing);
break; % first time V > threshold
end
end
if t_max(j) < T % neuron j has fired
V(j,t_max(j):end) = V(j,t_max(j)).*exp(-(Time(t_max(j):end)-Time(t_max(j)))/Tou_m);
end
end
[~,output_class] = min(t_max);
if test_target(test_sample) == output_class
correct = correct + 1;
end
end
correct_rate(Iterate) = correct/size(test,1);
%% Training
% Train for class 1
for samples = 1:size(InputSpike,1) %% Training samples for each iteration
t_max = 100*ones(1,3); %% default: all neuron reach max V at the end
Max_state = zeros(1,3); % strongest state of each output neuron
% In each iteration, T = 100ms
for t = 1:T
for neuron = 1:neurons %% Response function for 48 neurons at time t
Response(neuron) = K(t,InputSpike(samples,neuron));
end
% Calculate PSP
for j = 1:3
V(j,t) = Response*weight(:,j) + V_rest;
end
end
%% find t_max: first index that V cross threshold
for j = 1:3
for timing = 1:T
if V(j,timing) >= threshold
t_max(j) = timing;
Max_state(j) = V(j,timing);
break;
end
end
if t_max(j) < T
V(j,t_max(j):end) = V(j,t_max(j)).*exp(-(Time(t_max(j):end)-Time(t_max(j)))/Tou_m);
end
end
[~,output_class] = min(t_max);
%% weight modify when error occurs
if train_target(samples) == 1
if output_class ~= 1
for j = 1:3
if j == 1 %% error in target neuron
if Max_state(j) < threshold %% if P+ error occurs
for i = 1:neurons
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(j)
%% weight modified
weight(i,j) = weight(i,j) + ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
elseif j ~= 1 %% error on other 2 output neurons
if Max_state(j) >= threshold %% if P- error occurs
for i = 1:neurons
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(j)
%% weight modified
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
elseif output_class == 1
for j = 1:3
if j ~= 1
if Max_state(j) >= threshold
for i = 1:neurons
if InputSpike(samples,i) < t_max(j)
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
end
%% for neurons that fired but weaker than target neuron
elseif train_target(samples) ~= 1
if Max_state(1) >= threshold
for i = 1:neurons %% P- error occurs
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(1)
%% weight modified
weight(i,1) = weight(i,1) - ...
lr*K(t_max(1),InputSpike(samples,i));
end
end
end
end
end
% Train for class 2
for samples = 1:size(InputSpike,1) %% Training samples for each iteration
t_max = 100*ones(1,3); %% default: all neuron reach max V at the end
Max_state = zeros(1,3); % strongest state of each output neuron
% In each iteration, T = 100ms
for t = 1:T
for neuron = 1:neurons %% Response function for 48 neurons at time t
Response(neuron) = K(t,InputSpike(samples,neuron));
end
% Calculate PSP
for j = 1:3
V(j,t) = Response*weight(:,j) + V_rest;
end
end
%% find t_max: first index that V cross threshold
for j = 1:3
for timing = 1:T
if V(j,timing) >= threshold
t_max(j) = timing;
Max_state(j) = V(j,timing);
break;
end
end
if t_max(j) < T
V(j,t_max(j):end) = V(j,t_max(j)).*exp(-(Time(t_max(j):end)-Time(t_max(j)))/Tou_m);
end
end
[~,output_class] = min(t_max);
%% weight modify when error occurs
if train_target(samples) == 2
if output_class ~= 2 % [1.5 1 0] or [1 0 1]...
for j = 1:3
if j == 2 %% error in target neuron
if Max_state(j) < threshold %% if P+ error occurs
for i = 1:neurons
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(j)
%% weight modified
weight(i,j) = weight(i,j) + ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
elseif j ~= 2 %% error on other 2 output neurons
if Max_state(j) >= threshold %% if P- error occurs
for i = 1:neurons
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(j)
%% weight modified
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
elseif output_class == 2 %% [1 1.5 1]
for j = 1:3
if j ~= 2
if Max_state(j) >= threshold
for i = 1:neurons
if InputSpike(samples,i) < t_max(j)
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
end
%% for neurons that fired but weaker than target neuron
elseif train_target(samples) ~= 2
if Max_state(2) >= threshold
for i = 1:neurons %% P- error occurs
%% for all t_i < t_max
if InputSpike(samples,i) < t_max(2)
%% weight modified
weight(i,2) = weight(i,2) - ...
lr*K(t_max(2),InputSpike(samples,i));
end
end
end
end
end
% Train for class 3
for samples = 1:size(InputSpike,1) %% Training samples for each iteration
t_max = 100*ones(1,3); %% default: all neuron reach max V at the end
Max_state = zeros(1,3); % strongest state of each output neuron
% In each iteration, T = 100ms
for t = 1:T
for neuron = 1:neurons %% Response function for 48 neurons at time t
Response(neuron) = K(t,InputSpike(samples,neuron));
end
% Calculate PSP
for j = 1:3
V(j,t) = Response*weight(:,j) + V_rest;
end
end
%% find t_max: first index that V cross threshold
for j = 1:3
for timing = 1:T
if V(j,timing) >= threshold
t_max(j) = timing;
Max_state(j) = V(j,timing);
break;
end
end
if t_max(j) < T
V(j,t_max(j):end) = V(j,t_max(j)).*exp(-(Time(t_max(j):end)-Time(t_max(j)))/Tou_m);
end
end
[~,output_class] = min(t_max);
%% weight modify when error occurs
if train_target(samples) == 3
if output_class ~= 3
for j = 1:3
if j == 3 %% error in target neuron
if Max_state(j) < threshold %% if P+ error occurs
for i = 1:neurons
if InputSpike(samples,i) < t_max(j)
weight(i,j) = weight(i,j) + ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
elseif j ~= 3
if Max_state(j) >= threshold
for i = 1:neurons
if InputSpike(samples,i) < t_max(j)
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
elseif output_class == 3
for j = 1:3
if j ~= 3
if Max_state(j) >= threshold
for i = 1:neurons
if InputSpike(samples,i) < t_max(j)
weight(i,j) = weight(i,j) - ...
lr*K(t_max(j),InputSpike(samples,i));
end
end
end
end
end
end
elseif train_target(samples) ~= 3
if Max_state(3) >= threshold
for i = 1:neurons
if InputSpike(samples,i) < t_max(3)
weight(i,3) = weight(i,3) - ...
lr*K(t_max(3),InputSpike(samples,i));
end
end
end
end
end
end
figure(3)
plot(correct_rate)
xlabel('Iteration')
ylabel('Correct rate')
train_target(size(InputSpike,1))
time = 0:1:100;
figure;
subplot(3,1,1)
hold on
plot(V(1,:))
plot(time,threshold.*ones(length(time)),'k-.')
axis([0 100 0 1.5])
xlabel('t')
ylabel('V_1')
title('PSP Potential')
hold off
subplot(3,1,2)
hold on
plot(V(2,:))
plot(time,threshold.*ones(length(time)),'k-.')
axis([0 100 0 1.5])
xlabel('t')
ylabel('V_2')
hold off
subplot(3,1,3)
hold on
plot(V(3,:))
plot(time,threshold.*ones(length(time)),'k-.')
axis([0 100 0 1.5])
xlabel('t')
ylabel('V_3')
hold off
function response = K(t,t_i)
time = 0:1:100;
Tou_m = 15;
Tou_s = Tou_m/4;
V_0 = 1/( max(exp(-(time)/Tou_m) - exp(-(time)/Tou_s)) ); % normalize factor(divided by maximum)
response = V_0*( exp(-(t-t_i)/Tou_m) - exp(-(t-t_i)/Tou_s) )*heavyside(t,t_i);
% response K(t - t_i) for t >= t_i
end
function h = heavyside(t,t_i)
if (t-t_i) >= 0
h = 1;
elseif (t-t_i) < 0
h = 0;
end
end