-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBCI_owm_ill_inter.m
238 lines (198 loc) · 6.67 KB
/
BCI_owm_ill_inter.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
clear;
close all
linewidth = 3;
fontsize = 16;
fontweight = 'bold';
p = 0.3;
g = 1.5; % g greater than 1 leads to chaotic networks.
%% SECTION TITLE
% DESCRIPTIVE TEXT
alpha = 5;
train_nsecs =120*20;
test_nsecs =120*3;
dt = 0.1;
nRec2Out = 500;
nRec2Control = 100;
N = nRec2Out+nRec2Control;
for loop_rate=0
loop_rate
rand('seed',0);
Mc = sprandn(nRec2Control,nRec2Control,p);
M = sprandn(N,N,p)*g/sqrt(p*N);
M = full(M);
M_add=find(M~=0);
randn('seed',0);
M(M_add)=randn(1,length(M_add))*g/sqrt(p*N);
% rand('seed',0);
% Mo = sprandn(nRec2Out,nRec2Out,p);
% Mo = full(Mo);
% M_add=find(Mo~=0);
% randn('seed',0);
% Mo(M_add)=randn(1,length(M_add))*g/sqrt(p*nRec2Out);
%
% rand('seed',0);
% Mc = sprandn(nRec2Control,nRec2Control,p);
% Mc = full(Mc);
% M_add=find(Mc~=0);
% randn('seed',0);
% Mc(M_add)=randn(1,length(M_add))*g/sqrt(p*nRec2Control);
%
% M =blkdiag(Mo,Mc);
% disp([' nRec2Out: ', num2str(nRec2Out)]);
% disp([' nRec2Control: ', num2str(nRec2Control)]);
simtime_train = 0:dt:train_nsecs-dt;
simtime_train_len = length(simtime_train);
simtime_test=0:dt:test_nsecs-dt;
simtime_test_len = length(simtime_test);
load('Trajectory');
mydata=repmat(Trajectory_data,(train_nsecs+test_nsecs)/dt/size(Trajectory_data,1),1);
Num_trail=size(Trajectory_data,2);
ft_train = mydata(1:simtime_train_len ,1:Num_trail)' ;%3Xsimtime_len_train
ft_test = mydata(1+simtime_train_len :simtime_test_len+simtime_train_len,1:Num_trail)';%3Xsimtime_len_test
wo = zeros(nRec2Out,Num_trail);
rand('seed',0)
wo_f= 2*(rand(N,Num_trail)-0.5);
wo_len = zeros(Num_trail,simtime_train_len);
%wc = beta*randn(nRec2Control, 1)/sqrt(nRec2Control);
wc = zeros(nRec2Control, 1); % synaptic strengths from internal pool to control unit
%rand('seed',loop_rate)
wc_f = [rand(nRec2Out,1);zeros(nRec2Control,1)];% the feedback now comes from the control unit as opposed to the out
wc_len = zeros(1,simtime_train_len);
rand('seed',0)
x = 0.4*rand(N,1);
y = zeros(1,1);
z = zeros(Num_trail,1);
r = tanh(x);
zt = zeros(Num_trail,simtime_train_len);
% Deliberatley set the pre-synaptic neurons to nonoverlapping between the output and control units.
out_idxs = 1:nRec2Out;
con_idxs = nRec2Out+1:N;
rate_o = 0.3;
%figure;
flag = 0;
P = eye(nRec2Out);
Pc = eye(nRec2Control);
for ti = 1:length(simtime_train)
% if mod(ti, round(length(simtime_train)/2)) == 0
% % disp(['time: ' num2str(t,3) '.']);
% subplot 211;
% plot(simtime_train, ft_train(6,:), 'linewidth', linewidth, 'color', 'green');
% hold on;
% plot(simtime_train, zt(6,:), 'linewidth', linewidth, 'color', 'red');
% title('training', 'fontsize', fontsize, 'fontweight', fontweight);
% legend('f', 'z');
% xlabel('time', 'fontsize', fontsize, 'fontweight', fontweight);
% ylabel('f and z', 'fontsize', fontsize, 'fontweight', fontweight);
% hold off;
%
% subplot 212;
% plot(simtime_train, wo_len(6,:), 'linewidth', linewidth);
% hold on;
% plot(simtime_train, wc_len, 'linewidth', linewidth,'color','r');
% xlabel('time', 'fontsize', fontsize, 'fontweight', fontweight);
% ylabel('|w|,|wc|', 'fontsize', fontsize, 'fontweight', fontweight);
% legend('|w|','|wc|');
% hold off;
% pause(0.01);
% end
if flag == 0
x = (1.0-dt)*x + M*(r*dt) + (wo_f*(z*dt)+ wc_f*(y*dt)).*[ones(nRec2Out,1);zeros(nRec2Control,1)];
else
x = (1.0-dt)*x + M*(r*dt) + wo_f*(z*dt) + wc_f*(y*dt);
end
r = tanh(x);
rx = r(out_idxs); % the neurons that project to the output
ry = r(con_idxs); % the neurons that project to the control unit
z = wo'*rx;
y = wc'*ry*flag;
% update inverse correlation matrix
k = P*rx;
%rPrx = rx'*k;
c = 1.0/(alpha + rx'*k);
P = P - k*(k'*c);
% update the error for the linear readout
e =z - ft_train(:,ti);
% update the output weights
dwo = -k*e';
wo = wo + dwo*rate_o;
%%update inverse correlation matrix for the control unit
%NOTE WE USE THE OUTPUT'S ERROR %%% update the output weights
if ti> (1200*10)
M(1:nRec2Out,1:100)=0;
%break
rate_o = 0.003;
flag = 1;
rate_c = 0.017;
kc = Pc*ry;
%rPry = ry'*kc;
cy = 1.0/(7.4 + ry'*kc);
Pc = Pc - kc*(kc'*cy);
dwc = -kc*e';
wc = wc + mean(dwc,2)*rate_c*flag;
% %update the internal weight matrix using the output's error
% dM_t=[dwo;dwc]';
% M = M + repmat( sum(dM_t,1), N, 1);
end
% Store the output of the system.
zt(:,ti) = z;
wo_len(:,ti) = sqrt(sum(wo.^2,1))';
wc_len(ti) = sqrt(wc'*wc);
end
% error_avg_train = (mean( ((zt-ft_train)).^2,2)).^0.5;
% error_all_train = mean(error_avg_train)
% save state M x z y;
%
% %%
% %%%%test
% pred_data_function;
zpred_t = zeros(Num_trail,simtime_test_len);
for ti = 1:simtime_test_len % don't want to subtract time in indices
if flag == 0
x = (1.0-dt)*x + M*(r*dt) + (wo_f*(z*dt)+ wc_f*(y*dt)).*[ones(nRec2Out,1);zeros(nRec2Control,1)];
else
x = (1.0-dt)*x + M*(r*dt) + wo_f*(z*dt) + wc_f*(y*dt);
end
r = tanh(x);
rx = r(out_idxs); % the neurons that project to the output
ry = r(con_idxs); % the neurons that project to the control unit
z = wo'*rx;
y = wc'*ry;
zpred_t(:,ti) = z;
end
error_avg_test = (mean( ((zpred_t-ft_test)).^2,2)).^0.5;
error_all_test = mean(error_avg_test)
disp(['End testing... please wait.']);
end
%save pred_data_owm zpred_t;
%%
figure;
subplot 311;
hold on;
plot(simtime_test, ft_test(6,:), 'linewidth', linewidth, 'color', 'k');
axis tight;
plot(simtime_test, zpred_t(6,:), 'linewidth', linewidth, 'color', 'r');
axis tight;
title('Trajectory 1', 'fontsize', fontsize, 'fontweight', fontweight);
xlabel('time', 'fontsize', fontsize, 'fontweight', fontweight);
ylabel('f1 and z1', 'fontsize', fontsize, 'fontweight', fontweight);
legend('f1', 'z1');
subplot 312;
hold on;
plot(simtime_test, ft_test(7,:), 'linewidth', linewidth, 'color', 'k');
axis tight;
plot(simtime_test, zpred_t(7,:), 'linewidth', linewidth, 'color', 'r');
axis tight;
title('Trajectory 2', 'fontsize', fontsize, 'fontweight', fontweight);
xlabel('time', 'fontsize', fontsize, 'fontweight', fontweight);
ylabel('f2 and z2', 'fontsize', fontsize, 'fontweight', fontweight);
legend('f2', 'z2');
subplot 313;
hold on;
plot(simtime_test, ft_test(8,:), 'linewidth', linewidth, 'color', 'k');
axis tight;
plot(simtime_test, zpred_t(8,:), 'linewidth', linewidth, 'color', 'r');
axis tight;
title('Trajectory 3', 'fontsize', fontsize, 'fontweight', fontweight);
xlabel('time', 'fontsize', fontsize, 'fontweight', fontweight);
ylabel('f3 and z3', 'fontsize', fontsize, 'fontweight', fontweight);
legend('f3', 'z3');