-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact.go
1416 lines (1177 loc) · 49.5 KB
/
act.go
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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package axon
import (
"cogentcore.org/core/base/randx"
"cogentcore.org/core/math32"
"cogentcore.org/core/math32/minmax"
"cogentcore.org/core/vgpu/gosl/slbool"
"github.com/emer/axon/v2/chans"
)
///////////////////////////////////////////////////////////////////////
// act.go contains the activation params and functions for axon
//gosl:hlsl act
// #include "chans.hlsl"
// #include "minmax.hlsl"
// #include "neuron.hlsl"
//gosl:end act
//gosl:start act
//////////////////////////////////////////////////////////////////////////////////////
// SpikeParams
// SpikeParams contains spiking activation function params.
// Implements a basic thresholded Vm model, and optionally
// the AdEx adaptive exponential function (adapt is KNaAdapt)
type SpikeParams struct {
// threshold value Theta (Q) for firing output activation (.5 is more accurate value based on AdEx biological parameters and normalization
Thr float32 `default:"0.5"`
// post-spiking membrane potential to reset to, produces refractory effect if lower than VmInit -- 0.3 is appropriate biologically based value for AdEx (Brette & Gurstner, 2005) parameters. See also RTau
VmR float32 `default:"0.3"`
// post-spiking explicit refractory period, in cycles -- prevents Vm updating for this number of cycles post firing -- Vm is reduced in exponential steps over this period according to RTau, being fixed at Tr to VmR exactly
Tr int32 `min:"1" default:"3"`
// time constant for decaying Vm down to VmR -- at end of Tr it is set to VmR exactly -- this provides a more realistic shape of the post-spiking Vm which is only relevant for more realistic channels that key off of Vm -- does not otherwise affect standard computation
RTau float32 `default:"1.6667"`
// if true, turn on exponential excitatory current that drives Vm rapidly upward for spiking as it gets past its nominal firing threshold (Thr) -- nicely captures the Hodgkin Huxley dynamics of Na and K channels -- uses Brette & Gurstner 2005 AdEx formulation
Exp slbool.Bool `default:"true"`
// slope in Vm (2 mV = .02 in normalized units) for extra exponential excitatory current that drives Vm rapidly upward for spiking as it gets past its nominal firing threshold (Thr) -- nicely captures the Hodgkin Huxley dynamics of Na and K channels -- uses Brette & Gurstner 2005 AdEx formulation
ExpSlope float32 `default:"0.02"`
// membrane potential threshold for actually triggering a spike when using the exponential mechanism
ExpThr float32 `default:"0.9"`
// for translating spiking interval (rate) into rate-code activation equivalent, what is the maximum firing rate associated with a maximum activation value of 1
MaxHz float32 `default:"180" min:"1"`
// constant for integrating the spiking interval in estimating spiking rate
ISITau float32 `default:"5" min:"1"`
// rate = 1 / tau
ISIDt float32 `display:"-"`
// rate = 1 / tau
RDt float32 `display:"-"`
pad int32
}
func (sk *SpikeParams) Defaults() {
sk.Thr = 0.5
sk.VmR = 0.3
sk.Tr = 3
sk.RTau = 1.6667
sk.Exp.SetBool(true)
sk.ExpSlope = 0.02
sk.ExpThr = 0.9
sk.MaxHz = 180
sk.ISITau = 5
sk.Update()
}
func (sk *SpikeParams) Update() {
if sk.Tr <= 0 {
sk.Tr = 1 // hard min
}
sk.ISIDt = 1 / sk.ISITau
sk.RDt = 1 / sk.RTau
}
func (sk *SpikeParams) ShouldShow(field string) bool {
switch field {
case "ExpSlope", "ExpThr":
return sk.Exp.IsTrue()
default:
return true
}
}
// ActToISI compute spiking interval from a given rate-coded activation,
// based on time increment (.001 = 1msec default), Act.Dt.Integ
func (sk *SpikeParams) ActToISI(act, timeInc, integ float32) float32 {
if act == 0 {
return 0
}
return (1 / (timeInc * integ * act * sk.MaxHz))
}
// ActFromISI computes rate-code activation from estimated spiking interval
func (sk *SpikeParams) ActFromISI(isi, timeInc, integ float32) float32 {
if isi <= 0 {
return 0
}
maxInt := 1.0 / (timeInc * integ * sk.MaxHz) // interval at max hz..
return maxInt / isi // normalized
}
// AvgFromISI returns updated spiking ISI from current isi interval value
func (sk *SpikeParams) AvgFromISI(avg float32, isi float32) float32 {
if avg <= 0 {
avg = isi
} else if isi < 0.8*avg {
avg = isi // if significantly less than we take that
} else { // integrate on slower
avg += sk.ISIDt * (isi - avg) // running avg updt
}
return avg
}
//////////////////////////////////////////////////////////////////////////////////////
// DendParams
// DendParams are the parameters for updating dendrite-specific dynamics
type DendParams struct {
// dendrite-specific strength multiplier of the exponential spiking drive on Vm -- e.g., .5 makes it half as strong as at the soma (which uses Gbar.L as a strength multiplier per the AdEx standard model)
GbarExp float32 `default:"0.2,0.5"`
// dendrite-specific conductance of Kdr delayed rectifier currents, used to reset membrane potential for dendrite -- applied for Tr msec
GbarR float32 `default:"3,6"`
// SST+ somatostatin positive slow spiking inhibition level specifically affecting dendritic Vm (VmDend) -- this is important for countering a positive feedback loop from NMDA getting stronger over the course of learning -- also typically requires SubMean = 1 for TrgAvgAct and learning to fully counter this feedback loop.
SSGi float32 `default:"0,2"`
// set automatically based on whether this layer has any recv pathways that have a GType conductance type of Modulatory -- if so, then multiply GeSyn etc by GModSyn
HasMod slbool.Bool `edit:"-"`
// multiplicative gain factor on the total modulatory input -- this can also be controlled by the PathScale.Abs factor on ModulatoryG inputs, but it is convenient to be able to control on the layer as well.
ModGain float32
// if true, modulatory signal also includes ACh multiplicative factor
ModACh slbool.Bool
// baseline modulatory level for modulatory effects -- net modulation is ModBase + ModGain * GModSyn
ModBase float32
pad int32
}
func (dp *DendParams) Defaults() {
dp.SSGi = 2
dp.GbarExp = 0.2
dp.GbarR = 3
dp.ModGain = 1
dp.ModBase = 0
}
func (dp *DendParams) Update() {
}
func (dp *DendParams) ShouldShow(field string) bool {
switch field {
case "ModGain", "ModACh", "ModBase":
return dp.HasMod.IsTrue()
default:
return true
}
}
//////////////////////////////////////////////////////////////////////////////////////
// ActInitParams
// ActInitParams are initial values for key network state variables.
// Initialized in InitActs called by InitWts, and provides target values for DecayState.
type ActInitParams struct {
// initial membrane potential -- see Erev.L for the resting potential (typically .3)
Vm float32 `default:"0.3"`
// initial activation value -- typically 0
Act float32 `default:"0"`
// baseline level of excitatory conductance (net input) -- Ge is initialized to this value, and it is added in as a constant background level of excitatory input -- captures all the other inputs not represented in the model, and intrinsic excitability, etc
GeBase float32 `default:"0"`
// baseline level of inhibitory conductance (net input) -- Gi is initialized to this value, and it is added in as a constant background level of inhibitory input -- captures all the other inputs not represented in the model
GiBase float32 `default:"0"`
// variance (sigma) of gaussian distribution around baseline Ge values, per unit, to establish variability in intrinsic excitability. value never goes < 0
GeVar float32 `default:"0"`
// variance (sigma) of gaussian distribution around baseline Gi values, per unit, to establish variability in intrinsic excitability. value never goes < 0
GiVar float32 `default:"0"`
pad, pad1 int32
}
func (ai *ActInitParams) Update() {
}
func (ai *ActInitParams) Defaults() {
ai.Vm = 0.3
ai.Act = 0
ai.GeBase = 0
ai.GiBase = 0
ai.GeVar = 0
ai.GiVar = 0
}
//gosl:end act
// GeBase returns the baseline Ge value: Ge + rand(GeVar) > 0
func (ai *ActInitParams) GetGeBase(rnd randx.Rand) float32 {
ge := ai.GeBase
if ai.GeVar > 0 {
ge += float32(float64(ai.GeVar) * rnd.NormFloat64())
if ge < 0 {
ge = 0
}
}
return ge
}
// GiBase returns the baseline Gi value: Gi + rand(GiVar) > 0
func (ai *ActInitParams) GetGiBase(rnd randx.Rand) float32 {
gi := ai.GiBase
if ai.GiVar > 0 {
gi += float32(float64(ai.GiVar) * rnd.NormFloat64())
if gi < 0 {
gi = 0
}
}
return gi
}
//gosl:start act
//////////////////////////////////////////////////////////////////////////////////////
// DecayParams
// DecayParams control the decay of activation state in the DecayState function
// called in NewState when a new state is to be processed.
type DecayParams struct {
// proportion to decay most activation state variables toward initial values at start of every ThetaCycle (except those controlled separately below) -- if 1 it is effectively equivalent to full clear, resetting other derived values. ISI is reset every AlphaCycle to get a fresh sample of activations (doesn't affect direct computation -- only readout).
Act float32 `default:"0,0.2,0.5,1" max:"1" min:"0"`
// proportion to decay long-lasting conductances, NMDA and GABA, and also the dendritic membrane potential -- when using random stimulus order, it is important to decay this significantly to allow a fresh start -- but set Act to 0 to enable ongoing activity to keep neurons in their sensitive regime.
Glong float32 `default:"0,0.6" max:"1" min:"0"`
// decay of afterhyperpolarization currents, including mAHP, sAHP, and KNa, Kir -- has a separate decay because often useful to have this not decay at all even if decay is on.
AHP float32 `default:"0" max:"1" min:"0"`
// decay of Ca variables driven by spiking activity used in learning: CaSpk* and Ca* variables. These are typically not decayed but may need to be in some situations.
LearnCa float32 `default:"0" max:"1" min:"0"`
// decay layer at end of ThetaCycle when there is a global reward -- true by default for PTPred, PTMaint and PFC Super layers
OnRew slbool.Bool
pad, pad1, pad2 float32
}
func (dp *DecayParams) Update() {
}
func (dp *DecayParams) Defaults() {
dp.Act = 0.2
dp.Glong = 0.6
dp.AHP = 0
dp.LearnCa = 0
}
//////////////////////////////////////////////////////////////////////////////////////
// DtParams
// DtParams are time and rate constants for temporal derivatives in Axon (Vm, G)
type DtParams struct {
// overall rate constant for numerical integration, for all equations at the unit level -- all time constants are specified in millisecond units, with one cycle = 1 msec -- if you instead want to make one cycle = 2 msec, you can do this globally by setting this integ value to 2 (etc). However, stability issues will likely arise if you go too high. For improved numerical stability, you may even need to reduce this value to 0.5 or possibly even lower (typically however this is not necessary). MUST also coordinate this with network.time_inc variable to ensure that global network.time reflects simulated time accurately
Integ float32 `default:"1,0.5" min:"0"`
// membrane potential time constant in cycles, which should be milliseconds typically (tau is roughly how long it takes for value to change significantly -- 1.4x the half-life) -- reflects the capacitance of the neuron in principle -- biological default for AdEx spiking model C = 281 pF = 2.81 normalized
VmTau float32 `default:"2.81" min:"1"`
// dendritic membrane potential time constant in cycles, which should be milliseconds typically (tau is roughly how long it takes for value to change significantly -- 1.4x the half-life) -- reflects the capacitance of the neuron in principle -- biological default for AdEx spiking model C = 281 pF = 2.81 normalized
VmDendTau float32 `default:"5" min:"1"`
// number of integration steps to take in computing new Vm value -- this is the one computation that can be most numerically unstable so taking multiple steps with proportionally smaller dt is beneficial
VmSteps int32 `default:"2" min:"1"`
// time constant for decay of excitatory AMPA receptor conductance.
GeTau float32 `default:"5" min:"1"`
// time constant for decay of inhibitory GABAa receptor conductance.
GiTau float32 `default:"7" min:"1"`
// time constant for integrating values over timescale of an individual input state (e.g., roughly 200 msec -- theta cycle), used in computing ActInt, GeInt from Ge, and GiInt from GiSyn -- this is used for scoring performance, not for learning, in cycles, which should be milliseconds typically (tau is roughly how long it takes for value to change significantly -- 1.4x the half-life),
IntTau float32 `default:"40" min:"1"`
// time constant for integrating slower long-time-scale averages, such as nrn.ActAvg, Pool.ActsMAvg, ActsPAvg -- computed in NewState when a new input state is present (i.e., not msec but in units of a theta cycle) (tau is roughly how long it takes for value to change significantly) -- set lower for smaller models
LongAvgTau float32 `default:"20" min:"1"`
// cycle to start updating the SpkMaxCa, SpkMax values within a theta cycle -- early cycles often reflect prior state
MaxCycStart int32 `default:"10" min:"0"`
// nominal rate = Integ / tau
VmDt float32 `display:"-" json:"-" xml:"-"`
// nominal rate = Integ / tau
VmDendDt float32 `display:"-" json:"-" xml:"-"`
// 1 / VmSteps
DtStep float32 `display:"-" json:"-" xml:"-"`
// rate = Integ / tau
GeDt float32 `display:"-" json:"-" xml:"-"`
// rate = Integ / tau
GiDt float32 `display:"-" json:"-" xml:"-"`
// rate = Integ / tau
IntDt float32 `display:"-" json:"-" xml:"-"`
// rate = 1 / tau
LongAvgDt float32 `display:"-" json:"-" xml:"-"`
}
func (dp *DtParams) Update() {
if dp.VmSteps < 1 {
dp.VmSteps = 1
}
dp.VmDt = dp.Integ / dp.VmTau
dp.VmDendDt = dp.Integ / dp.VmDendTau
dp.DtStep = 1 / float32(dp.VmSteps)
dp.GeDt = dp.Integ / dp.GeTau
dp.GiDt = dp.Integ / dp.GiTau
dp.IntDt = dp.Integ / dp.IntTau
dp.LongAvgDt = 1 / dp.LongAvgTau
}
func (dp *DtParams) Defaults() {
dp.Integ = 1
dp.VmTau = 2.81
dp.VmDendTau = 5
dp.VmSteps = 2
dp.GeTau = 5
dp.GiTau = 7
dp.IntTau = 40
dp.LongAvgTau = 20
dp.MaxCycStart = 10
dp.Update()
}
// GeSynFromRaw integrates a synaptic conductance from raw spiking using GeTau
func (dp *DtParams) GeSynFromRaw(geSyn, geRaw float32) float32 {
return geSyn + geRaw - dp.GeDt*geSyn
}
// GeSynFromRawSteady returns the steady-state GeSyn that would result from
// receiving a steady increment of GeRaw every time step = raw * GeTau.
// dSyn = Raw - dt*Syn; solve for dSyn = 0 to get steady state:
// dt*Syn = Raw; Syn = Raw / dt = Raw * Tau
func (dp *DtParams) GeSynFromRawSteady(geRaw float32) float32 {
return geRaw * dp.GeTau
}
// GiSynFromRaw integrates a synaptic conductance from raw spiking using GiTau
func (dp *DtParams) GiSynFromRaw(giSyn, giRaw float32) float32 {
return giSyn + giRaw - dp.GiDt*giSyn
}
// GiSynFromRawSteady returns the steady-state GiSyn that would result from
// receiving a steady increment of GiRaw every time step = raw * GiTau.
// dSyn = Raw - dt*Syn; solve for dSyn = 0 to get steady state:
// dt*Syn = Raw; Syn = Raw / dt = Raw * Tau
func (dp *DtParams) GiSynFromRawSteady(giRaw float32) float32 {
return giRaw * dp.GiTau
}
// AvgVarUpdate updates the average and variance from current value, using LongAvgDt
func (dp *DtParams) AvgVarUpdate(avg, vr *float32, val float32) {
if *avg == 0 { // first time -- set
*avg = val
*vr = 0
} else {
del := val - *avg
incr := dp.LongAvgDt * del
*avg += incr
// following is magic exponentially weighted incremental variance formula
// derived by Finch, 2009: Incremental calculation of weighted mean and variance
if *vr == 0 {
*vr = 2 * (1 - dp.LongAvgDt) * del * incr
} else {
*vr = (1 - dp.LongAvgDt) * (*vr + del*incr)
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Noise
// SpikeNoiseParams parameterizes background spiking activity impinging on the neuron,
// simulated using a poisson spiking process.
type SpikeNoiseParams struct {
// add noise simulating background spiking levels
On slbool.Bool
// mean frequency of excitatory spikes -- typically 50Hz but multiple inputs increase rate -- poisson lambda parameter, also the variance
GeHz float32 `default:"100"`
// excitatory conductance per spike -- .001 has minimal impact, .01 can be strong, and .15 is needed to influence timing of clamped inputs
Ge float32 `min:"0"`
// mean frequency of inhibitory spikes -- typically 100Hz fast spiking but multiple inputs increase rate -- poisson lambda parameter, also the variance
GiHz float32 `default:"200"`
// excitatory conductance per spike -- .001 has minimal impact, .01 can be strong, and .15 is needed to influence timing of clamped inputs
Gi float32 `min:"0"`
// add Ge noise to GeMaintRaw instead of standard Ge -- used for PTMaintLayer for example
MaintGe slbool.Bool
// Exp(-Interval) which is the threshold for GeNoiseP as it is updated
GeExpInt float32 `display:"-" json:"-" xml:"-"`
// Exp(-Interval) which is the threshold for GiNoiseP as it is updated
GiExpInt float32 `display:"-" json:"-" xml:"-"`
}
func (an *SpikeNoiseParams) Update() {
an.GeExpInt = math32.Exp(-1000.0 / an.GeHz)
an.GiExpInt = math32.Exp(-1000.0 / an.GiHz)
}
func (an *SpikeNoiseParams) Defaults() {
an.GeHz = 100
an.Ge = 0.001
an.GiHz = 200
an.Gi = 0.001
an.Update()
}
func (an *SpikeNoiseParams) ShouldShow(field string) bool {
switch field {
case "On":
return true
default:
return an.On.IsTrue()
}
}
// PGe updates the GeNoiseP probability, multiplying a uniform random number [0-1]
// and returns Ge from spiking if a spike is triggered
func (an *SpikeNoiseParams) PGe(ctx *Context, p *float32, ni, di uint32) float32 {
ndi := di*ctx.NetIndexes.NNeurons + ni
*p *= GetRandomNumber(ndi, ctx.RandCtr, RandFunActPGe)
if *p <= an.GeExpInt {
*p = 1
return an.Ge
}
return 0
}
// PGi updates the GiNoiseP probability, multiplying a uniform random number [0-1]
// and returns Gi from spiking if a spike is triggered
func (an *SpikeNoiseParams) PGi(ctx *Context, p *float32, ni, di uint32) float32 {
ndi := di*ctx.NetIndexes.NNeurons + ni
*p *= GetRandomNumber(ndi, ctx.RandCtr, RandFunActPGi)
if *p <= an.GiExpInt {
*p = 1
return an.Gi
}
return 0
}
//////////////////////////////////////////////////////////////////////////////////////
// ClampParams
// ClampParams specify how external inputs drive excitatory conductances
// (like a current clamp) -- either adds or overwrites existing conductances.
// Noise is added in either case.
type ClampParams struct {
// is this a clamped input layer? set automatically based on layer type at initialization
IsInput slbool.Bool `edit:"-"`
// is this a target layer? set automatically based on layer type at initialization
IsTarget slbool.Bool `edit:"-"`
// amount of Ge driven for clamping -- generally use 0.8 for Target layers, 1.5 for Input layers
Ge float32 `default:"0.8,1.5"`
// add external conductance on top of any existing -- generally this is not a good idea for target layers (creates a main effect that learning can never match), but may be ok for input layers
Add slbool.Bool `default:"false"`
// threshold on neuron Act activity to count as active for computing error relative to target in PctErr method
ErrThr float32 `default:"0.5"`
pad, pad1, pad2 float32
}
func (cp *ClampParams) Update() {
}
func (cp *ClampParams) Defaults() {
cp.Ge = 0.8
cp.ErrThr = 0.5
}
//////////////////////////////////////////////////////////////////////////////////////
// SMaintParams
// SMaintParams for self-maintenance simulating a population of
// NMDA-interconnected spiking neurons
type SMaintParams struct {
// is self maintenance active?
On slbool.Bool
// number of neurons within the self-maintenance pool,
// each of which is assumed to have the same probability of spiking
NNeurons float32 `default:"10"`
// conductance multiplier for self maintenance synapses
Gbar float32 `default:"0.2"`
// inhib controls how much of the extra maintenance conductance goes to the GeExt, which drives extra proportional inhibition
Inhib float32 `default:"1"`
// ISI (inter spike interval) range -- min is used as min ISIAvg for poisson spike rate expected from the population, and above max, no additional maintenance conductance is added
ISI minmax.F32 `display:"inline"`
}
func (sm *SMaintParams) Defaults() {
sm.NNeurons = 10
sm.ISI.Set(1, 20)
sm.Gbar = 0.2
sm.Inhib = 1
}
func (sm *SMaintParams) Update() {
}
func (sm *SMaintParams) ShouldShow(field string) bool {
switch field {
case "On":
return true
default:
return sm.On.IsTrue()
}
}
// ExpInt returns the exponential interval value for determining
// when the next excitatory spike will arrive, based on given ISI
// value for this neuron.
func (sm *SMaintParams) ExpInt(isi float32) float32 {
if isi <= 0 {
return 0
}
isi = max(isi, sm.ISI.Min)
return math32.FastExp(-isi / sm.NNeurons)
}
//////////////////////////////////////////////////////////////////////////////////////
// PopCodeParams
// PopCodeParams provides an encoding of scalar value using population code,
// where a single continuous (scalar) value is encoded as a gaussian bump
// across a population of neurons (1 dimensional).
// It can also modulate rate code and number of neurons active according to the value.
// This is for layers that represent values as in the Rubicon system (from Context.Rubicon).
// Both normalized activation values (1 max) and Ge conductance values can be generated.
type PopCodeParams struct {
// use popcode encoding of variable(s) that this layer represents
On slbool.Bool
// Ge multiplier for driving excitatory conductance based on PopCode -- multiplies normalized activation values
Ge float32 `default:"0.1"`
// minimum value representable -- for GaussBump, typically include extra to allow mean with activity on either side to represent the lowest value you want to encode
Min float32 `default:"-0.1"`
// maximum value representable -- for GaussBump, typically include extra to allow mean with activity on either side to represent the lowest value you want to encode
Max float32 `default:"1.1"`
// activation multiplier for values at Min end of range, where values at Max end have an activation of 1 -- if this is < 1, then there is a rate code proportional to the value in addition to the popcode pattern -- see also MinSigma, MaxSigma
MinAct float32 `default:"1,0.5"`
// sigma parameter of a gaussian specifying the tuning width of the coarse-coded units, in normalized 0-1 range -- for Min value -- if MinSigma < MaxSigma then more units are activated for Max values vs. Min values, proportionally
MinSigma float32 `default:"0.1,0.08"`
// sigma parameter of a gaussian specifying the tuning width of the coarse-coded units, in normalized 0-1 range -- for Min value -- if MinSigma < MaxSigma then more units are activated for Max values vs. Min values, proportionally
MaxSigma float32 `default:"0.1,0.12"`
// ensure that encoded and decoded value remains within specified range
Clip slbool.Bool
}
func (pc *PopCodeParams) Defaults() {
pc.Ge = 0.1
pc.Min = -0.1
pc.Max = 1.1
pc.MinAct = 1
pc.MinSigma = 0.1
pc.MaxSigma = 0.1
pc.Clip.SetBool(true)
}
func (pc *PopCodeParams) Update() {
}
func (pc *PopCodeParams) ShouldShow(field string) bool {
switch field {
case "On":
return true
default:
return pc.On.IsTrue()
}
}
// SetRange sets the min, max and sigma values
func (pc *PopCodeParams) SetRange(min, max, minSigma, maxSigma float32) {
pc.Min = min
pc.Max = max
pc.MinSigma = minSigma
pc.MaxSigma = maxSigma
}
// ClipVal returns clipped (clamped) value in min / max range
func (pc *PopCodeParams) ClipValue(val float32) float32 {
clipVal := val
if clipVal < pc.Min {
clipVal = pc.Min
}
if clipVal > pc.Max {
clipVal = pc.Max
}
return clipVal
}
// ProjectParam projects given min / max param value onto val within range
func (pc *PopCodeParams) ProjectParam(minParam, maxParam, clipVal float32) float32 {
normVal := (clipVal - pc.Min) / (pc.Max - pc.Min)
return minParam + normVal*(maxParam-minParam)
}
// EncodeValue returns value for given value, for neuron index i
// out of n total neurons. n must be 2 or more.
func (pc *PopCodeParams) EncodeValue(i, n uint32, val float32) float32 {
clipVal := pc.ClipValue(val)
if pc.Clip.IsTrue() {
val = clipVal
}
rng := pc.Max - pc.Min
act := float32(1)
if pc.MinAct < 1 {
act = pc.ProjectParam(pc.MinAct, 1.0, clipVal)
}
sig := pc.MinSigma
if pc.MaxSigma > pc.MinSigma {
sig = pc.ProjectParam(pc.MinSigma, pc.MaxSigma, clipVal)
}
gnrm := 1.0 / (rng * sig)
incr := rng / float32(n-1)
trg := pc.Min + incr*float32(i)
dist := gnrm * (trg - val)
return act * math32.FastExp(-(dist * dist))
}
// EncodeGe returns Ge value for given value, for neuron index i
// out of n total neurons. n must be 2 or more.
func (pc *PopCodeParams) EncodeGe(i, n uint32, val float32) float32 {
return pc.Ge * pc.EncodeValue(i, n, val)
}
//////////////////////////////////////////////////////////////////////////////////////
// ActParams
// axon.ActParams contains all the activation computation params and functions
// for basic Axon, at the neuron level .
// This is included in axon.Layer to drive the computation.
type ActParams struct {
// Spiking function parameters
Spikes SpikeParams `display:"inline"`
// dendrite-specific parameters
Dend DendParams `display:"inline"`
// initial values for key network state variables -- initialized in InitActs called by InitWts, and provides target values for DecayState
Init ActInitParams `display:"inline"`
// amount to decay between AlphaCycles, simulating passage of time and effects of saccades etc, especially important for environments with random temporal structure (e.g., most standard neural net training corpora)
Decay DecayParams `display:"inline"`
// time and rate constants for temporal derivatives / updating of activation state
Dt DtParams `display:"inline"`
// maximal conductances levels for channels
Gbar chans.Chans `display:"inline"`
// reversal potentials for each channel
Erev chans.Chans `display:"inline"`
// how external inputs drive neural activations
Clamp ClampParams `display:"inline"`
// how, where, when, and how much noise to add
Noise SpikeNoiseParams `display:"inline"`
// range for Vm membrane potential -- -- important to keep just at extreme range of reversal potentials to prevent numerical instability
VmRange minmax.F32 `display:"inline"`
// M-type medium time-scale afterhyperpolarization mAHP current -- this is the primary form of adaptation on the time scale of multiple sequences of spikes
Mahp chans.MahpParams `display:"inline"`
// slow time-scale afterhyperpolarization sAHP current -- integrates CaSpkD at theta cycle intervals and produces a hard cutoff on sustained activity for any neuron
Sahp chans.SahpParams `display:"inline"`
// sodium-gated potassium channel adaptation parameters -- activates a leak-like current as a function of neural activity (firing = Na influx) at two different time-scales (Slick = medium, Slack = slow)
KNa chans.KNaMedSlow `display:"inline"`
// potassium (K) inwardly rectifying (ir) current, which is similar to GABAB
// (which is a GABA modulated Kir channel). This channel is off by default
// but plays a critical role in making medium spiny neurons (MSNs) relatively
// quiet in the striatum.
Kir chans.KirParams `display:"inline"`
// NMDA channel parameters used in computing Gnmda conductance for bistability, and postsynaptic calcium flux used in learning. Note that Learn.Snmda has distinct parameters used in computing sending NMDA parameters used in learning.
NMDA chans.NMDAParams `display:"inline"`
// NMDA channel parameters used in computing Gnmda conductance for bistability, and postsynaptic calcium flux used in learning. Note that Learn.Snmda has distinct parameters used in computing sending NMDA parameters used in learning.
MaintNMDA chans.NMDAParams `display:"inline"`
// GABA-B / GIRK channel parameters
GabaB chans.GABABParams `display:"inline"`
// voltage gated calcium channels -- provide a key additional source of Ca for learning and positive-feedback loop upstate for active neurons
VGCC chans.VGCCParams `display:"inline"`
// A-type potassium (K) channel that is particularly important for limiting the runaway excitation from VGCC channels
AK chans.AKsParams `display:"inline"`
// small-conductance calcium-activated potassium channel produces the pausing function as a consequence of rapid bursting.
SKCa chans.SKCaParams `display:"inline"`
// for self-maintenance simulating a population of
// NMDA-interconnected spiking neurons
SMaint SMaintParams `display:"inline"`
// provides encoding population codes, used to represent a single continuous (scalar) value, across a population of units / neurons (1 dimensional)
PopCode PopCodeParams `display:"inline"`
}
func (ac *ActParams) Defaults() {
ac.Spikes.Defaults()
ac.Dend.Defaults()
ac.Init.Defaults()
ac.Decay.Defaults()
ac.Dt.Defaults()
ac.Gbar.SetAll(1.0, 0.2, 1.0, 1.0) // E, L, I, K: gbar l = 0.2 > 0.1
ac.Erev.SetAll(1.0, 0.3, 0.1, 0.1) // E, L, I, K: K = hyperpolarized -90mv
ac.Clamp.Defaults()
ac.Noise.Defaults()
ac.VmRange.Set(0.1, 1.0)
ac.Mahp.Defaults()
ac.Mahp.Gbar = 0.02
ac.Sahp.Defaults()
ac.Sahp.Gbar = 0.05
ac.Sahp.CaTau = 5
ac.KNa.Defaults()
ac.KNa.On.SetBool(true)
ac.Kir.Defaults()
ac.Kir.Gbar = 0
ac.NMDA.Defaults()
ac.NMDA.Gbar = 0.006
ac.MaintNMDA.Defaults()
ac.MaintNMDA.Gbar = 0.007
ac.MaintNMDA.Tau = 200
ac.GabaB.Defaults()
ac.VGCC.Defaults()
ac.VGCC.Gbar = 0.02
ac.VGCC.Ca = 25
ac.AK.Defaults()
ac.AK.Gbar = 0.1
ac.SKCa.Defaults()
ac.SKCa.Gbar = 0
ac.SMaint.Defaults()
ac.PopCode.Defaults()
ac.Update()
}
// Update must be called after any changes to parameters
func (ac *ActParams) Update() {
ac.Spikes.Update()
ac.Dend.Update()
ac.Init.Update()
ac.Decay.Update()
ac.Dt.Update()
ac.Clamp.Update()
ac.Noise.Update()
ac.Mahp.Update()
ac.Sahp.Update()
ac.KNa.Update()
ac.Kir.Update()
ac.NMDA.Update()
ac.MaintNMDA.Update()
ac.GabaB.Update()
ac.VGCC.Update()
ac.AK.Update()
ac.SKCa.Update()
ac.SMaint.Update()
ac.PopCode.Update()
}
///////////////////////////////////////////////////////////////////////
// Init
// DecayLearnCa decays neuron-level calcium learning and spiking variables
// by given factor. Note: this is generally NOT useful,
// causing variability in these learning factors as a function
// of the decay parameter that then has impacts on learning rates etc.
// see Act.Decay.LearnCa param controlling this
func (ac *ActParams) DecayLearnCa(ctx *Context, ni, di uint32, decay float32) {
AddNrnV(ctx, ni, di, GnmdaLrn, -decay*NrnV(ctx, ni, di, GnmdaLrn))
AddNrnV(ctx, ni, di, NmdaCa, -decay*NrnV(ctx, ni, di, NmdaCa))
AddNrnV(ctx, ni, di, VgccCa, -decay*NrnV(ctx, ni, di, VgccCa))
AddNrnV(ctx, ni, di, VgccCaInt, -decay*NrnV(ctx, ni, di, VgccCaInt))
AddNrnV(ctx, ni, di, CaLrn, -decay*NrnV(ctx, ni, di, CaLrn))
AddNrnV(ctx, ni, di, CaSpkM, -decay*NrnV(ctx, ni, di, CaSpkM))
AddNrnV(ctx, ni, di, CaSpkP, -decay*NrnV(ctx, ni, di, CaSpkP))
AddNrnV(ctx, ni, di, CaSpkD, -decay*NrnV(ctx, ni, di, CaSpkD))
AddNrnV(ctx, ni, di, NrnCaM, -decay*NrnV(ctx, ni, di, NrnCaM))
AddNrnV(ctx, ni, di, NrnCaP, -decay*NrnV(ctx, ni, di, NrnCaP))
AddNrnV(ctx, ni, di, NrnCaD, -decay*NrnV(ctx, ni, di, NrnCaD))
AddNrnV(ctx, ni, di, SKCaIn, decay*(1.0-NrnV(ctx, ni, di, SKCaIn))) // recovers
AddNrnV(ctx, ni, di, SKCaR, -decay*NrnV(ctx, ni, di, SKCaR))
AddNrnV(ctx, ni, di, SKCaM, -decay*NrnV(ctx, ni, di, SKCaM))
}
// DecayAHP decays after-hyperpolarization variables
// by given factor (typically Decay.AHP)
func (ac *ActParams) DecayAHP(ctx *Context, ni, di uint32, decay float32) {
AddNrnV(ctx, ni, di, MahpN, -decay*NrnV(ctx, ni, di, MahpN))
AddNrnV(ctx, ni, di, Gmahp, -decay*NrnV(ctx, ni, di, Gmahp))
AddNrnV(ctx, ni, di, SahpCa, -decay*NrnV(ctx, ni, di, SahpCa))
AddNrnV(ctx, ni, di, SahpN, -decay*NrnV(ctx, ni, di, SahpN))
AddNrnV(ctx, ni, di, Gsahp, -decay*NrnV(ctx, ni, di, Gsahp))
AddNrnV(ctx, ni, di, GknaMed, -decay*NrnV(ctx, ni, di, GknaMed))
AddNrnV(ctx, ni, di, GknaSlow, -decay*NrnV(ctx, ni, di, GknaSlow))
kirMrest := ac.Kir.Mrest
AddNrnV(ctx, ni, di, KirM, decay*(kirMrest-NrnV(ctx, ni, di, KirM)))
AddNrnV(ctx, ni, di, Gkir, -decay*NrnV(ctx, ni, di, Gkir))
}
// DecayState decays the activation state toward initial values
// in proportion to given decay parameter. Special case values
// such as Glong and KNa are also decayed with their
// separately parameterized values.
// Called with ac.Decay.Act by Layer during NewState
func (ac *ActParams) DecayState(ctx *Context, ni, di uint32, decay, glong, ahp float32) {
// always reset these -- otherwise get insanely large values that take forever to update
SetNrnV(ctx, ni, di, ISIAvg, -1)
SetNrnV(ctx, ni, di, ActInt, ac.Init.Act) // start fresh
SetNrnV(ctx, ni, di, Spiked, 0) // always fresh
if decay > 0 { // no-op for most, but not all..
SetNrnV(ctx, ni, di, Spike, 0)
AddNrnV(ctx, ni, di, Act, -decay*(NrnV(ctx, ni, di, Act)-ac.Init.Act))
AddNrnV(ctx, ni, di, ActInt, -decay*(NrnV(ctx, ni, di, ActInt)-ac.Init.Act))
AddNrnV(ctx, ni, di, GeSyn, -decay*(NrnV(ctx, ni, di, GeSyn)-NrnAvgV(ctx, ni, GeBase)))
AddNrnV(ctx, ni, di, Ge, -decay*(NrnV(ctx, ni, di, Ge)-NrnAvgV(ctx, ni, GeBase)))
AddNrnV(ctx, ni, di, Gi, -decay*(NrnV(ctx, ni, di, Gi)-NrnAvgV(ctx, ni, GiBase)))
AddNrnV(ctx, ni, di, Gk, -decay*NrnV(ctx, ni, di, Gk))
AddNrnV(ctx, ni, di, Vm, -decay*(NrnV(ctx, ni, di, Vm)-ac.Init.Vm))
AddNrnV(ctx, ni, di, GeNoise, -decay*NrnV(ctx, ni, di, GeNoise))
AddNrnV(ctx, ni, di, GiNoise, -decay*NrnV(ctx, ni, di, GiNoise))
AddNrnV(ctx, ni, di, GiSyn, -decay*NrnV(ctx, ni, di, GiSyn))
AddNrnV(ctx, ni, di, GeInt, -decay*NrnV(ctx, ni, di, GeInt))
AddNrnV(ctx, ni, di, GiInt, -decay*NrnV(ctx, ni, di, GiInt))
AddNrnV(ctx, ni, di, GeIntNorm, -decay*NrnV(ctx, ni, di, GeIntNorm))
}
AddNrnV(ctx, ni, di, VmDend, -glong*(NrnV(ctx, ni, di, VmDend)-ac.Init.Vm))
if ahp > 0 {
ac.DecayAHP(ctx, ni, di, ahp)
}
AddNrnV(ctx, ni, di, GgabaB, -glong*NrnV(ctx, ni, di, GgabaB))
AddNrnV(ctx, ni, di, GABAB, -glong*NrnV(ctx, ni, di, GABAB))
AddNrnV(ctx, ni, di, GABABx, -glong*NrnV(ctx, ni, di, GABABx))
AddNrnV(ctx, ni, di, GnmdaSyn, -glong*NrnV(ctx, ni, di, GnmdaSyn))
AddNrnV(ctx, ni, di, Gnmda, -glong*NrnV(ctx, ni, di, Gnmda))
AddNrnV(ctx, ni, di, GMaintSyn, -glong*NrnV(ctx, ni, di, GMaintSyn))
AddNrnV(ctx, ni, di, GnmdaMaint, -glong*NrnV(ctx, ni, di, GnmdaMaint))
AddNrnV(ctx, ni, di, Gvgcc, -glong*NrnV(ctx, ni, di, Gvgcc))
AddNrnV(ctx, ni, di, VgccM, -glong*NrnV(ctx, ni, di, VgccM))
AddNrnV(ctx, ni, di, VgccH, -glong*NrnV(ctx, ni, di, VgccH))
AddNrnV(ctx, ni, di, Gak, -glong*NrnV(ctx, ni, di, Gak))
// don't mess with SKCa -- longer time scale
AddNrnV(ctx, ni, di, Gsk, -glong*NrnV(ctx, ni, di, Gsk))
if ac.Decay.LearnCa > 0 { // learning-based Ca values -- not usual
ac.DecayLearnCa(ctx, ni, di, ac.Decay.LearnCa)
}
SetNrnV(ctx, ni, di, Inet, 0)
SetNrnV(ctx, ni, di, GeRaw, 0)
SetNrnV(ctx, ni, di, GiRaw, 0)
SetNrnV(ctx, ni, di, GModRaw, 0)
SetNrnV(ctx, ni, di, GModSyn, 0)
SetNrnV(ctx, ni, di, GMaintRaw, 0)
SetNrnV(ctx, ni, di, SSGi, 0)
SetNrnV(ctx, ni, di, SSGiDend, 0)
SetNrnV(ctx, ni, di, GeExt, 0)
AddNrnV(ctx, ni, di, CtxtGeOrig, -glong*NrnV(ctx, ni, di, CtxtGeOrig))
}
//gosl:end act
// InitActs initializes activation state in neuron -- called during InitWts but otherwise not
// automatically called (DecayState is used instead)
func (ac *ActParams) InitActs(ctx *Context, ni, di uint32) {
SetNrnV(ctx, ni, di, Spike, 0)
SetNrnV(ctx, ni, di, Spiked, 0)
SetNrnV(ctx, ni, di, ISI, -1)
SetNrnV(ctx, ni, di, ISIAvg, -1)
SetNrnV(ctx, ni, di, Act, ac.Init.Act)
SetNrnV(ctx, ni, di, ActInt, ac.Init.Act)
SetNrnV(ctx, ni, di, GeSyn, NrnAvgV(ctx, ni, GeBase))
SetNrnV(ctx, ni, di, Ge, NrnAvgV(ctx, ni, GeBase))
SetNrnV(ctx, ni, di, Gi, NrnAvgV(ctx, ni, GiBase))
SetNrnV(ctx, ni, di, Gk, 0)
SetNrnV(ctx, ni, di, Inet, 0)
SetNrnV(ctx, ni, di, Vm, ac.Init.Vm)
SetNrnV(ctx, ni, di, VmDend, ac.Init.Vm)
SetNrnV(ctx, ni, di, Target, 0)
SetNrnV(ctx, ni, di, Ext, 0)
SetNrnV(ctx, ni, di, SpkMaxCa, 0)
SetNrnV(ctx, ni, di, SpkMax, 0)
SetNrnV(ctx, ni, di, RLRate, 1)
SetNrnV(ctx, ni, di, GeNoiseP, 1)
SetNrnV(ctx, ni, di, GeNoise, 0)
SetNrnV(ctx, ni, di, GiNoiseP, 1)
SetNrnV(ctx, ni, di, GiNoise, 0)
SetNrnV(ctx, ni, di, GiSyn, 0)
SetNrnV(ctx, ni, di, SMaintP, 1)
SetNrnV(ctx, ni, di, GeInt, 0)
SetNrnV(ctx, ni, di, GeIntNorm, 0)
SetNrnV(ctx, ni, di, GiInt, 0)
SetNrnV(ctx, ni, di, MahpN, 0)
SetNrnV(ctx, ni, di, Gmahp, 0)
SetNrnV(ctx, ni, di, SahpCa, 0)
SetNrnV(ctx, ni, di, SahpN, 0)
SetNrnV(ctx, ni, di, Gsahp, 0)
SetNrnV(ctx, ni, di, GknaMed, 0)
SetNrnV(ctx, ni, di, GknaSlow, 0)
SetNrnV(ctx, ni, di, KirM, ac.Kir.Mrest)
SetNrnV(ctx, ni, di, Gkir, 0)
SetNrnV(ctx, ni, di, GnmdaSyn, 0)
SetNrnV(ctx, ni, di, Gnmda, 0)
SetNrnV(ctx, ni, di, GnmdaMaint, 0)
SetNrnV(ctx, ni, di, GnmdaLrn, 0)
SetNrnV(ctx, ni, di, NmdaCa, 0)
SetNrnV(ctx, ni, di, GgabaB, 0)
SetNrnV(ctx, ni, di, GABAB, 0)
SetNrnV(ctx, ni, di, GABABx, 0)
SetNrnV(ctx, ni, di, Gvgcc, 0)