forked from lusterwater/single-matrix-network-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cpp
1204 lines (873 loc) · 40.2 KB
/
Network.cpp
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
// Network.cpp: implementation of the Network class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Network.h"
#include "Math.h";
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Network::Network()
{
printf("Network Construction.\n");
numberOfInputs = 2 ;
numberOfOutputs = 2;
numberOfInterNeurons = 6;
networkDimension = numberOfInputs + numberOfOutputs + numberOfInterNeurons;
setNeuronOutput( 0.0 ); // set initial activations to zero. Maybe better to be one.
setNeuronThresholds( 0.0 ); // set thresholds for output in network neurons.
setNeuronLearningRate( 0.0 ); // set learning rate for plastic neurons in network - 0 is non-learning.
setNeuronRefractoryState( 0 ); // set set refactory state for neurons in the network - 0 is no refactory state -- must not be negative.
setNeuronWeightTotal( 1.0 ); // set set refactory state for neurons in the network - 0 is no refactory state -- must not be negative.
setNetworkWeights( 0.5 ); // Temporary function and value to test the code
setNeuronActivation( 0.0 ); // Temporary function and value to test the code
setNetworkOutputs( 0.0 );
setPlasticWeightsMask( 0 );
normalizeNeuronWeights( ); // Set the sum of network weights for each unit to the unit total specified above.
}
// This is a blank constructor that will produce a file in the current standard format.
Network::Network(int inputs, int interneurons, int outputs, char * out_file_name )
{
printf("Network Construction.\n");
numberOfInputs = inputs ;
numberOfOutputs = outputs;
numberOfInterNeurons = interneurons;
networkDimension = numberOfInputs + numberOfOutputs + numberOfInterNeurons;
setNeuronOutput( 0.0 ); // set initial activations to zero. Maybe better to be one.
setNeuronThresholds( 0.0 ); // set thresholds for output in network neurons.
setNeuronLearningRate( 0.0 ); // set learning rates for network neurons -- 0 means that neuron does not adjust its connection weights
setNeuronRefractoryState( 0 ); // set set refactory state for neurons in the network - 0 is no refactory state -- must not be negative.
setNeuronWeightTotal( 1.0 ); // set set weight for neurons in the network - 1.0 typical and default.
setNetworkWeights( 0.5 ); // Temporary function and value to test the code
setNeuronActivation( 0.0 ); // Temporary function and value to test the code
setNetworkOutputs( 0.0 );
setPlasticWeightsMask( 0 );
normalizeNeuronWeights( ); // Set the sum of network weights for each unit to the unit total specified above.
writeNetworkToFile( out_file_name );
}
// This constructor Assumes a properly formatted data file. It does no checking or corrections.
Network::Network( char * file_name )
{
int error = 0;
error = readNetworkFromFile( file_name ); // This function should return an error message for an improperly specified network.
// setNetworkNeuronActivation( 0.0 );
if(error == 1) printf("Bad Network file specification in file %s.\n", file_name); // if the file is bad print a warning
}
Network::~Network()
{
printf("Network destruction.\n");
}
void Network::instantiateDefaultNetwork( void )
{
numberOfInputs = 2 ;
numberOfOutputs = 2;
numberOfInterNeurons = 6;
networkDimension = numberOfInputs + numberOfOutputs + numberOfInterNeurons;
setNeuronOutput( 0.0 ); // set initial activations to zero. Maybe better to be one.
setNeuronThresholds( 0.0 ); // set thresholds for output in network neurons.
setNeuronLearningRate( 0.0 ); // set learning rate for plastic neurons in network - 0 is non-learning.
setNeuronRefractoryState( 0 ); // set set refactory state for neurons in the network - 0 is no refactory state -- must not be negative.
setNeuronWeightTotal( 1.0 ); // set set inpute weight for neurons in the network - 1 is typical and default-- must not be negative.
setNetworkWeights( 0.5 ); // Temporary function and value to test the code
setNeuronActivation( 0.0 ); // Temporary function and value to test the code
setNetworkOutputs( 0.0 );
setPlasticWeightsMask( 0 );
}
/* --------------------------------------------------
Print network state
*/
void Network::PrintNetworkState( void )
{
printf(" Number of inputs: %d\n",numberOfInputs);
printf(" Number of outputs: %d\n",numberOfOutputs);
printf(" Number of interneuorns: %d\n",numberOfInterNeurons);
printf(" Network Dimension: %d\n",networkDimension);
}
/* --------------------------------------------------
networkActivation
This is a comitted matrix multiplication routine that generates the activation from the current state of the network.
It treates autapses and inputs specially so it is not strictly speaking pure matrix mathematics
*/
void Network::networkActivation( void )
{
int neuron_number, source_neuron_number, k;
// ----------------- Compute intrisinc network activations
// -- Update autapses
for( neuron_number = 0; neuron_number < networkDimension; ++neuron_number){
k = networkDimension*neuron_number + neuron_number; // you should make this a function rather than computing it 2x in this routine, it could be re-used for other routines and avoid problems of different computations in different locations
neuronActivation[neuron_number] = neuronActivation[neuron_number] * networkWeights[k];
//printf("-- %2.3lf %2.3lf\n", neuronActivation[neuron_number], networkWeights[k]);
}
// -- Update inputs from other neurons
for( neuron_number = 0; neuron_number < networkDimension; ++neuron_number){ // note that iterations can be reduced in this routine by skipping the inputs -- which do not need to be updated through network weights, they are set from the outside
for(source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
if(neuron_number != source_neuron_number){ // used self weights above, avoid double dipping
k = networkDimension*source_neuron_number + neuron_number; // obtain the index of the 2d weight array represented as a 1 d array.
neuronActivation[neuron_number] += neuronOutput[source_neuron_number] * networkWeights[k];
//printf("-- %2.3lf %2.3lf\n", neuronActivation[neuron_number], networkWeights[k]);
}
}
// ------------------ Add External Inputs to Activations -----------------------
if( neuron_number < numberOfInputs ) {
neuronActivation[neuron_number] += networkInputs[neuron_number]; // Network inputs are set externally
//printf("-- %2.3lf %2.3lf\n", neuronActivation[neuron_number], networkInputs[neuron_number]);
}
}
}
/* --------------------------------------------------
setNetworkInput
copy and external vector of inputs the the input section of the network inputs vector.
note: this routine should check that the inputs are approprirate -- in bounds.
*/
void Network::setNetworkInput( double *vector)
{
int i;
for(i = 0; i< numberOfInputs ; ++i) {
networkInputs[i] = vector[i];
//printf("input %lf ",vector[i]);
//printf("input %lf ",networkInputs[i]);
}
//printf("\n ");
}
/* --------------------------------------------------
copyNetworkInputsToInputNeuronOutputs
*/
void Network::copyNetworkInputsToInputNeuronOutputs( void )
{
int i;
for(i = 0; i < numberOfInputs; ++i ) {
neuronOutput[i] = networkInputs[i];
//printf("input %lf ",networkNeuronOutput[i]);
}
//printf("\n ");
}
/* --------------------------------------------------
setNetworkOuput
copy and external vector of inputs the the input section of the network inputs vector.
note: this routine should check that the inputs are approprirate -- in bounds.
*/
void Network::setNetworkOuput( void )
{
int i;
for(i = 0; i< numberOfOutputs; ++i) {
networkOutputs[i] = neuronOutput[numberOfInputs + numberOfInterNeurons + i];
//printf("* %d ",numberOfInputs + numberOfInterNeurons + i);
//printf("* %lf ",networkNeuronOutput[numberOfInputs + numberOfInterNeurons -1 + i]);
}
//printf("\n",networkOutputs[i]);
}
/* --------------------------------------------------
setNetworkNeuronOutput
*/
//Network::setNetworkNeuronOutput( void )
void Network::copyNeuronActivationsToNeuronOutputs( void )
{
int i;
for(i = 0; i < networkDimension; ++i){
neuronActivation[i] = neuronActivation[i];
//printf("%2.2lf %2.2lf | ",networkNeuronOutput[i] , networkNeuronActivation[i]);
}
//printf("\n");
}
/* --------------------------------------------------
thresholdNeuronOutputs
Computes a hard thresholded output from the neuron activations using the individual neuron threshold
*/
void Network::thresholdNeuronOutputs( void )
{
int i;
for(i = 0; i < networkDimension; ++i){
if(neuronActivation[i] > neuronThresholds[i]){
neuronOutput[i] = neuronActivation[i] - neuronThresholds[i];
// neuronActivation[i] = neuronActivation[i];
}
else neuronOutput[i] = 0.0;
//printf("*** %2.3lf %2.3lf\n",neuronOutput[i],neuronThresholds[i]);
}
//printf("\n ");
}
/* --------------------------------------------------
getNetworkOuput
a function meant to supply the network outputs to outside process
*/
void Network::getNetworkOuput( double * vector )
{
int i;
for(i = 0; i< numberOfOutputs; ++i) {
vector[i] = networkOutputs[i];
}
}
/* --------------------------------------------------
setNetworkWeights
a function meant to supply the network outputs to outside process
*/
void Network::setNetworkWeights( double value )
{
int i;
for(i = 0; i< networkDimension*networkDimension ; ++i) {
networkWeights[i] = value;
}
}
/* --------------------------------------------------
setNetworkWeightsDiagonalRange
a function meant to supply the network outputs to outside process
*/
void Network::setNetworkWeightsDiagonalRange( double value, int start_row_and_col, int end_row_and_col )
{
int i;
for(i = start_row_and_col; i < end_row_and_col; i++) {
networkWeights[i*networkDimension + i] = value;
}
}
/* --------------------------------------------------
setNetworkWeightsRectangle
a function meant to supply the network outputs to outside process
NEEDS to check that values passeed are in bounds of the array.
*/
void Network::setNetworkWeightsRectangle( double value, int start_row, int end_row, int start_column, int end_column )
{
int i, j, index;
for(i = start_column; i < end_column; i++) {
for(j = start_row; j < end_row ; j++) {
index = networkDimension*i +j;
networkWeights[index] = value;
}
}
}
/* --------------------------------------------------
setNetworkWeightsUpperTriangle
a function meant to supply the network outputs to outside process
NEEDS to check that values passeed are in bounds of the array.
*/
void Network::setNetworkWeightsUpperTriangle( double value, int start_row, int end_row, int start_column, int end_column )
{
int i, j, index;
for(i = start_column; i < end_column; i++) {
for(j = start_row; j < end_row ; j++) {
if(i > j ){
index = networkDimension*i +j;
networkWeights[index] = value;
}
}
}
}
/* --------------------------------------------------
setNetworkWeightsLowerTriangle
a function meant to supply the network outputs to outside process
NEEDS to check that values passeed are in bounds of the array.
*/
void Network::setNetworkWeightsLowerTriangle( double value, int start_row, int end_row, int start_column, int end_column )
{
int i, j, index;
for(i = start_column; i < end_column; i++) {
for(j = start_row; j < end_row ; j++) {
if(i > j ){
index = networkDimension*i +j;
networkWeights[index] = value;
}
}
}
}
/* --------------------------------------------------
setNetworkWeightsUpperLowerTriangleAndDiagonal
a function meant to supply the network outputs to outside process
NEEDS to check that values passeed are in bounds of the array.
*/
void Network::setNetworkWeightsUpperLowerTriangleAndDiagonal( double diagonal_value, double upper_triangle_value, double lower_triangle_value)
{
int i, j, index;
for(i = 0; i < networkDimension ; i++) {
for(j = 0; j < networkDimension ; j++) {
index = networkDimension*i +j;
if( i < j ) networkWeights[index] = upper_triangle_value;
if( i > j ) networkWeights[index] = lower_triangle_value;
if( i == j ) networkWeights[index] = diagonal_value;
}
}
}
/* --------------------------------------------------
/* --------------------------------------------------
setPlasticWeightsMask
a function meant to supply the network outputs to outside process
*/
void Network::setPlasticWeightsMask( short int value )
{
int i;
for(i = 0; i< networkDimension*networkDimension ; ++i) {
plasticWeightsMask[i] = value;
}
}
/* --------------------------------------------------
setNetworkNeuronActivation
*/
void Network::setNeuronActivation( double value )
{
int i;
for(i = 0; i< networkDimension ; ++i) {
neuronActivation[i] = value;
}
}
/* --------------------------------------------------
setNetworkNeuronActivation
*/
void Network::setNeuronOutput( double value )
{
int i;
for(i = 0; i< networkDimension ; ++i) {
neuronOutput[i] = value;
}
}
/* --------------------------------------------------
setNetworkNeuronThresholds
*/
void Network::setNeuronThresholds( double value )
{
int i;
for(i = 0; i< networkDimension ; ++i) {
neuronThresholds[i] = value;
}
}
/* --------------------------------------------------
setNetworkLearningRate
Set to zero for non-learning neurons
Negative values should be used with care.
Typically positive values within the interval [0,1]
*/
void Network::setNeuronLearningRate( double value )
{
int i;
for(i = 0; i< networkDimension ; ++i) {
neuronLearningRate[i] = value;
}
}
/* --------------------------------------------------
setNetworkRefractoryState
Integer values are the number of the time-steps that the neuron is unable to fire.
Set to zero for non-refactory neurons
Must not be negative. Negative values are set to zero. !!! SET TO ZERO !!!
Returns an error of 1 if the refractory state has been changed to zero because a negative value was passed as an argument
*/
int Network::setNeuronRefractoryState( int value )
{
int i, error = 0;
if(value <0){
value = 0;
error = 1;
}
for(i = 0; i< networkDimension ; ++i) {
neuronRefractoryState[i] = value;
}
return(error);
}
/* --------------------------------------------------
setNeuronWeightTotal
Integer values are the number of the time-steps that the neuron is unable to fire.
Set to zero for non-refactory neurons
Must not be negative. Negative values are set to zero. !!! SET TO ZERO !!!
Returns an error of 1 if the refractory state has been changed to zero because a negative value was passed as an argument
*/
void Network::setNeuronWeightTotal( double value)
{
int i;
for(i = 0; i< networkDimension ; ++i) {
neuronWeightTotal[i] = value;
}
}
/* --------------------------------------------------
setNetworkOutputs
*/
void Network::setNetworkOutputs( double value )
{
int i;
for(i = 0; i< numberOfOutputs; ++i) {
networkOutputs[i] = value;
}
}
/* --------------------------------------------------
squashNeuronOutput
Function to apply the scale logistic funtion to a double value
Unscale version have the value
max = 1.0, slope = 1.0, offset = 0
Scaling of the neuron output with the squashing function
max sets the maximum value of the neuron output (min value should be 0)
slope maps the a wide output range with low slope and a narrow output range with large slope
offset shifts the input on the x axis
Last revised: 11/07/16 CL, IT, ZC, JJ, FG
*/
double Network::squashNeuronOutput(double value, double max, double slope, double offset)
{
double result;
result = max/(1+exp(-value * slope + offset));
return (result);
}
/* --------------------------------------------------
printNetworkOuput
a function meant to supply the network outputs to outside process
*/
void Network::printNetworkOuput( void )
{
int i;
for(i = 0; i< numberOfOutputs; ++i) {
printf("%f ",networkOutputs[i]);
}
printf("\n");
}
/* --------------------------------------------------
cycleNetwork
a function meant to supply the network outputs to outside process
Notes:
1. Inputs should be set separately. This routine does not make use of external input. It uses the current neuron outputs into inputs.
The network inputs must be set before calling this routine to get the network to respond to new input
information.
*/
void Network::cycleNetwork( void )
{
networkActivation( ); // perform adjusted matrix multiplication of the weights and current network state
// setNetworkNeuronOutput( ); // Transform activations into outputs and copy
copyNeuronActivationsToNeuronOutputs( );
// thresholdNeuronOutputs( ); // Transform activations into outputs following hard threshold
squashNetworkOutput( );
setNetworkOuput( ); // Copy the network output to the output array *+* consider moving this call out of the function to allow network "settling time" before external functions have access to the network output
}
/* --------------------------------------------------
cycleNetwork
a function meant to supply the network outputs to outside process
Notes:
1. Inputs should be set separately. This routine does not make use of external input. It uses the current neuron outputs into inputs.
The network inputs must be set before calling this routine to get the network to respond to new input
information.
*/
void Network::squashNetworkOutput(void)
{
int i;
for (i = 0; i < networkDimension; ++i) {
networkOutputs[i] = squashNeuronOutput(networkOutputs[i], 1.0, 1.0, 0.0);
}
setNetworkOuput(); // Copy the network output to the output array *+* consider moving this call out of the function to allow network "settling time" before external functions have access to the network output
}
void Network::cycleNetworkNormalizeHebbianLearning( void )
{
/* *+* */
hebbianExcitatoryWeightUpdate( );
normalizeNonDiagonalExcitatoryNeuronWeights( ); // note that this temporary value of 1.0 will ensure the weights of all the units sum to 1.0.
}
/*
hebbianWeightUpdate
NOTE: weight updates will only occur if the weights are marked as plastic and if the neuron has a non-zero learning rate
Both these parameters must agree. If the learning rate is zero there will be no update for that neuron.
If the plastic weights mask is zero there will be no weight update for that weight.
NOTE: The weight changes effected by this routine will cause the values of the weights to grow. Over many cycles they will grow without bound.
Some form of normalization or negative weights need to be used to offset this unbounded weight growth if the network is to be stable
*/
void Network::hebbianWeightUpdate( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_increment;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
if(neuronLearningRate[target_neuron_number] !=0){ // save clock cyles by only computing updates on weights that are plastic.
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_increment = 0;
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
weight_increment = neuronLearningRate[target_neuron_number]*neuronOutput[source_neuron_number]*neuronOutput[target_neuron_number]*plasticWeightsMask[weight_index]; // remember that the plastic weights mask AND the learning rate for a neuron must agree ( both be non-zero) for a neuron to have adaptive weights
networkWeights[weight_index] += weight_increment;
}
}
}
}
/*
hebbianPositiveWeightUpdate
Same as hebbianWeightUpdate but applied only to positive valued weights
NOTE: weight updates will only occur if the weights are marked as plastic and if the neuron has a non-zero learning rate
Both these parameters must agree. If the learning rate is zero there will be no update for that neuron.
If the plastic weights mask is zero there will be no weight update for that weight.
NOTE: The weight changes effected by this routine will cause the values of the weights to grow. Over many cycles they will grow without bound.
Some form of normalization or negative weights need to be used to offset this unbounded weight growth if the network is to be stable
*/
void Network::hebbianExcitatoryWeightUpdate( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_increment;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
if(neuronLearningRate[target_neuron_number] !=0){ // save clock cyles by only computing updates on weights that are plastic.
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_increment = 0;
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if( networkWeights[weight_index] > 0 ){
weight_increment = neuronLearningRate[target_neuron_number]*neuronOutput[source_neuron_number]*neuronOutput[target_neuron_number]*plasticWeightsMask[weight_index]; // remember that the plastic weights mask AND the learning rate for a neuron must agree ( both be non-zero) for a neuron to have adaptive weights
networkWeights[weight_index] += weight_increment;
}
}
}
}
}
/*
hebbianInhibitoryWeightUpdate
Same as hebbianWeightUpdate but applied only to negative valued weights
This function DECREMENTS the weights, making them more negative
NOTE: weight updates will only occur if the weights are marked as plastic and if the neuron has a non-zero learning rate
Both these parameters must agree. If the learning rate is zero there will be no update for that neuron.
If the plastic weights mask is zero there will be no weight update for that weight.
NOTE: The weight changes effected by this routine will cause the values of the weights to grow. Over many cycles they will grow without bound.
Some form of normalization or negative weights need to be used to offset this unbounded weight growth if the network is to be stable
*/
void Network::hebbianInhibitoryWeightUpdate( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_increment;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
if(neuronLearningRate[target_neuron_number] !=0){ // save clock cyles by only computing updates on weights that are plastic.
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_increment = 0;
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if( networkWeights[weight_index] < 0 ){
// weight_increment = neuronLearningRate[target_neuron_number]*neuronOutput[source_neuron_number]*plasticWeightsMask[weight_index]; // remember that the plastic weights mask AND the learning rate for a neuron must agree ( both be non-zero) for a neuron to have adaptive weights
weight_increment = neuronLearningRate[target_neuron_number]*neuronOutput[source_neuron_number]*neuronOutput[target_neuron_number]*plasticWeightsMask[weight_index]; // remember that the plastic weights mask AND the learning rate for a neuron must agree ( both be non-zero) for a neuron to have adaptive weights
networkWeights[weight_index] -= weight_increment; // Note that this DECREMENTS the weight
}
}
}
}
}
/*
normalizeNeuronWeights
Computes the sume of the weights for a given neuron.
Then make a proportional change in the weights of that neuron so that it sums to the passed value.
*/
void Network::normalizeNeuronWeights( double value )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_sum;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
weight_sum = 0.0;
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
// weight_index = networkDimension*target_neuron_number + source_neuron_number;
if(networkWeights[weight_index] >0) weight_sum += networkWeights[weight_index];
if(networkWeights[weight_index] <0) weight_sum = weight_sum - networkWeights[weight_index];
}
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
// weight_index = networkDimension*target_neuron_number + source_neuron_number; weight_index = networkDimension*source_neuron_number + target_neuron_number;
networkWeights[weight_index] = value*( networkWeights[weight_index]/weight_sum);
}
}
}
/*
normalizeNeuronWeights
Computes the sume of the weights for a given neuron.
Then make a proportional change in the weights of that neuron so that it sums to the passed value.
This version preserves the weight total found in a given unit's wieghts. The function normalizeNeuronWeights( double ) sets them to a user specificed value
This function use the sum of the absolute value of each weight to determine the normalization value. (Negative weights are inverted in sign before they are summed).
*/
void Network::normalizeNeuronWeights( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_sum;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
weight_sum = 0.0;
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(networkWeights[weight_index] > 0) weight_sum += networkWeights[weight_index];
if(networkWeights[weight_index] < 0) weight_sum -= networkWeights[weight_index];
}
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
networkWeights[weight_index] = neuronWeightTotal[ target_neuron_number ]*( networkWeights[weight_index]/(double)weight_sum);
}
}
}
/*
normalizeNonDiagonalNeuronWeights
Computes the sume of the weights for a given neuron.
Then make a proportional change in the weights of that neuron so that it sums to the passed value.
It does not update weights that are autapses. self connections along the network diagonal.
This function leaves the weight matrix diagonals ( autapses ) unchanged. And it does not use them in the calculations.
*/
void Network::normalizeNonDiagonalNeuronWeights( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_sum;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
weight_sum = 0.0;
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(target_neuron_number != source_neuron_number){
if(networkWeights[weight_index] > 0) weight_sum += networkWeights[weight_index];
if(networkWeights[weight_index] < 0) weight_sum -= networkWeights[weight_index];
//printf("Weight sum: %lf ", weight_sum);
}
}
if(target_neuron_number != source_neuron_number && weight_sum != 0.0){ // avoid division by zero for input units the autapse may be the only non-zero weight.
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
networkWeights[weight_index] = neuronWeightTotal[ target_neuron_number ]*( networkWeights[weight_index]/(double)weight_sum);
}
}
}
}
/*
normalizeNonDiagonalExcitatoryNeuronWeights
Computes the sume of the weights for a given neuron.
Then make a proportional change in the weights of that neuron so that it sums to the passed value.
It does not update weights that are autapses. self connections along the network diagonal.
This function leaves the weight matrix diagonals ( autapses ) unchanged. And it does not use them in the calculations.
*/
void Network::normalizeNonDiagonalExcitatoryNeuronWeights( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_sum;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
weight_sum = 0.0;
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(target_neuron_number != source_neuron_number && networkWeights[weight_index] > 0){
weight_sum += networkWeights[weight_index];
}
}
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(target_neuron_number != source_neuron_number && weight_sum != 0.0 && networkWeights[weight_index] > 0){ // avoid division by zero for input units the autapse may be the only non-zero weight.
networkWeights[weight_index] = neuronWeightTotal[ target_neuron_number ]*( networkWeights[weight_index]/weight_sum);
}
}
}
}
/*
normalizeNonDiagonalExcitatoryNeuronWeights
Computes the sume of the weights for a given neuron.
Then make a proportional change in the weights of that neuron so that it sums to the passed value.
It does not update weights that are autapses. self connections along the network diagonal.
This function leaves the weight matrix diagonals ( autapses ) unchanged. And it does not use them in the calculations.
*/
void Network::normalizeNonDiagonalInhibitoryNeuronWeights( void )
{
int source_neuron_number, target_neuron_number, weight_index;
double weight_sum;
for( target_neuron_number = 0; target_neuron_number < networkDimension; ++target_neuron_number){
weight_sum = 0.0;
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(target_neuron_number != source_neuron_number && networkWeights[weight_index] < 0){
weight_sum -= networkWeights[weight_index]; // Since the weights are negative, we want a positve sum for the normalization below. at *+*
}
}
for( source_neuron_number = 0; source_neuron_number < networkDimension; ++source_neuron_number){
weight_index = computeWeightIndex( source_neuron_number, target_neuron_number );
if(target_neuron_number != source_neuron_number && weight_sum != 0.0 && networkWeights[weight_index] < 0){ // avoid division by zero for input units the autapse may be the only non-zero weight.
networkWeights[weight_index] = neuronWeightTotal[ target_neuron_number ]*( networkWeights[weight_index]/weight_sum); // *+* a postive weight sum here will produce a negative proportion
}
}
}
}
/* --------------------------------------------------
readNetworkFromFile
takes as input a file name
expects the file to be formatted according to the standard network form
changes to this should be mirrored in writeNetworkToFile
returns an error message 1 if there was an error, 0 if there was no error on file open.
*/
int Network::readNetworkFromFile( char * file_name )
{
int i, item_count = 0, error = 0;
char dummy[MAX_DUMMY_STRING_LENGTH];
FILE *fp;
fp= fopen(file_name,"r");
if( fp == 0) error = 1;
else{
fscanf(fp,"%s %d",&dummy, &numberOfInputs);
fscanf(fp,"%s %d",&dummy, &numberOfOutputs);
fscanf(fp,"%s %d",&dummy, &numberOfInterNeurons);
fscanf(fp,"%s %d",&dummy, &networkDimension); // perhaps networkDimension should be omitted from the Read and computed on the read or in the constructor
// Read the stored network activations
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronActivation[i]);
// Read the stored network outputs
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronOutput[i]);
// Read the stored neuron thresholds
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronThresholds[i]);
// Read the stored neuron learning rates
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronLearningRate[i]);
// Read the stored neuron refractory states
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronRefractoryState[i]);
// Read the stored neuron refractory states
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension; ++i) fscanf(fp,"%lf",&neuronWeightTotal[i]);
// Read the stored network weights
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension*networkDimension; ++i) fscanf(fp,"%lf",&networkWeights[i]);
// Read the stored network inputs
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < numberOfInputs; ++i) fscanf(fp,"%lf",&networkInputs[i]);
fscanf(fp,"%s",&dummy);
// Read the stored network outputs
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < numberOfOutputs; ++i) fscanf(fp,"%lf",&networkOutputs[i]);
// Read the stored network plastic weights mask
fscanf(fp,"%s",&dummy);
for( i = 0 ; i < networkDimension*networkDimension; ++i) fscanf(fp,"%d",&plasticWeightsMask[i]);
}
fclose(fp);
return(error);
}