-
Notifications
You must be signed in to change notification settings - Fork 7
/
layer.go
789 lines (726 loc) · 23.1 KB
/
layer.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
// Code generated by "goal build"; DO NOT EDIT.
//line layer.goal:1
// 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 (
"errors"
"fmt"
"io"
"log"
"math/rand"
"strconv"
"strings"
"cogentcore.org/core/core"
"cogentcore.org/core/icons"
"cogentcore.org/core/math32"
"cogentcore.org/core/tree"
"github.com/emer/emergent/v2/emer"
"github.com/emer/emergent/v2/weights"
)
// index naming:
// lni = layer-based neuron index (0 = first neuron in layer)
// ni = absolute whole network neuron index
// Layer implements the basic Axon spiking activation function,
// and manages learning in the pathways.
type Layer struct {
emer.LayerBase
// Params are layer parameters (pointer to item in Network.LayerParams).
Params *LayerParams
// our parent network, in case we need to use it to find
// other layers etc; set when added by network.
Network *Network `copier:"-" json:"-" xml:"-" display:"-"`
// Type is the type of layer, which drives specialized computation as needed.
Type LayerTypes
// NNeurons is the number of neurons in the layer.
NNeurons uint32 `display:"-"`
// NeurStIndex is the starting index of neurons for this layer within
// the global Network list.
NeurStIndex uint32 `display:"-" inactive:"-"`
// NPools is the number of inhibitory pools based on layer shape,
// with the first one representing the entire set of neurons in the layer,
// and 4D shaped layers have sub-pools after that.
NPools uint32 `display:"-"`
// MaxData is the maximum amount of input data that can be processed in
// parallel in one pass of the network (copied from [NetworkIndexes]).
// Neuron, Pool, Values storage is allocated to hold this amount.
MaxData uint32 `display:"-"`
// RecvPaths is the list of receiving pathways into this layer from other layers.
RecvPaths []*Path
// SendPaths is the list of sending pathways from this layer to other layers.
SendPaths []*Path
// BuildConfig has configuration data set when the network is configured,
// that is used during the network Build() process via PostBuild method,
// after all the structure of the network has been fully constructed.
// In particular, the Params is nil until Build, so setting anything
// specific in there (e.g., an index to another layer) must be done
// as a second pass. Note that Params are all applied after Build
// and can set user-modifiable params, so this is for more special
// algorithm structural parameters set during ConfigNet() methods.
BuildConfig map[string]string `table:"-"`
// DefaultParams are closures that apply default parameters
// prior to user-set parameters. These are useful for specific layer
// functionality in specialized brain areas (e.g., Rubicon, BG etc)
// not associated with a layer type, which otherwise is used to hard-code
// initial default parameters.
DefaultParams []func(ly *LayerParams) `display:"-"`
}
// emer.Layer interface methods
func (ly *Layer) TypeName() string { return ly.Type.String() }
func (ly *Layer) TypeNumber() int { return int(ly.Type) }
func (ly *Layer) NumRecvPaths() int { return len(ly.RecvPaths) }
func (ly *Layer) RecvPath(idx int) emer.Path { return ly.RecvPaths[idx] }
func (ly *Layer) NumSendPaths() int { return len(ly.SendPaths) }
func (ly *Layer) SendPath(idx int) emer.Path { return ly.SendPaths[idx] }
func (ly *Layer) Defaults() { //types:add
ctx := ly.Network.Context()
li := ly.Index
if ly.Params != nil {
ly.Params.Type = ly.Type
ly.Params.Defaults()
for di := uint32(0); di < ly.MaxData; di++ {
LayerStates.Set(1, int(li), int(di), int(LayerGiMult))
}
ly.Params.Learn.CaLearn.Dt.PDTauForNCycles(int(ctx.ThetaCycles))
ly.Params.Learn.CaSpk.Dt.PDTauForNCycles(int(ctx.ThetaCycles))
}
for _, pt := range ly.RecvPaths { // must do path defaults first, then custom
pt.Defaults()
}
if ly.Params == nil {
return
}
switch ly.Type {
case InputLayer:
ly.Params.Acts.Clamp.Ge = 1.5
ly.Params.Inhib.Layer.Gi = 0.9
ly.Params.Inhib.Pool.Gi = 0.9
ly.Params.Learn.TrgAvgAct.SubMean = 0
case TargetLayer:
ly.Params.Acts.Clamp.Ge = 0.8
ly.Params.Learn.TrgAvgAct.SubMean = 0
// ly.Params.Learn.RLRate.SigmoidMin = 1
case CTLayer:
ly.Params.CTDefaults()
case PTMaintLayer:
ly.PTMaintDefaults()
case PTPredLayer:
ly.Params.PTPredDefaults()
case PulvinarLayer:
ly.Params.PulvDefaults()
case RewLayer:
ly.Params.RWDefaults()
case RWPredLayer:
ly.Params.RWDefaults()
ly.Params.RWPredDefaults()
case RWDaLayer:
ly.Params.RWDefaults()
case TDPredLayer:
ly.Params.TDDefaults()
ly.Params.TDPredDefaults()
case TDIntegLayer, TDDaLayer:
ly.Params.TDDefaults()
case LDTLayer:
ly.LDTDefaults()
case BLALayer:
ly.BLADefaults()
case CeMLayer:
ly.CeMDefaults()
case VSPatchLayer:
ly.Params.VSPatchDefaults()
case DrivesLayer:
ly.Params.DrivesDefaults()
case UrgencyLayer:
ly.Params.UrgencyDefaults()
case USLayer:
ly.Params.USDefaults()
case PVLayer:
ly.Params.PVDefaults()
case MatrixLayer:
ly.MatrixDefaults()
case GPLayer:
ly.GPDefaults()
case STNLayer:
ly.STNDefaults()
case BGThalLayer:
ly.BGThalDefaults()
case VSGatedLayer:
ly.Params.VSGatedDefaults()
}
ly.Params.CT.DecayForNCycles(int(ctx.ThetaCycles))
ly.applyDefaultParams()
ly.UpdateParams()
}
// Update is an interface for generically updating after edits
// this should be used only for the values on the struct itself.
// UpdateParams is used to update all parameters, including Path.
func (ly *Layer) Update() {
if ly.Params == nil {
return
}
if !ly.Is4D() && ly.Params.Inhib.Pool.On.IsTrue() {
ly.Params.Inhib.Pool.On.SetBool(false)
}
ly.Params.Update()
}
// UpdateParams updates all params given any changes that might
// have been made to individual values including those in the
// receiving pathways of this layer.
// This is not called Update because it is not just about the
// local values in the struct.
func (ly *Layer) UpdateParams() {
ly.Update()
for _, pt := range ly.RecvPaths {
pt.UpdateParams()
}
}
// todo: not standard:
func (ly *Layer) SetOff(off bool) {
ly.Off = off
// a Path is off if either the sending or the receiving layer is off
// or if the path has been set to Off directly
for _, pt := range ly.RecvPaths {
pt.Off = pt.Send.Off || off
}
for _, pt := range ly.SendPaths {
pt.Off = pt.Recv.Off || off
}
}
// RecipToSendPath finds the reciprocal pathway to
// the given sending pathway within the ly layer.
// i.e., where ly is instead the *receiving* layer from same other layer B
// that is the receiver of the spj pathway we're sending to.
//
// ly = A, other layer = B:
//
// spj: S=A -> R=B
// rpj: R=A <- S=B
//
// returns false if not found.
func (ly *Layer) RecipToSendPath(spj *Path) (*Path, bool) {
for _, rpj := range ly.RecvPaths {
if rpj.Send == spj.Recv { // B = sender of rpj, recv of spj
return rpj, true
}
}
return nil, false
}
// RecipToRecvPath finds the reciprocal pathway to
// the given recv pathway within the ly layer.
// i.e., where ly is instead the *sending* layer to same other layer B
// that is the sender of the rpj pathway we're receiving from.
//
// ly = A, other layer = B:
//
// rpj: R=A <- S=B
// spj: S=A -> R=B
//
// returns false if not found.
func (ly *Layer) RecipToRecvPath(rpj *Path) (*Path, bool) {
for _, spj := range ly.SendPaths {
if spj.Recv == rpj.Send { // B = sender of rpj, recv of spj
return spj, true
}
}
return nil, false
}
// AddDefaultParams adds given default param setting function.
func (ly *Layer) AddDefaultParams(fun func(ly *LayerParams)) {
ly.DefaultParams = append(ly.DefaultParams, fun)
}
// applyDefaultParams applies DefaultParams default parameters.
// Called by Layer.Defaults()
func (ly *Layer) applyDefaultParams() {
for _, f := range ly.DefaultParams {
f(ly.Params)
}
}
// AllParams returns a listing of all parameters in the Layer
func (ly *Layer) AllParams() string {
str := "/////////////////////////////////////////////////\nLayer: " + ly.Name + "\n" + ly.Params.AllParams()
for _, pt := range ly.RecvPaths {
str += pt.AllParams()
}
return str
}
//////// Build
// SetBuildConfig sets named configuration parameter to given string value
// to be used in the PostBuild stage -- mainly for layer names that need to be
// looked up and turned into indexes, after entire network is built.
func (ly *Layer) SetBuildConfig(param, val string) {
ly.BuildConfig[param] = val
}
// BuildConfigByName looks for given BuildConfig option by name,
// and reports & returns an error if not found.
func (ly *Layer) BuildConfigByName(nm string) (string, error) {
cfg, ok := ly.BuildConfig[nm]
if !ok {
err := fmt.Errorf("Layer: %s does not have BuildConfig: %s set -- error in ConfigNet", ly.Name, nm)
log.Println(err)
return cfg, err
}
return cfg, nil
}
// BuildConfigFindLayer looks for BuildConfig of given name
// and if found, looks for layer with corresponding name.
// if mustName is true, then an error is logged if the BuildConfig
// name does not exist. An error is always logged if the layer name
// is not found. -1 is returned in any case of not found.
func (ly *Layer) BuildConfigFindLayer(nm string, mustName bool) int32 {
idx := int32(-1)
if rnm, ok := ly.BuildConfig[nm]; ok {
dly := ly.Network.LayerByName(rnm)
if dly != nil {
idx = int32(dly.Index)
}
} else {
if mustName {
err := fmt.Errorf("Layer: %s does not have BuildConfig: %s set -- error in ConfigNet", ly.Name, nm)
log.Println(err)
}
}
return idx
}
// BuildSubPools initializes neuron start / end indexes for sub-pools
func (ly *Layer) BuildSubPools(ctx *Context) {
if !ly.Is4D() {
return
}
sh := ly.Shape.Sizes
spy := sh[0]
spx := sh[1]
spi := uint32(1)
for py := 0; py < spy; py++ {
for px := 0; px < spx; px++ {
soff := int32(ly.Shape.IndexTo1D(py, px, 0, 0))
eoff := int32(ly.Shape.IndexTo1D(py, px, sh[2]-1, sh[3]-1) + 1)
for di := uint32(0); di < ly.MaxData; di++ {
pi := ly.Params.PoolIndex(spi)
PoolsInt.Set(soff, int(pi), int(di), int(PoolNeurSt))
PoolsInt.Set(eoff, int(pi), int(di), int(PoolNeurEd))
}
for lni := soff; lni < eoff; lni++ {
ni := ly.NeurStIndex + uint32(lni)
NeuronIxs.Set(uint32(spi), int(ni), int(NrnSubPool))
}
spi++
}
}
}
// BuildPools builds the inhibitory pools structures -- nu = number of units in layer
func (ly *Layer) BuildPools(ctx *Context, nn uint32) error {
np := 1 + ly.NumPools()
for di := uint32(0); di < ly.MaxData; di++ {
lpi := ly.Params.PoolIndex(0)
PoolsInt.Set(0, int(lpi), int(di), int(PoolNeurSt))
PoolsInt.Set(int32(nn), int(lpi), int(di), int(PoolNeurEd))
PoolsInt.Set(1, int(lpi), int(di), int(PoolIsLayer))
}
if np > 1 {
ly.BuildSubPools(ctx)
}
return nil
}
// BuildPaths builds the pathways, send-side
func (ly *Layer) BuildPaths(ctx *Context) error {
emsg := ""
for _, pt := range ly.SendPaths {
if pt.Off {
continue
}
err := pt.Build()
if err != nil {
emsg += err.Error() + "\n"
}
}
if emsg != "" {
return errors.New(emsg)
}
return nil
}
// Build constructs the layer state, including calling Build on the pathways
func (ly *Layer) Build() error {
ctx := ly.Network.Context()
nn := uint32(ly.Shape.Len())
if nn == 0 {
return fmt.Errorf("Build Layer %v: no units specified in Shape", ly.Name)
}
for lni := uint32(0); lni < nn; lni++ {
ni := ly.NeurStIndex + lni
NeuronIxs.Set(lni, int(ni), int(NrnNeurIndex))
NeuronIxs.Set(uint32(ly.Index), int(ni), int(NrnLayIndex))
}
err := ly.BuildPools(ctx, nn)
if err != nil {
return err
}
err = ly.BuildPaths(ctx)
ly.PostBuild()
return err
}
// PostBuild performs special post-Build() configuration steps for specific algorithms,
// using configuration data set in BuildConfig during the ConfigNet process.
func (ly *Layer) PostBuild() {
ly.Params.LayInhib.Index1 = ly.BuildConfigFindLayer("LayInhib1Name", false) // optional
ly.Params.LayInhib.Index2 = ly.BuildConfigFindLayer("LayInhib2Name", false) // optional
ly.Params.LayInhib.Index3 = ly.BuildConfigFindLayer("LayInhib3Name", false) // optional
ly.Params.LayInhib.Index4 = ly.BuildConfigFindLayer("LayInhib4Name", false) // optional
switch ly.Type {
case PulvinarLayer:
ly.PulvPostBuild()
case LDTLayer:
ly.LDTPostBuild()
case RWDaLayer:
ly.RWDaPostBuild()
case TDIntegLayer:
ly.TDIntegPostBuild()
case TDDaLayer:
ly.TDDaPostBuild()
case BLALayer, CeMLayer, USLayer, PVLayer, VSPatchLayer:
ly.RubiconPostBuild()
case MatrixLayer:
ly.MatrixPostBuild()
case GPLayer:
ly.GPPostBuild()
}
}
// UnitVarNames returns a list of variable names available on the units in this layer
func (ly *Layer) UnitVarNames() []string {
return NeuronVarNames
}
// UnitVarProps returns properties for variables
func (ly *Layer) UnitVarProps() map[string]string {
return NeuronVarProps
}
// UnitVarIndex returns the index of given variable within the Neuron,
// according to *this layer's* UnitVarNames() list (using a map to lookup index),
// or -1 and error message if not found.
func (ly *Layer) UnitVarIndex(varNm string) (int, error) {
return NeuronVarIndexByName(varNm)
}
// UnitVarNum returns the number of Neuron-level variables
// for this layer. This is needed for extending indexes in derived types.
func (ly *Layer) UnitVarNum() int {
return len(NeuronVarNames)
}
// UnitValue1D returns value of given variable index on given unit, using 1-dimensional index.
// returns NaN on invalid index.
// This is the core unit var access method used by other methods,
// so it is the only one that needs to be updated for derived layer types.
func (ly *Layer) UnitValue1D(varIndex int, idx, di int) float32 {
if idx < 0 || idx >= int(ly.NNeurons) {
return math32.NaN()
}
if varIndex < 0 || varIndex >= ly.UnitVarNum() {
return math32.NaN()
}
if di < 0 || di >= int(ly.MaxData) {
return math32.NaN()
}
ni := ly.NeurStIndex + uint32(idx)
nvars := ly.UnitVarNum()
if varIndex >= nvars-NNeuronLayerVars {
lvi := varIndex - (ly.UnitVarNum() - NNeuronLayerVars)
switch lvi {
case 0:
return GlobalScalars.Value(int(GvDA), int(uint32(di)))
case 1:
return GlobalScalars.Value(int(GvACh), int(uint32(di)))
case 2:
return GlobalScalars.Value(int(GvNE), int(uint32(di)))
case 3:
return GlobalScalars.Value(int(GvSer), int(uint32(di)))
case 4:
pi := ly.Params.PoolIndex(NeuronIxs.Value(int(ni), int(NrnSubPool)))
return float32(PoolsInt.Value(int(pi), int(di), int(PoolGated)))
}
} else if NeuronVars(varIndex) >= NeuronVarsN {
return NeuronAvgs.Value(int(ni), int(NeuronVars(varIndex)-NeuronVarsN))
} else {
return Neurons.Value(int(ni), int(di), int(varIndex))
}
return math32.NaN()
}
// RecvPathValues fills in values of given synapse variable name,
// for pathway into given sending layer and neuron 1D index,
// for all receiving neurons in this layer,
// into given float32 slice (only resized if not big enough).
// pathType is the string representation of the path type -- used if non-empty,
// useful when there are multiple pathways between two layers.
// Returns error on invalid var name.
// If the receiving neuron is not connected to the given sending layer or neuron
// then the value is set to math32.NaN().
// Returns error on invalid var name or lack of recv path (vals always set to nan on path err).
func (ly *Layer) RecvPathValues(vals *[]float32, varNm string, sendLay emer.Layer, sendIndex1D int, pathType string) error {
var err error
nn := int(ly.NNeurons)
if *vals == nil || cap(*vals) < nn {
*vals = make([]float32, nn)
} else if len(*vals) < nn {
*vals = (*vals)[0:nn]
}
nan := math32.NaN()
for i := 0; i < nn; i++ {
(*vals)[i] = nan
}
if sendLay == nil {
return fmt.Errorf("sending layer is nil")
}
slay := sendLay.AsEmer()
var pt emer.Path
if pathType != "" {
pt, err = slay.SendPathByRecvNameType(ly.Name, pathType)
if pt == nil {
pt, err = slay.SendPathByRecvName(ly.Name)
}
} else {
pt, err = slay.SendPathByRecvName(ly.Name)
}
if pt == nil {
return err
}
if pt.AsEmer().Off {
return fmt.Errorf("pathway is off")
}
for ri := 0; ri < nn; ri++ {
(*vals)[ri] = pt.AsEmer().SynValue(varNm, sendIndex1D, ri) // this will work with any variable -- slower, but necessary
}
return nil
}
// SendPathValues fills in values of given synapse variable name,
// for pathway into given receiving layer and neuron 1D index,
// for all sending neurons in this layer,
// into given float32 slice (only resized if not big enough).
// pathType is the string representation of the path type -- used if non-empty,
// useful when there are multiple pathways between two layers.
// Returns error on invalid var name.
// If the sending neuron is not connected to the given receiving layer or neuron
// then the value is set to math32.NaN().
// Returns error on invalid var name or lack of recv path (vals always set to nan on path err).
func (ly *Layer) SendPathValues(vals *[]float32, varNm string, recvLay emer.Layer, recvIndex1D int, pathType string) error {
var err error
nn := int(ly.NNeurons)
if *vals == nil || cap(*vals) < nn {
*vals = make([]float32, nn)
} else if len(*vals) < nn {
*vals = (*vals)[0:nn]
}
nan := math32.NaN()
for i := 0; i < nn; i++ {
(*vals)[i] = nan
}
if recvLay == nil {
return fmt.Errorf("receiving layer is nil")
}
rlay := recvLay.AsEmer()
var pt emer.Path
if pathType != "" {
pt, err = rlay.RecvPathBySendNameType(ly.Name, pathType)
if pt == nil {
pt, err = rlay.RecvPathBySendName(ly.Name)
}
} else {
pt, err = rlay.RecvPathBySendName(ly.Name)
}
if pt == nil {
return err
}
if pt.AsEmer().Off {
return fmt.Errorf("pathway is off")
}
for si := 0; si < nn; si++ {
(*vals)[si] = pt.AsEmer().SynValue(varNm, si, recvIndex1D)
}
return nil
}
// VarRange returns the min / max values for given variable
// todo: support r. s. pathway values
// error occurs when variable name is not found.
func (ly *Layer) VarRange(varNm string) (min, max float32, err error) {
nn := ly.NNeurons
if nn == 0 {
return
}
vidx, err := ly.UnitVarIndex(varNm)
if err != nil {
return
}
nvar := vidx
v0 := Neurons.Value(int(ly.NeurStIndex), int(0), int(nvar))
min = v0
max = v0
for lni := uint32(1); lni < nn; lni++ {
ni := ly.NeurStIndex + lni
vl := Neurons.Value(int(ni), int(0), int(nvar))
if vl < min {
min = vl
}
if vl > max {
max = vl
}
}
return
}
//////// Weights
// WriteWeightsJSON writes the weights from this layer from the receiver-side perspective
// in a JSON text format. We build in the indentation logic to make it much faster and
// more efficient.
func (ly *Layer) WriteWeightsJSON(w io.Writer, depth int) {
li := ly.Index
ly.MetaData = make(map[string]string)
ly.MetaData["ActMAvg"] = fmt.Sprintf("%g", LayerStates.Value(int(li), int(0), int(LayerActMAvg)))
ly.MetaData["ActPAvg"] = fmt.Sprintf("%g", LayerStates.Value(int(li), int(0), int(LayerActPAvg)))
ly.MetaData["GiMult"] = fmt.Sprintf("%g", LayerStates.Value(int(li), int(0), int(LayerGiMult)))
if ly.Params.IsLearnTrgAvg() {
ly.LayerBase.WriteWeightsJSONBase(w, depth, "ActAvg", "TrgAvg")
} else {
ly.LayerBase.WriteWeightsJSONBase(w, depth)
}
}
// SetWeights sets the weights for this layer from weights.Layer decoded values
func (ly *Layer) SetWeights(lw *weights.Layer) error {
if ly.Off {
return nil
}
li := ly.Index
ctx := ly.Network.Context()
if lw.MetaData != nil {
for di := uint32(0); di < ly.MaxData; di++ {
if am, ok := lw.MetaData["ActMAvg"]; ok {
pv, _ := strconv.ParseFloat(am, 32)
LayerStates.Set(float32(pv), int(li), int(di), int(LayerActMAvg))
}
if ap, ok := lw.MetaData["ActPAvg"]; ok {
pv, _ := strconv.ParseFloat(ap, 32)
LayerStates.Set(float32(pv), int(li), int(di), int(LayerActPAvg))
}
if gi, ok := lw.MetaData["GiMult"]; ok {
pv, _ := strconv.ParseFloat(gi, 32)
LayerStates.Set(float32(pv), int(li), int(di), int(LayerGiMult))
}
}
}
if lw.Units != nil {
if ta, ok := lw.Units["ActAvg"]; ok {
for lni := range ta {
if lni > int(ly.NNeurons) {
break
}
ni := ly.NeurStIndex + uint32(lni)
NeuronAvgs.Set(ta[lni], int(ni), int(ActAvg))
}
}
if ta, ok := lw.Units["TrgAvg"]; ok {
for lni := range ta {
if lni > int(ly.NNeurons) {
break
}
ni := ly.NeurStIndex + uint32(lni)
NeuronAvgs.Set(ta[lni], int(ni), int(TrgAvg))
}
}
}
var err error
if len(lw.Paths) == ly.NumRecvPaths() { // this is essential if multiple paths from same layer
for pi := range lw.Paths {
pw := &lw.Paths[pi]
pt := ly.RecvPaths[pi]
er := pt.SetWeights(pw)
if er != nil {
err = er
}
}
} else {
for pi := range lw.Paths {
pw := &lw.Paths[pi]
pt, _ := ly.RecvPathBySendName(pw.From)
if pt != nil {
er := pt.SetWeights(pw)
if er != nil {
err = er
}
}
}
}
ly.Params.AvgDifFromTrgAvg(ctx) // update AvgPct based on loaded ActAvg values
return err
}
// JsonToParams reformates json output to suitable params display output
func JsonToParams(b []byte) string {
br := strings.Replace(string(b), `"`, ``, -1)
br = strings.Replace(br, ",\n", "", -1)
br = strings.Replace(br, "{\n", "{", -1)
br = strings.Replace(br, "} ", "}\n ", -1)
br = strings.Replace(br, "\n }", " }", -1)
br = strings.Replace(br, "\n }\n", " }", -1)
return br[1:] + "\n"
}
// TestValues returns a map of key vals for testing
// ctrKey is a key of counters to contextualize values.
func (ly *Layer) TestValues(ctrKey string, vals map[string]float32) {
for spi := uint32(0); spi < ly.NPools; spi++ {
for di := uint32(0); di < ly.MaxData; di++ {
pi := ly.Params.PoolIndex(spi)
key := fmt.Sprintf("%s Lay: %s\tPool: %d\tDi: %d", ctrKey, ly.Name, pi, di)
PoolTestValues(pi, di, key, vals)
}
}
}
//////// Lesion
// UnLesionNeurons unlesions (clears the Off flag) for all neurons in the layer
func (ly *Layer) UnLesionNeurons() { //types:add
nn := ly.NNeurons
for lni := uint32(0); lni < nn; lni++ {
ni := ly.NeurStIndex + lni
for di := uint32(0); di < ly.MaxData; di++ {
NeuronClearFlag(NeuronOff, ni, di)
}
}
}
// LesionNeurons lesions (sets the Off flag) for given proportion (0-1) of neurons in layer
// returns number of neurons lesioned. Emits error if prop > 1 as indication that percent
// might have been passed
func (ly *Layer) LesionNeurons(prop float32) int { //types:add
ly.UnLesionNeurons()
if prop > 1 {
log.Printf("LesionNeurons got a proportion > 1 -- must be 0-1 as *proportion* (not percent) of neurons to lesion: %v\n", prop)
return 0
}
nn := ly.NNeurons
if nn == 0 {
return 0
}
p := rand.Perm(int(nn))
nl := int(prop * float32(nn))
for lni := uint32(0); lni < nn; lni++ {
nip := uint32(p[lni])
ni := ly.NeurStIndex + nip
if NeuronIsOff(ni) {
continue
}
for di := uint32(0); di < ly.MaxData; di++ {
NeuronSetFlag(NeuronOff, ni, di)
}
}
return nl
}
// MakeToolbar is the standard core GUI toolbar for the layer when edited.
func (ly *Layer) MakeToolbar(p *tree.Plan) {
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(ly.Defaults).SetIcon(icons.Reset)
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(ly.InitWeights).SetIcon(icons.Reset)
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(ly.InitActs).SetIcon(icons.Reset)
})
tree.Add(p, func(w *core.Separator) {})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(ly.LesionNeurons).SetIcon(icons.Cut)
})
tree.Add(p, func(w *core.FuncButton) {
w.SetFunc(ly.UnLesionNeurons).SetIcon(icons.Cut)
})
}