-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.C
1148 lines (978 loc) · 33.8 KB
/
neuron.C
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "def.h"
#include "synapse.h"
#include "neuron.h"
#include "network.h"
#include "speech.h"
#include "channel.h"
#include "util.h"
#include <stdlib.h>
#include <iostream>
#include <utility>
#include <string>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <utility>
#include <algorithm>
#include <climits>
#include <cmath>
// NOTE: The time constants have been changed to 2*original settings
// optimized performance for letter recognition.
using namespace std;
extern int Current;
extern int Threshold;
/** unit defined for digital system but not used in continuous model **/
extern const int one = 1;
// ONLY FOR RESERVOIR
Neuron::Neuron(char * name, bool excitatory, Network * network):
_mode(NORMAL),
_excitatory(excitatory),
_EP_max(INT_MIN), _EP_min(INT_MAX), _EN_max(INT_MIN), _EN_min(INT_MAX),
_IP_max(INT_MIN), _IP_min(INT_MAX), _IN_max(INT_MIN), _IN_min(INT_MAX),
_prev_delta_ep(0), _prev_delta_en(0), _prev_delta_ip(0), _prev_delta_in(0),
_curr_delta_ep(0), _curr_delta_en(0), _curr_delta_ip(0), _curr_delta_in(0),
_pre_fire_max(0),
_lsm_v_mem(0),
_lsm_v_mem_pre(0),
_lsm_calcium(0),
_lsm_calcium_pre(0),
_lsm_state_EP(0),
_lsm_state_EN(0),
_lsm_state_IP(0),
_lsm_state_IN(0),
_lsm_tau_EP(4.0*2),
_lsm_tau_EN(LSM_T_SYNE*2),
_lsm_tau_IP(4.0*2),
_lsm_tau_IN(LSM_T_SYNI*2),
_lsm_tau_FO(LSM_T_FO),
_lsm_v_thresh(LSM_V_THRESH),
_lsm_ref(0),
_lsm_R(LSM_T_M_C),
_lsm_channel(NULL),
_lsm_input(0),
_lsm_t_m_c(LSM_T_M_C),
_inputsyn_sq_sum(-1),
_t_next_spike(-1),
_network(network),
_teacherSignal(0),
_ind(-1)
{
_name = new char[strlen(name)+2];
strcpy(_name,name);
_indexInGroup = -1;
_del = false;
_f_count = 0;
_fired = false;
_error = 0;
_SpiKL_ip = false;
_fire_start=false;
}
//FOR INPUT AND OUTPUT
Neuron::Neuron(char * name, bool excitatory, Network * network, double v_mem):
_mode(NORMAL),
_excitatory(excitatory),
_EP_max(INT_MIN), _EP_min(INT_MAX), _EN_max(INT_MIN), _EN_min(INT_MAX),
_IP_max(INT_MIN), _IP_min(INT_MAX), _IN_max(INT_MIN), _IN_min(INT_MAX),
_prev_delta_ep(0), _prev_delta_en(0), _prev_delta_ip(0), _prev_delta_in(0),
_curr_delta_ep(0), _curr_delta_en(0), _curr_delta_ip(0), _curr_delta_in(0),
_pre_fire_max(0),
_lsm_v_mem(0),
_lsm_v_mem_pre(0),
_lsm_calcium(0),
_lsm_calcium_pre(0),
_lsm_state_EP(0),
_lsm_state_EN(0),
_lsm_state_IP(0),
_lsm_state_IN(0),
_lsm_tau_EP(4.0*2),
_lsm_tau_EN(LSM_T_SYNE*2),
_lsm_tau_IP(4.0*2),
_lsm_tau_IN(LSM_T_SYNI*2),
_lsm_tau_FO(LSM_T_FO),
_lsm_v_thresh(v_mem),
_lsm_input(0),
_lsm_ref(0),
_lsm_R(LSM_T_M_C),
_lsm_t_m_c(LSM_T_M_C),
_lsm_channel(NULL),
_inputsyn_sq_sum(-1),
_t_next_spike(-1),
_network(network),
_teacherSignal(-1),
_ind(-1)
{
_name = new char[strlen(name)+2];
strcpy(_name,name);
_indexInGroup = -1;
_del = false;
_f_count = 0;
_fired = false;
_error = 0;
_SpiKL_ip = false;
_fire_start=false;
}
Neuron::~Neuron(){
if(_name != NULL) delete [] _name;
}
char * Neuron::Name(){
return _name;
}
void Neuron::AddPostSyn(Synapse * postSyn){
_outputSyns.push_back(postSyn);
}
void Neuron::AddPreSyn(Synapse * preSyn){
_inputSyns.push_back(preSyn);
}
void Neuron::LSMPrintInputSyns(ofstream& f_out){
for(Synapse * synapse : _inputSyns){
f_out<<synapse->PreNeuron()->Name()<<"\t"<<synapse->PostNeuron()->Name()<<"\t"<<synapse->Weight()<<endl;
}
}
inline void Neuron::SetVth(double vth){
_lsm_v_thresh = vth;
}
inline void Neuron::SetTeacherSignal(int signal){
assert((signal >= -1) && (signal <= 1));
_teacherSignal = signal;
}
inline void Neuron::PrintTeacherSignal(){
cout<<"Teacher signal of "<<_name<<": "<<_teacherSignal<<endl;
}
inline void Neuron::PrintMembranePotential(){
cout<<"Membrane potential of "<<_name<<": "<<_lsm_v_mem<<endl;
}
void Neuron::LSMClear(){
_f_count = 0;
_fired = false;
_error = 0;
_vmems.clear();
_calcium_stamp.clear();
_fire_timings.clear();
_prev_delta_ep = 0, _prev_delta_en = 0, _prev_delta_ip = 0, _prev_delta_in = 0;
_curr_delta_ep = 0, _curr_delta_en = 0, _curr_delta_ip = 0, _curr_delta_in = 0;
_lsm_ref = 0;
_lsm_v_mem = 0;
_lsm_v_mem_pre = 0;
_lsm_calcium = 0; // both of calicum might be LSM_CAL_MID-3;
_lsm_calcium_pre = 0;
_lsm_state_EP = 0;
_lsm_state_EN = 0;
_lsm_state_IP = 0;
_lsm_state_IN = 0;
_t_next_spike = -1;
_teacherSignal = 0;
_inputsyn_sq_sum = -1;
_fire_start=false;
_lsm_R=LSM_T_M_C;
_lsm_input=0;
_lsm_t_m_c=LSM_T_M_C;
}
void Neuron::LSMClearIP(){
_f_count = 0;
_fired = false;
_error = 0;
_vmems.clear();
_calcium_stamp.clear();
_fire_timings.clear();
_prev_delta_ep = 0, _prev_delta_en = 0, _prev_delta_ip = 0, _prev_delta_in = 0;
_curr_delta_ep = 0, _curr_delta_en = 0, _curr_delta_ip = 0, _curr_delta_in = 0;
_lsm_ref = 0;
_lsm_v_mem = 0;
_lsm_v_mem_pre = 0;
_lsm_calcium = 0; // both of calicum might be LSM_CAL_MID-3;
_lsm_calcium_pre = 0;
_lsm_state_EP = 0;
_lsm_state_EN = 0;
_lsm_state_IP = 0;
_lsm_state_IN = 0;
_t_next_spike = -1;
_teacherSignal = 0;
_inputsyn_sq_sum = -1;
_fire_start=false;
_lsm_input=0;
}
//* function wrapper for ExpDecay under continuous case
//****** IMPORTANT: According to my experiment, the leaking terms seem to
//****** too large for v_mem. And you need to reduce the leaking
//****** so that good performance is obtained under continuous case.
//****** But without leaking term, the continuous case will not work!
inline void Neuron::ExpDecay(double & var, const int time_c){
var -= var/time_c;
#ifdef DIGITAL
assert(0);
#endif
}
inline void Neuron::ExpDecay(double & var, double time_c){
var -= var/time_c;
#ifdef DIGITAL
assert(0);
#endif
}
/** collect the synaptic response and accumulate them **/
inline void Neuron::AccumulateSynapticResponse(const int pos, double value){
if(pos > 0){
_lsm_state_EP += value;
_lsm_state_EN += value;
}
else{
_lsm_state_IP += value;
_lsm_state_IN += value;
}
}
/** Calculate the whole response together **/
inline double Neuron::NOrderSynapticResponse(){
return (_lsm_state_EP-_lsm_state_EN)/(_lsm_tau_EP-_lsm_tau_EN)+(_lsm_state_IP-_lsm_state_IN)/(_lsm_tau_IP-_lsm_tau_IN);
}
/**********************************************************************************
* This is a function to handle the firing actvities with regard to postsynaptic-neurons
* @para1: is used as a neuron that only output spikes to drive the network
* both input and reservoir neurons can be in this case;
* @para2: simulation time; @para3: in supervised training or not
**********************************************************************************/
inline void Neuron::HandleFiringActivity(bool isInput, int time, bool train){
for(Synapse * synapse : _outputSyns){
// need to get rid of the deactivated neuron !
if(synapse->PostNeuron()->LSMCheckNeuronMode(DEACTIVATED) == true) continue;
// need to get rid of the deactivated synapse !
if(synapse->DisableStatus()) continue;
synapse->LSMDeliverSpike();
if(isInput){
if(_name[0] != 'i'&&_mode == READCHANNEL){
if(synapse->IsReadoutSyn())
synapse->LSMActivate(_network, true, train);
}
}
}
}
//* update the previous delta effect with current delta effect
void Neuron::UpdateDeltaEffect(){
if(_mode == DEACTIVATED || _mode == READCHANNEL)
return;
_prev_delta_ep = _curr_delta_ep;
_prev_delta_en = _curr_delta_en;
_prev_delta_ip = _curr_delta_ip;
_prev_delta_in = _curr_delta_in;
_curr_delta_ep = 0;
_curr_delta_en = 0;
_curr_delta_ip = 0;
_curr_delta_in = 0;
}
void Neuron::LSMNextTimeStep(int t, FILE * Foutp, bool train, int end_time){
if(_mode == DEACTIVATED) return;
if(_mode == READCHANNEL ){
if(_mode != READCHANNEL){
// if training the input-reservoir/reservoir-readout synapses, need to keep track the cal!
_lsm_v_mem_pre = _lsm_v_mem;
_lsm_calcium_pre = _lsm_calcium;
_lsm_calcium -= _lsm_calcium/TAU_C;
}
if(_t_next_spike == -1) return;
if(t < _t_next_spike) return;
if(_mode != READCHANNEL){
_lsm_calcium += one;
}
/** Hand the firing behavior here for both input neurons and reservoir neurons */
/** @param1: is the neuron only used to output spike? **/
HandleFiringActivity(true, t, train);
_fired = true;
_t_next_spike = _lsm_channel->NextSpikeT();
if(_t_next_spike == -1) {
_network->LSMChannelDecrement(_lsm_channel->Mode());
}
return;
}
// the following code is to simulate the mode of NORMAL, WRITECHANNEL, and STDP
_lsm_v_mem_pre = _lsm_v_mem;
_lsm_calcium_pre = _lsm_calcium;
_lsm_calcium -= _lsm_calcium/TAU_C;
if((_teacherSignal==1)&&(_lsm_calcium < LSM_CAL_MID+1)){
_lsm_v_mem += 20;
}else if((_teacherSignal==-1)&&(_lsm_calcium > LSM_CAL_MID-1)){
_lsm_v_mem -= 15*0.75;
}
list<Synapse*>::iterator iter;
int pos;
double value;
if(_name[0]=='r')
ExpDecay(_lsm_v_mem, _lsm_t_m_c);
else
ExpDecay(_lsm_v_mem, LSM_T_M_C);
ExpDecay(_lsm_state_EP, _lsm_tau_EP);
ExpDecay(_lsm_state_EN, _lsm_tau_EN);
ExpDecay(_lsm_state_IP, _lsm_tau_IP);
ExpDecay(_lsm_state_IN, _lsm_tau_IN);
// sum up the effect
_lsm_state_EP += _prev_delta_ep;
_lsm_state_EN += _prev_delta_en;
_lsm_state_IP += _prev_delta_ip;
_lsm_state_IN += _prev_delta_in;
double temp = NOrderSynapticResponse();
_lsm_v_mem += _lsm_R*temp/_lsm_t_m_c;
_lsm_input=temp;
if(temp>0){
_fire_start=true;
}
if(_lsm_ref > 0){
_lsm_ref--;
_lsm_v_mem = LSM_V_REST;
_fired = false;
return;
}
_fired = false;
if(_lsm_v_mem > _lsm_v_thresh){
_lsm_calcium += one;
// 1. handle the _outputSyns after the neuron fires and activate the _outputSyns
// 2. keep track of the t_spike_pre for the corresponding syns
// 3. @para1: whether or not the current neuron is only used as a dummy input
HandleFiringActivity(false, t, train);
_fired = true;
_lsm_v_mem = LSM_V_RESET;
_lsm_ref = LSM_T_REFRAC;
if(_name[0] == 'o' || _name[0] == 'h'){
_f_count ++;
assert(t > (_fire_timings.empty() ? -1 : _fire_timings.back()));
_fire_timings.push_back(t);
}
else
_f_count++;
if(_mode == WRITECHANNEL&&_generate_transient){
if(_lsm_channel == NULL){
cout<<"Failure to assign a channel ptr to the neuron: "<<_name<<endl;
assert(_lsm_channel);
}
_lsm_channel->AddSpike(t);
}
}
#ifdef SPIKL_IP
if(_SpiKL_ip && _network->LSMGetNetworkMode()==TRANSIENTSTATE)
SpiKL_IP(t);
#endif
}
void Neuron::LSMSetChannel(Channel * channel, channelmode_t channelmode){
_lsm_channel = channel;
_t_next_spike = _lsm_channel->FirstSpikeT();
if(_t_next_spike == -1 || _mode == DEACTIVATED) _network->LSMChannelDecrement(channelmode);
}
void Neuron::LSMRemoveChannel(){
_lsm_channel = NULL;
}
//* Get the timing of the spikes
void Neuron::GetSpikeTimes(vector<int>& times){
if((_name[0] == 'o' || _name[0] == 'h') || !_fire_timings.empty()){
times = _fire_timings;
}
else{
if(_lsm_channel == NULL){
times = vector<int>();
}
else
_lsm_channel->GetAllSpikes(times);
}
}
//* Set the timing of the spikes, only valid for the readout or hidden neurons
void Neuron::SetSpikeTimes(const vector<int>& times){
assert(_name[0] == 'o' || _name[0] == 'h');
_fire_timings = times;
}
//* Collect the presynaptic neuron firing activity:
void Neuron::CollectPreSynAct(double& p_n, double& avg_i_n, int& max_i_n){
if(_presyn_act.empty()){
cout<<"Do you forget to record the pre-synaptic firing activities??\n"
<<"Or do you mistakenly clear the vector<bool> _presyn_act ?"<<endl;
assert(!_presyn_act.empty());
}
int sum_intvl = 0, cnt_f = 0, max_i = 0;
int start = -1;
for(int i = 0; i < _presyn_act.size(); ++i){
if(_presyn_act[i]){
++cnt_f;
max_i = max(max_i, i - start - 1);
sum_intvl += i - start - 1;
start = i;
}
}
max_i = max(max_i, (int)_presyn_act.size() - start - 1);
sum_intvl += _presyn_act.size() - start - 1;
p_n = ((double)cnt_f)/((double)_presyn_act.size());
avg_i_n = ((double)sum_intvl)/(cnt_f+1);
max_i_n = max_i;
// clear the presynaptic neuron activity vector after visiting it!
_presyn_act.clear();
}
// The function to apply dynamic threshold
void Neuron::SpiKL_IP(int t){
double Tau_Lower_Bound=32;
double Tau_Upper_Bound=512;
double R_Lower_Bound=32;
double R_Upper_Bound=512;
double y=_lsm_calcium/(TAU_C-1);
double LR1=5;
double LR2=5;
double beta=5; //reciprocal of miu
if(!(_name[0] == 'r' && _name[9] == '_')) return;
double X;
if(y>0.01){
X=_lsm_v_thresh/(exp((1/_lsm_t_m_c)*(1/y-LSM_T_REFRAC))-1);
_lsm_R+=LR1*(2*y*_lsm_t_m_c*_lsm_v_thresh-X-_lsm_v_thresh-beta*_lsm_t_m_c*_lsm_v_thresh*y*y)/(_lsm_R*X);
_lsm_t_m_c+=LR2*(2*LSM_T_REFRAC*y-1-beta*(LSM_T_REFRAC*y*y-y))/_lsm_t_m_c;
}
else{
_lsm_R+=LR1*0.1;
_lsm_t_m_c-=LR2*0.1;
}
if(_lsm_R<R_Lower_Bound)
_lsm_R=R_Lower_Bound;
if(_lsm_R>R_Upper_Bound)
_lsm_R=R_Upper_Bound;
if(_lsm_t_m_c<Tau_Lower_Bound)
_lsm_t_m_c=Tau_Lower_Bound;
if(_lsm_t_m_c>Tau_Upper_Bound)
_lsm_t_m_c=Tau_Upper_Bound;
}
//* write the output synaptic weigths to the file
void Neuron::WriteOutputWeights(ofstream& f_out, int& index, const string& post_g){
for(Synapse* synapse : _outputSyns){
assert(synapse);
string post_name(synapse->PostNeuron()->Name());
if(post_name.substr(0, post_g.length()) != post_g) continue;
f_out<<index++<<"\t"<<_name<<"\t"<<synapse->PostNeuron()->Name()<<"\t"
<<synapse->Weight()<<endl;
}
}
//* this function is to disable the output synapses whose type is syn_t
void Neuron::DisableOutputSyn(synapsetype_t syn_t){
for(Synapse* synapse : _outputSyns){
bool flag = syn_t == INPUT_SYN ? synapse->IsInputSyn() :
syn_t == READOUT_SYN ? synapse->IsReadoutSyn() :
syn_t == RESERVOIR_SYN ? synapse->IsLiquidSyn() : false;
if(flag){
synapse->DisableStatus(true);
}
}
}
// erase the ptr of the input syns given the name of the presynaptic neuron
void Neuron::LSMDeleteInputSynapse(char * pre_name){
Neuron * pre;
for(auto iter = _inputSyns.begin(); iter != _inputSyns.end(); ){
pre = (*iter)->PreNeuron();
assert(pre != NULL);
//cout<<pre->Name()<<endl;
if(strcmp(pre->Name(),pre_name) == 0){
iter = _inputSyns.erase(iter);
break;
}
else
iter++;
}
}
// erase the ptr of the synapses whose weight is zero with the given type
// @param1: synapsetype_t , @param2: "in" or "out" synapses
int Neuron::RMZeroSyns(synapsetype_t syn_t, const char * t){
bool f;
int cnt = 0;
if(strcmp(t, "in") == 0) f = false;
else if(strcmp(t, "out") == 0) f = true;
else assert(0);
vector<Synapse*>& syns = !f ? _inputSyns : _outputSyns;
for(auto iter = syns.begin(); iter != syns.end(); ){
assert(*iter);
bool flag = syn_t == INPUT_SYN ? (*iter)->IsInputSyn() :
syn_t == READOUT_SYN ? (*iter)->IsReadoutSyn() :
syn_t == RESERVOIR_SYN ? (*iter)->IsLiquidSyn() : false;
flag &= (*iter)->IsWeightZero();
if(flag){
(*iter)->DisableStatus(true);
iter = syns.erase(iter);
cnt++;
}
else
iter++;
}
return cnt;
}
//* This function is to delete all the in/out synapses starts with char 's'
// @param1: the type : "in" or "out"; @param2: the start character
void Neuron::DeleteSyn(const char * t, const char s){
bool f;
if(strcmp(t, "in") == 0) f = false;
else if(strcmp(t, "out") == 0) f = true;
else assert(0);
vector<Synapse*>& syns = !f ? _inputSyns : _outputSyns;
// delete all the input synapses starts with 's':
for(auto it=syns.begin();it != syns.end(); ){
Neuron * pre = (*it)->PreNeuron(); assert(pre);
char * pre_name = pre->Name(); assert(pre_name);
if(pre_name[0] == s) it = syns.erase(it);
else ++it;
}
}
//* This function is to print all the in/out synapses starts with char 's'
//* It is mainly used for debugging purpose.
//* @param1: target file name @param2: "in" or "out"; @param3: the start character
void Neuron::PrintSyn(ofstream& f_out, const char * t, const char s){
bool f;
if(strcmp(t, "in") == 0) f = false;
else if(strcmp(t, "out") == 0) f = true;
else assert(0);
vector<Synapse*>& syns = !f ? _inputSyns : _outputSyns;
// print all the input synapses starts with 's':
for(auto it=syns.begin();it != syns.end(); ++it){
Neuron * neu = !f ? (*it)->PreNeuron() : (*it)->PostNeuron(); assert(neu);
char * name = neu->Name(); assert(name);
if(name[0] == s){
if(!f)
f_out<<name<<"\t"<<_name;
else
f_out<<_name<<"\t"<<name;
}
}
f_out<<endl;
}
void Neuron::DeleteAllSyns(){
_inputSyns.clear();
_outputSyns.clear();
_del = true;
}
inline bool Neuron::GetStatus(){
if(_del == true) return true;
else return false;
}
//* Get the square sum of all its input synapses
double Neuron::GetInputSynSqSum(double weight_limit){
if(_inputsyn_sq_sum == -1){
double sum = 0;
for(auto it = _inputSyns.begin(); it != _inputSyns.end(); ++it){
double weight = (*it)->Weight();
sum += (weight/weight_limit)*(weight/weight_limit);
}
_inputsyn_sq_sum = _inputSyns.empty() ? 0 : sum/_inputSyns.size();
}
return _inputsyn_sq_sum;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
BiasNeuron::BiasNeuron(char * name, bool excitatory, Network * network, double vmem, int dummy_f):
Neuron(name, excitatory, network, vmem),
_dummy_freq(dummy_f)
{
}
void BiasNeuron::LSMNextTimeStep(int t, FILE * Foutp, bool train, int end_time){
assert(_mode == NORMAL);
_fired = false;
if(t % _dummy_freq == 0){
HandleFiringActivity(true, t, train);
_fired = true;
++_f_count;
assert(t > (_fire_timings.empty() ? -1 : _fire_timings.back()));
_fire_timings.push_back(t);
}
}
///////////////////////////////////////////////////////////////////////////
NeuronGroup::NeuronGroup(char * name, int num, Network * network, bool excitatory, double v_mem):
_firstCalled(false),
_s_firstCalled(false),
_lsm_coordinates(0),
_b_neuron(NULL),
_has_lateral(false),
_lateral_w(0.0),
_network(network)
{
_name = new char[strlen(name)+2];
strcpy(_name,name);
_neurons.resize(num);
char * neuronName = new char[1024];
for(int i = 0; i < num; i++){
sprintf(neuronName,"%s_%d",name,i);
Neuron * neuron = new Neuron(neuronName, excitatory, network, v_mem);
neuron->SetIndexInGroup(i);
_neurons[i] = neuron;
}
delete [] neuronName;
}
NeuronGroup::NeuronGroup(char * name, char * path_neuron, char * path_synapse, Network * network):
_firstCalled(false),
_s_firstCalled(false),
_b_neuron(NULL),
_has_lateral(false),
_lateral_w(0.0),
_network(network)
{
bool excitatory;
char ** token = new char*[64];
char linestring[8192];
int i,weight_value,count;
size_t pre_i,post_j;
FILE * fp_neuron_path = fopen(path_neuron , "r");
assert(fp_neuron_path != NULL);
FILE * fp_synapse_path = fopen(path_synapse , "r");
assert(fp_synapse_path != NULL);
_name = new char[strlen(name)+2];
strcpy(_name,name);
_neurons.resize(0);
while(fgets(linestring, 8191 , fp_neuron_path) != NULL){ //Parse the information of reservoir neurons
if(strlen(linestring) <= 1) continue;
if(linestring[0] == '#') continue;
token[0] = strtok(linestring,"\t\n");
assert(token[0] != NULL);
token[1] = strtok(NULL,"\t\n");
assert(token[1] != NULL);
assert((strcmp(token[1],"excitatory") == 0)||(strcmp(token[1],"inhibitory") == 0));
if(strcmp(token[1],"excitatory") == 0) excitatory = true;
else excitatory = false;
Neuron * neuron = new Neuron(token[0],excitatory,network);
_neurons.push_back(neuron);
}
// for(i = 0; i < _neurons.size(); ++i)
// cout<<"i:\t"<<i<<"\tname:"<<(*_neurons[i]).Name()<<endl;
fclose(fp_neuron_path);
count = 0;
while(fgets(linestring, 8191, fp_synapse_path) != NULL){ //Parse the information of connectivity within the reservoir
if(strlen(linestring) <= 1) continue;
if(linestring[0] == '#') continue;
token[0] = strtok(linestring,"\t\n");
assert(token[0] != NULL);
token[1] = strtok(NULL,"\t\n");
assert(token[1] != NULL);
token[2] = strtok(NULL,"\t\n");
assert(token[1] != NULL);
token[3] = strtok(NULL,"\t\n");
assert(token[1] != NULL);
pre_i = atoi(token[0]+10);
post_j = atoi(token[1]+10);
assert((pre_i <= _neurons.size())&&(post_j <= _neurons.size()));
weight_value = atoi(token[3]);
_network->LSMAddSynapse(_neurons[pre_i], _neurons[post_j], (double)weight_value, true, 8.0, true, this);
++count;
}
fclose(fp_synapse_path);
cout<<"# of Reservoir Synapses = "<<count<<endl;
delete [] token;
}
NeuronGroup::NeuronGroup(char * name, int dim1, int dim2, int dim3, Network * network):
_firstCalled(false),
_s_firstCalled(false),
_b_neuron(NULL),
_has_lateral(false),
_lateral_w(0.0),
_network(network)
{
int num = dim1*dim2*dim3;
bool excitatory;
int i, j, k, index;
char * neuronName = new char[1024];
srand(5);
_name = new char[strlen(name)+2];
strcpy(_name,name);
_firstCalled = false;
_neurons.resize(num);
_lsm_coordinates = new int*[num];
// initialization of neurons
for(i = 0; i < num; i++){
if(rand()%100 < 20) excitatory = false;
else excitatory = true;
sprintf(neuronName,"%s_%d",name,i);
Neuron * neuron = new Neuron(neuronName,excitatory,network);
#ifdef SPIKL_IP
neuron->EnableSpiKL_IP(true);
#endif
neuron->SetIndexInGroup(i);
_neurons[i] = neuron;
_lsm_coordinates[i] = new int[3];
}
for(i = 0; i < dim1; i++)
for(j = 0; j < dim2; j++)
for(k = 0; k < dim3; k++){
index = ((i*dim2+j)*dim3)+k;
_lsm_coordinates[index][0] = i;
_lsm_coordinates[index][1] = j;
_lsm_coordinates[index][2] = k;
}
// initialization of synapses
double c, a;
double distsq, dist;
const double factor = 10;
const double factor2 = 1;
int counter = 0;
for(i = 0; i < num; i++)
for(j = 0; j < num; j++){
// if(i==j) continue;
if(_neurons[i]->IsExcitatory()){
if(_neurons[j]->IsExcitatory()){
c = 0.3*factor2;
a = 1;
}else{
c = 0.2*factor2;
a = 1;
}
}else{
if(_neurons[j]->IsExcitatory()){
c = 0.4*factor2;
a = -1;
}else{
c = 0.1*factor2;
a = -1;
}
}
distsq = 0;
dist= _lsm_coordinates[i][0]-_lsm_coordinates[j][0];
distsq += dist*dist;
dist= _lsm_coordinates[i][1]-_lsm_coordinates[j][1];
distsq += dist*dist;
dist= _lsm_coordinates[i][2]-_lsm_coordinates[j][2];
distsq += dist*dist;
if(rand()%100000 < 100000*c*exp(-distsq/3)){
counter++;
_network->LSMAddSynapse(_neurons[i], _neurons[j],a, true, 8.0, true, this);
}
}
cout<<"# of reservoir synapses = "<<counter<<endl;
delete [] neuronName;
neuronName = 0;
}
NeuronGroup::~NeuronGroup(){
if(_name != NULL) delete [] _name;
}
// add the synapse (reservoir synapses) into the neurongroup:
void NeuronGroup::AddSynapse(Synapse * synapse){
assert(synapse && !synapse->IsReadoutSyn()&& !synapse->IsInputSyn());
_synapses.push_back(synapse);
}
Neuron * NeuronGroup::First(){
assert(_firstCalled == false);
_firstCalled = true;
_iter = _neurons.begin();
if(_iter != _neurons.end()) return (*_iter);
else return NULL;
}
Neuron * NeuronGroup::Next(){
assert(_firstCalled == true);
if(_iter == _neurons.end()) return NULL;
_iter++;
if(_iter != _neurons.end()) return (*_iter);
else{
_firstCalled = false;
return NULL;
}
}
Synapse * NeuronGroup::FirstSynapse(){
assert(_s_firstCalled == false);
_s_firstCalled = true;
_s_iter = _synapses.begin();
if(_s_iter != _synapses.end()) return (*_s_iter);
else return NULL;
}
Synapse * NeuronGroup::NextSynapse(){
assert(_s_firstCalled == true);
if(_s_iter == _synapses.end()) return NULL;
_s_iter++;
if(_s_iter != _synapses.end()) return (*_s_iter);
else{
_s_firstCalled = false;
return NULL;
}
}
Neuron * NeuronGroup::Order(int index){
assert((index >= 0)&&(index < _neurons.size()));
return _neurons[index];
}
void NeuronGroup::UnlockFirst(){
_firstCalled = false;
}
void NeuronGroup::UnlockFirstSynapse(){
_s_firstCalled = false;
}
void NeuronGroup::PrintTeacherSignal(){
for(int i = 0; i < _neurons.size(); i++) _neurons[i]->PrintTeacherSignal();
}
void NeuronGroup::PrintMembranePotential(double t){
cout<<t;
for(int i = 0; i < _neurons.size(); i++) _neurons[i]->PrintMembranePotential();
cout<<endl;
}
//* this function is to load speech into the neuron group:
//* channel mode can be INPUT/RESERVOIR, which is a way to tell the types of the neuron:
void NeuronGroup::LSMLoadSpeech(Speech * speech, int * n_channel, neuronmode_t neuronmode, channelmode_t channelmode){
assert(speech);
if(_neurons.size() != speech->NumChannels(channelmode)){
if(!(channelmode == RESERVOIRCHANNEL && (speech->NumChannels(channelmode) == 0))){
cout<<"channelmode: "<<channelmode<<" has "<<speech->NumChannels(channelmode)<<" channels!"<<endl;
assert(channelmode == RESERVOIRCHANNEL && speech->NumChannels(channelmode) == 0);
}
if(channelmode == RESERVOIRCHANNEL)
speech->SetNumChannel(_neurons.size(), RESERVOIRCHANNEL);
else
assert(0); // your code should never go here
}
if(neuronmode == DEACTIVATED)
*n_channel = 0;
else
*n_channel = speech->NumChannels(channelmode);
// assign the channel ptr to each neuron:
for(int i = 0; i < _neurons.size(); i++){
_neurons[i]->LSMSetChannel(speech->GetChannel(i,channelmode),channelmode);
}
LSMSetNeurons(neuronmode);
}
//* Set the neuron mode for each neuron in the group
void NeuronGroup::LSMSetNeurons(neuronmode_t neuronmode){
for(int i = 0; i < _neurons.size(); i++){
_neurons[i]->LSMSetNeuronMode(neuronmode);
}
}
//* Collect the max/min for E/I/P/N and max # of active pre-spikes for each neuron
void NeuronGroup::Collect4State(int& ep_max, int& ep_min, int& ip_max, int& ip_min,
int& en_max, int& en_min, int& in_max, int& in_min, int& pre_active_max)
{
for(size_t i = 0; i < _neurons.size(); ++i){
assert(_neurons[i]);
ep_max = max(ep_max, _neurons[i]->GetEPMax()), ep_min = min(ep_min, _neurons[i]->GetEPMin());
en_max = max(en_max, _neurons[i]->GetENMax()), en_min = min(en_min, _neurons[i]->GetENMin());
ip_max = max(ip_max, _neurons[i]->GetIPMax()), ip_min = min(ip_min, _neurons[i]->GetIPMin());
in_max = max(in_max, _neurons[i]->GetINMax()), in_min = min(in_min, _neurons[i]->GetINMin());
pre_active_max = max(pre_active_max, _neurons[i]->GetPreActiveMax());
}
}
//* Collect the presynaptic firing activity:
void NeuronGroup::CollectPreSynAct(double & p_r, double & avg_i_r, int & max_i_r){
double p_n = 0.0, avg_i_n = 0.0;
int max_i_n = 0;
for(size_t i = 0; i < _neurons.size(); ++i){
assert(_neurons[i]);
_neurons[i]->CollectPreSynAct(p_n, avg_i_n, max_i_n);
p_r += p_n, avg_i_r += avg_i_n, max_i_r = max(max_i_r, max_i_n);
}
p_r = _neurons.empty() ? 0 : p_r/((double)_neurons.size());
avg_i_r = _neurons.empty() ? 0 : avg_i_r/((double)_neurons.size());
}
//* judge the results of the readout layer after each speech is presented:
int NeuronGroup::Judge(int cls){
vector<pair<int, int> > f_pairs;