-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLeaky_Integrate_and_Fire.m
183 lines (161 loc) · 5.23 KB
/
Leaky_Integrate_and_Fire.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
%% Test LIF model for pattern recognition on Iris datset
clc; clear all; clf;
%% data set
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);
%% take 120 for train data, 30 for test data
train = feature(1:120,:);
train_target = target(1:120,1);
test = feature(121:150,:);
test_target = target(121:150,1);
len = length(train);
%% indices of each class
c1 = find(train_target==1);
c2 = find(train_target==2);
c3 = find(train_target==3);
%% normalization
for i = 1:2
train(:,i) = (train(:,i)-mean(train(:,i)))/(max(train(:,i))-min(train(:,i)));
test(:,i) = (test(:,i)-mean(test(:,i)))/(max(test(:,i))-min(test(:,i)));
end
%% initial LIF parameter
threshold = 50;
restPotential = -60;
a = 0.5;
b = -0.001;
c = -50;
gain = 0.1;
weight = rand(4,1);
I = train*weight*gain; %% fixed encoded InputCurrent
T = 1000; %% firing interval
PSP = zeros(1,T);
actionPotential = zeros(1,T); %% spike train for a input current
PSP(1,1) = restPotential;
lastPSP = restPotential;
lastAction = 0;
fr = zeros(len,1);
for num = 1:len
for time = 1:T
PSP(1,time) = lastPSP + I(num) + a - b*lastPSP; %% accumulate current PSP with input current
if PSP(1,time) > threshold
lastPSP = c;
actionPotential(1,time) = 1;
elseif PSP(1,time) < threshold
lastPSP = PSP(1,time);
actionPotential(1,time) = 0;
end
lastAction = actionPotential(1,time);
end
spikes = find(actionPotential==1);
spike_num = length(spikes);
firing_rate = spike_num/T;
fr(num) = firing_rate; %% firing rate array for training data
end
%% average firing rate of each class
AFR1 = mean(fr(c1));
AFR2 = mean(fr(c2));
AFR3 = mean(fr(c3));
%% standard deviation of firing rate of each class
SDFR1 = sqrt(sum((fr(c1)-AFR1).^2)/length(c1));
SDFR2 = sqrt(sum((fr(c2)-AFR2).^2)/length(c2));
SDFR3 = sqrt(sum((fr(c3)-AFR3).^2)/length(c3));
%% train synapse weight
%% differential evolution algorithm
%% DE pamameter
MAXGEN = 1000; %% max generation; the maximum number of childs generated
F = 0.8; %%control vector
XMAX = 1; %% max value of these parameters
XMIN = 0; %% min value of these parameters
CR = 0.9; %% hybrid control parameter
NP = 40; %% populations per cycle(generate NP solutions at first)
% initialize population for first generation
gene = rand(NP,4);
best = zeros(1,4);
for iteration = 1:(MAXGEN/NP)
for i = 1:NP
target_vector = gene(i,:); %% initial target vector is the first array
index = randperm(NP);
index(find(index==i)) = []; % r1!=r2!=r3!=i
r = index(1:3); % r = [r1,r2,r3]
% mutation (the mutation output should be constrainted in [XMIN,XMAX])
h = gene(r(1),:) + F*(gene(r(2),:)-gene(r(3),:));
for k = 1:size(h,2)
if h(k) > XMAX
h(k) = XMIN + (XMAX-XMIN)*rand;
elseif h(k) < XMIN
h(k) = XMIN + (XMAX-XMIN)*rand;
end
end
% crossover
v = zeros(size(h));
for j = 1:size(v,2)
param = XMIN + (XMAX-XMIN)*rand;
if param <= CR
v(j) = h(j);
else
v(j) = target_vector(j);
end
end
%selection
if fitness(train,c1,c2,c3,v) < fitness(train,c1,c2,c3,target_vector) %if new weight obtain a better fitness function
gene(i,:) = v; %% update new generation
else
gene(i,:) = target_vector;
end
if fitness(train,c1,c2,c3,gene(i,:)) < fitness(train,c1,c2,c3,best) %% check if the current solution is better, update best solution
best = gene(i,:);
end
fitness(train,c1,c2,c3,best)
end
end
figure(1);
hold on
weight = best
plot(test_target,'bo')
%% test
I = test*weight'*gain;
class = zeros(size(test_target));
PSP = zeros(1,T);
actionPotential = zeros(1,T); %% spike train for a input current
PSP(1,1) = restPotential;
lastPSP = restPotential;
lastAction = 0;
fr = zeros(len,1);
%% test data
for num = 1:length(test)
for time = 1:T
PSP(1,time) = lastPSP + I(num) + a - b*lastPSP; %% accumulate current PSP with input current
if PSP(1,time) > threshold
lastPSP = c;
actionPotential(1,time) = 1;
elseif PSP(1,time) < threshold
lastPSP = PSP(1,time);
actionPotential(1,time) = 0;
end
lastAction = actionPotential(1,time);
end
spikes = find(actionPotential==1);
spike_num = length(spikes);
firing_rate = spike_num/T;
fr(num) = firing_rate; %% Elements in I corresponds to fr
d = zeros(1,3);
d(1) = abs(AFR1-fr(num));
d(2) = abs(AFR2-fr(num));
d(3) = abs(AFR3-fr(num));
class(num) = find(d == min(d));
end
count = 0;
for i = 1:length(test)
if test_target(i) == class(i)
count = count + 1;
end
end
error = 1 - count/length(test);
plot(class,'rx')
title(['Error rate=',num2str(error)]);
figure(5);
plot(PSP,'b');
hold off