forked from emackev/HVCModelCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoutOnsetDifferentiation_movie.m
345 lines (327 loc) · 14.1 KB
/
BoutOnsetDifferentiation_movie.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
% Emily Mackevicius 1/14/2015, heavily copied from Hannah Payne's code
% which builds off Ila Fiete's model, with help from Michale Fee and Tatsuo
% Okubo.
% Alternating seed neuron differentiation
clear all;
figure(1); clf
set(gcf, 'color', ones(1,3), 'units', 'inches', 'position', [.1 1 4 3]);
highQual = 0;
PlottingParams.msize = 20;
PlottingParams.linewidth = .5;
PlottingParams.labelFontSize = 12;
PlottingParams.SeedColor = [.95 .5 1];
PlottingParams.Syl1Color = [0 0 1];
PlottingParams.Syl2Color = [1 0 0];
PlottingParams.Syl1BarColor = [0 0 1];
PlottingParams.Syl2BarColor = [1 0 0];
PlottingParams.numFontSize = 5;
PlottingParams.wplotmin = 0;
PlottingParams.wplotmax = 2; % this should be wmaxSplit
PlottingParams.wprctile = 0; % plot all weights above this percentile.
PlottingParams.totalPanels = 4;
PlottingParams.thisPanel = 1;
PlottingParams.sortby = 'weightMatrix';
rng('default')
%% Network parameters
% fixed parameters
seed = 1009;
p.seed = seed; % seed random number generator
p.n = 100; % n neurons
p.trainint = 10; % Time interval between inputs
p.nsteps = 500; % time-steps to simulate -- each time-step is 1 burst duration.
p.pin = .01; % probability of external stimulation of at least one neuron at any time
k = 10; % number of training neurons
p.trainingInd = 1:k; % index of training neurons
p.beta = .13; % strength of feedforward inhibition
p.alpha = 30; % strength of neural adaptation
p.eta = .05; % learning rate parameter
p.epsilon = .14; % relative strength of heterosynaptic LTD
p.tau = 4; % time constant of adaptation
gammaStart = .01; % strength of recurrent inhibition
gammaSplit = .04; % increased strength of recurrent inhibition to induce splitting
wmaxStart = 1; % single synapse hard bound
wmaxSplit = 2; % single synapse hard bound to induce splitting (increased to encourage fewer stronger synapses)
mStart = 5; % desired number of synapses per neuron (wmax = Wmax/m)
Wmax = mStart*wmaxStart;% soft bound for weights of each neuron
mSplit = Wmax/wmaxSplit;% keep Wmax constant, change m & wmax to induce fewer stronger synapses
HowClamped = 10; % give training neurons higher threshold
HowOn = 10; % higher inputs to bout onset training neurons
HowOnPsylStart = HowOn; % inputs to protosyllable training neurons
HowOnPsylSplit = 1; % decrease input to protosyllable training neurons during splitting
% how many iterations to run before plotting
nIterEarly = 5; % early protosyllable stage
nIterProto = 100; % end of protosyllable stage
nIterPlotSplit1 = 30; % number of splitting iterations before plotting intermediate splitting phase
nIterPlotSplit2 = 300; % total number of splitting iterations
% parameters that change over development
protosyllableStage = [true(1,nIterProto) false(1,nIterPlotSplit2)];
splittingStage = [false(1,nIterProto) true(1,nIterPlotSplit2)];
gammas(protosyllableStage) = gammaStart;
gammas(splittingStage) = gammaSplit * sigmf(1:nIterPlotSplit2,[1/200 250]);
wmaxs(protosyllableStage) = wmaxStart;
wmaxs(splittingStage) = wmaxSplit;
ms(protosyllableStage) = mStart;
ms(splittingStage) = mSplit;
HowOnPsyl(protosyllableStage) = HowOnPsylStart;
HowOnPsyl(splittingStage) = HowOnPsylSplit;
% params for training inputs
CyclesPerBout = 5;
bOnOffset = 3;
% training neurons for plotting
trainingNeurons{1}.nIDs = 1:k/2;
trainingNeurons{2}.nIDs = (k/2+1):k;
trainingNeurons{1}.candLat = (-bOnOffset+1):p.trainint;
trainingNeurons{2}.candLat = 1:p.trainint;
buffSize = 9;
trainingNeurons{1}.thres = 4;
trainingNeurons{2}.thres = 6;
% set up to record movie
folder = fileparts(mfilename('fullpath'));% 'C:\Users\emackev\Documents\MATLAB\code\misc_elm\HVCmodel\NetworkMovies';
timestamp = datestr(now, 'mmm-dd-yyyy-HH-MM-SS');
filename = ['NetLearnsSeed' num2str(seed) timestamp];
%aviobj = avifile(fullfile(folder, filename), 'compression', 'none', 'fps',
%20); -- old matlab version
obj = vision.VideoFileWriter(fullfile(folder, [filename, '.mp4']));
obj.FrameRate = 20;
obj.FileFormat='MPEG4';
%% run simulation
% random initial weights
rng(seed);
w = 2*rand(p.n)*Wmax/p.n;
bOnOffsetVar = [1 randperm(20)]; % variable inter-bout-interval
wOld = w;
tind1Old = [];
tind2Old = [];
xdynOld = zeros(p.n,0);
% learning stages
for t = 1:(nIterProto+nIterPlotSplit2)
p.w = w;
% set parameters that change over development
p.gamma = gammas(t);
p.wmax = wmaxs(t);
p.m = ms(t);
% Construct input
Input = -HowClamped*ones(k, p.nsteps); % clamp training neurons
bOnOffsetVar = [1 randperm(20)]; % variable inter-bout-interval
indPsyl = []; indBstart = []; indOff = []; prevPsylEnd = 1; % initializing
for i = 1:(p.nsteps/CyclesPerBout/p.trainint)
istart = (i-1)*CyclesPerBout*p.trainint+1+bOnOffsetVar(i)+bOnOffset;
indBstart = [indBstart istart-bOnOffset]; % bout onset times
indPsyl = [indPsyl istart istart+p.trainint istart+2*p.trainint]; % 3 protosyllables per bout
indOff = [indOff prevPsylEnd:(istart-bOnOffset-1)]; % will clamp all neurons between bouts
prevPsylEnd = istart+3*p.trainint; % keep track of when bout ends, to clamp neurons between bouts
end
indPsyl = indPsyl(indPsyl<=p.nsteps);
indBstart = indBstart(indBstart<=p.nsteps);
Input(1:k/2,indBstart) = HowOn; % input to bout onset neurons
Input((k/2+1):k,indPsyl) = HowOnPsyl(t); % input to protosyllable neurons
bdyn = double(rand(p.n,p.nsteps)>=(1-p.pin)); % Random activation
bdyn(1:k,:) = Input;
bdyn(:,indOff) = -HowClamped; % clamp all neurons between bouts
p.input = bdyn;
% run one iteration
[w xdyn] = HVCIter(p);
dw(t) = norm(w(:) - wOld(:));
wOld = w;
% save certain iterations for plotting later
if dw(t)>1 | t == (nIterProto+nIterPlotSplit2)
trainingNeuronsEarly = trainingNeurons;
trainingNeuronsEarly{1}.tind = [indBstart+bOnOffset tind1Old];
trainingNeuronsEarly{2}.tind = [setdiff(indPsyl, indBstart+bOnOffset) tind2Old];
trainingNeuronsEarly{1}.thres = .5*length(trainingNeuronsEarly{1}.tind);
trainingNeuronsEarly{2}.thres = .5*length(trainingNeuronsEarly{2}.tind);
if t < buffSize
slowrate = 10;
trainingNeuronsEarly{1}.thres = trainingNeurons{1}.thres;
trainingNeuronsEarly{2}.thres = trainingNeurons{2}.thres;
elseif protosyllableStage(i)
slowrate = 1;
else
slowrate = 2;
end
trainingNeuronsEarly = trainingNeurons;
trainingNeuronsEarly{1}.tind = [indBstart+bOnOffset tind1Old];
trainingNeuronsEarly{2}.tind = [setdiff(indPsyl, indBstart+bOnOffset) tind2Old];
trainingNeuronsEarly{1}.thres = .5*length(trainingNeuronsEarly{1}.tind);
trainingNeuronsEarly{2}.thres = .5*length(trainingNeuronsEarly{2}.tind);
figure(1); clf
set(gcf, 'color', ones(1,3), 'units', 'inches', 'position', [.1 1 4 3]);
netForMovie_boutOnset(w, [xdyn xdynOld], trainingNeuronsEarly, PlottingParams)
if protosyllableStage(t)
text(3.5,9,['Protosyllable stage: iteration ', num2str(t)], 'fontsize', PlottingParams.labelFontSize, 'color', [0 0 0], 'horizontalalignment', 'center', 'verticalalignment', 'bottom')%, 'fontweight', 'bold')
else
text(3.5,9,['Splitting stage: iteration ', num2str(t-nIterProto)], 'fontsize', PlottingParams.labelFontSize, 'color', [1 0 0], 'horizontalalignment', 'center', 'verticalalignment', 'bottom')%, 'fontweight', 'bold')
end
if highQual
s = rng;
myaa % anti-aliasing, from matlab file exchange
rng(s);
end
F = getframe(gcf)
for l = 1:slowrate
step(obj, F.cdata);
%aviobj = addframe(aviobj,F); -- old matlab version
end
close all
end
buffersize = min(buffSize*p.nsteps,size(xdynOld,2)+p.nsteps);
xdynOld = [xdyn xdynOld];
xdynOld = xdynOld(:,1:buffersize);
tind1Old = [indBstart+bOnOffset tind1Old]+p.nsteps;
tind1Old = tind1Old(tind1Old<=(p.nsteps+buffersize));
tind2Old = [setdiff(indPsyl, indBstart+bOnOffset) tind2Old]+p.nsteps;
tind2Old = tind2Old(tind2Old<=(p.nsteps+buffersize));
%
% if t<4
% wEarly = w;
% xdynEarly = xdyn;
% trainingNeuronsEarly = trainingNeurons;
% trainingNeuronsEarly{1}.tind = indBstart+bOnOffset;
% trainingNeuronsEarly{2}.tind = setdiff(indPsyl, indBstart+bOnOffset);
% slowrate = 20;
% figure
% set(gcf, 'color', ones(1,3));
% netForMovie_boutOnset(w, xdyn, trainingNeuronsEarly, PlottingParams)
% title(['Protosyllable stage: iteration ', num2str(t)], 'fontsize', PlottingParams.labelFontSize)
% if highQual
% s = rng;
% myaa % anti-aliasing
% rng(s);
% end
% F = getframe(gcf)
% for l = 1:slowrate
% aviobj = addframe(aviobj,F);
% end
% close all
% elseif t<nIterProto
% wProto = w;
% xdynProto = xdyn;
% trainingNeuronsProto = trainingNeurons;
% trainingNeuronsProto{1}.tind = indBstart+bOnOffset;
% trainingNeuronsProto{2}.tind = setdiff(indPsyl, indBstart+bOnOffset);
% speedrate = 2;
% if mod(t,speedrate) == 0
% figure
% set(gcf, 'color', ones(1,3));
% netForMovie_boutOnset(w, xdyn, trainingNeuronsProto, PlottingParams)
% title(['Protosyllable stage: iteration ', num2str(t)], 'fontsize', PlottingParams.labelFontSize)
% if highQual
% s = rng;
% myaa % anti-aliasing
% rng(s);
% end
% F = getframe(gcf)
% aviobj = addframe(aviobj,F);
% close all
% end
% elseif t < 140
% wSplit1 = w;
% xdynSplit1 = xdyn;
% trainingNeuronsSplit1 = trainingNeurons;
% trainingNeuronsSplit1{1}.tind = indBstart+bOnOffset;
% trainingNeuronsSplit1{2}.tind = setdiff(indPsyl, indBstart+bOnOffset);
% slowrate = 5;
% figure
% set(gcf, 'color', ones(1,3));
% netForMovie_boutOnset(w, xdyn, trainingNeuronsSplit1, PlottingParams)
% title(['Splitting stage: iteration ', num2str(t-nIterProto)], 'fontsize', PlottingParams.labelFontSize)
% if highQual
% s = rng;
% myaa % anti-aliasing
% rng(s);
% end
% F = getframe(gcf)
% for l = 1:slowrate
% aviobj = addframe(aviobj,F);
% end
% close all
% else
% wSplit2 = w;
% xdynSplit2 = xdyn;
% trainingNeuronsSplit2 = trainingNeurons;
% trainingNeuronsSplit2{1}.tind = indBstart+bOnOffset;
% trainingNeuronsSplit2{2}.tind = setdiff(indPsyl, indBstart+bOnOffset);
% netForMovie_boutOnset(w, xdyn, trainingNeuronsSplit2, PlottingParams)
% speedrate = 5;
% if mod(t,speedrate) == 0
% figure
% set(gcf, 'color', ones(1,3));
% netForMovie_boutOnset(w, xdyn, trainingNeuronsSplit2, PlottingParams)
% title(['Splitting stage: iteration ', num2str(t-nIterProto)], 'fontsize', PlottingParams.labelFontSize)
% if highQual
% s = rng;
% myaa % anti-aliasing
% rng(s);
% end
% F = getframe(gcf)
% aviobj = addframe(aviobj,F);
% close all
% end
% end
end
slowrate = 20;
for l = 1:slowrate
step(obj, F.cdata);
%aviobj = addframe(aviobj,F); -- old matlab version
end
release(obj)
% %% plotting early network activity
%
% trainingNeuronsEarly{1}.nIDs = 1:k/2;
% trainingNeuronsEarly{2}.nIDs = (k/2+1):k;
% trainingNeuronsEarly{1}.candLat = (-bOnOffset+1):p.trainint;
% trainingNeuronsEarly{2}.candLat = 1:p.trainint;
% trainingNeuronsEarly{1}.thres = 4;
% trainingNeuronsEarly{2}.thres = 6;
%
% PlottingParams.thisPanel = 1;
% PlottingParams.Hor = 0;
% plotHVCnet_boutOnset(wEarly, xdynEarly, trainingNeuronsEarly, PlottingParams)
% PlottingParams.Hor = 1;
%
% %% plotting protosyllable
%
% trainingNeuronsProto{1}.nIDs = 1:k/2;
% trainingNeuronsProto{2}.nIDs = (k/2+1):k;
% trainingNeuronsProto{1}.candLat = (-bOnOffset+1):p.trainint;
% trainingNeuronsProto{2}.candLat = 1:p.trainint;
% trainingNeuronsProto{1}.thres = 4;
% trainingNeuronsProto{2}.thres = 6;
%
% PlottingParams.thisPanel = 2;
% plotHVCnet_boutOnset(wProto, xdynProto, trainingNeuronsProto, PlottingParams)
%
% %% plotting splitting stages
%
% trainingNeuronsSplit1{1}.nIDs = 1:k/2;
% trainingNeuronsSplit1{2}.nIDs = (k/2+1):k;
% trainingNeuronsSplit1{1}.candLat = (-bOnOffset+1):p.trainint;
% trainingNeuronsSplit1{2}.candLat = 1:p.trainint;
% trainingNeuronsSplit1{1}.thres = 4;
% trainingNeuronsSplit1{2}.thres = 6;
%
% PlottingParams.thisPanel = 3;
% plotHVCnet_boutOnset(wSplit1, xdynSplit1, trainingNeuronsSplit1, PlottingParams)
%
% trainingNeuronsSplit2{1}.nIDs = 1:k/2;
% trainingNeuronsSplit2{2}.nIDs = (k/2+1):k;
% trainingNeuronsSplit2{1}.candLat = (-bOnOffset+1):p.trainint;
% trainingNeuronsSplit2{2}.candLat = 1:p.trainint;
% trainingNeuronsSplit2{1}.thres = 4;
% trainingNeuronsSplit2{2}.thres = 6;
%
% PlottingParams.thisPanel = 4;
% plotHVCnet_boutOnset(wSplit2, xdynSplit2, trainingNeuronsSplit2, PlottingParams)
%
%
% % figure parameters
% if isEPS
% cd('Z:\Fee_lab\Papers\HVC_differentiation\Figures\EPS_files');
% export_fig(1,'Fig5j.eps','-transparent','-eps','-painters');
% else
% figw = 6;
% figh = 4;
% set(gcf, 'color', [1 1 1],'papersize', [figw figh], 'paperposition', [0 0 figw*.9 figh])
% suptitle(['seed ', num2str(seed)])
% print -dmeta -r150
% end