forked from emer/leabra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctxtprjn.go
215 lines (187 loc) · 5.85 KB
/
ctxtprjn.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
// 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 deep
import (
"github.com/ccnlab/leabrax/leabra"
"github.com/chewxy/math32"
"github.com/emer/emergent/emer"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// CtxtSender is an interface for layers that implement the SendCtxtGe method
// (SuperLayer, CTLayer)
type CtxtSender interface {
leabra.LeabraLayer
// SendCtxtGe sends activation over CTCtxtPrjn projections to integrate
// CtxtGe excitatory conductance on CT layers.
// This must be called at the end of the Burst quarter for this layer.
SendCtxtGe(ltime *leabra.Time)
}
// CTCtxtPrjn is the "context" temporally-delayed projection into CTLayer,
// (corticothalamic deep layer 6) where the CtxtGe excitatory input
// is integrated only at end of Burst Quarter.
// Set FmSuper for the main projection from corresponding Super layer.
type CTCtxtPrjn struct {
leabra.Prjn // access as .Prjn
FmSuper bool `desc:"if true, this is the projection from corresponding Superficial layer -- should be OneToOne prjn, with Learn.Learn = false, WtInit.Var = 0, Mean = 0.8 -- these defaults are set if FmSuper = true"`
CtxtGeInc []float32 `desc:"local per-recv unit accumulator for Ctxt excitatory conductance from sending units -- not a delta -- the full value"`
}
var KiT_CTCtxtPrjn = kit.Types.AddType(&CTCtxtPrjn{}, PrjnProps)
func (pj *CTCtxtPrjn) Defaults() {
pj.Prjn.Defaults()
if pj.FmSuper {
pj.Learn.Learn = false
pj.WtInit.Mean = 0.5 // .5 better than .8 in several cases..
pj.WtInit.Var = 0
}
}
func (pj *CTCtxtPrjn) UpdateParams() {
pj.Prjn.UpdateParams()
}
func (pj *CTCtxtPrjn) Type() emer.PrjnType {
return CTCtxt
}
func (pj *CTCtxtPrjn) PrjnTypeName() string {
if pj.Typ < emer.PrjnTypeN {
return pj.Typ.String()
}
ptyp := PrjnType(pj.Typ)
ts := ptyp.String()
sz := len(ts)
if sz > 0 {
return ts[:sz-1] // cut off trailing _
}
return ""
}
func (pj *CTCtxtPrjn) Build() error {
err := pj.Prjn.Build()
if err != nil {
return err
}
rsh := pj.Recv.Shape()
rlen := rsh.Len()
pj.CtxtGeInc = make([]float32, rlen)
return nil
}
//////////////////////////////////////////////////////////////////////////////////////
// Init methods
func (pj *CTCtxtPrjn) InitGInc() {
pj.Prjn.InitGInc()
for ri := range pj.CtxtGeInc {
pj.CtxtGeInc[ri] = 0
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Act methods
// SendGDelta: disabled for this type
func (pj *CTCtxtPrjn) SendGDelta(si int, delta float32) {
}
// RecvGInc: disabled for this type
func (pj *CTCtxtPrjn) RecvGInc() {
}
// SendCtxtGe sends the full Burst activation from sending neuron index si,
// to integrate CtxtGe excitatory conductance on receivers
func (pj *CTCtxtPrjn) SendCtxtGe(si int, dburst float32) {
scdb := dburst * pj.GScale
nc := pj.SConN[si]
st := pj.SConIdxSt[si]
syns := pj.Syns[st : st+nc]
scons := pj.SConIdx[st : st+nc]
for ci := range syns {
ri := scons[ci]
pj.CtxtGeInc[ri] += scdb * syns[ci].Wt
}
}
// RecvCtxtGeInc increments the receiver's CtxtGe from that of all the projections
func (pj *CTCtxtPrjn) RecvCtxtGeInc() {
rlay, ok := pj.Recv.(*CTLayer)
if !ok {
return
}
for ri := range rlay.CtxtGes {
rlay.CtxtGes[ri] += pj.CtxtGeInc[ri]
pj.CtxtGeInc[ri] = 0
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Learn methods
// DWt computes the weight change (learning) for Ctxt projections
func (pj *CTCtxtPrjn) DWt() {
if !pj.Learn.Learn {
return
}
slay := pj.Send.(leabra.LeabraLayer).AsLeabra()
sslay, issuper := pj.Send.(*SuperLayer)
rlay := pj.Recv.(leabra.LeabraLayer).AsLeabra()
for si := range slay.Neurons {
sact := float32(0)
if issuper {
sact = sslay.SuperNeurs[si].BurstPrv
} else {
sact = slay.Neurons[si].ActQ0
}
nc := int(pj.SConN[si])
st := int(pj.SConIdxSt[si])
syns := pj.Syns[st : st+nc]
scons := pj.SConIdx[st : st+nc]
for ci := range syns {
sy := &syns[ci]
ri := scons[ci]
rn := &rlay.Neurons[ri]
// following line should be ONLY diff: sact for *both* short and medium *sender*
// activations, which are first two args:
err, bcm := pj.Learn.CHLdWt(sact, sact, rn.AvgSLrn, rn.AvgM, rn.AvgL)
bcm *= pj.Learn.XCal.LongLrate(rn.AvgLLrn)
err *= pj.Learn.XCal.MLrn
dwt := bcm + err
norm := float32(1)
if pj.Learn.Norm.On {
norm = pj.Learn.Norm.NormFmAbsDWt(&sy.Norm, math32.Abs(dwt))
}
if pj.Learn.Momentum.On {
dwt = norm * pj.Learn.Momentum.MomentFmDWt(&sy.Moment, dwt)
} else {
dwt *= norm
}
sy.DWt += pj.Learn.Lrate * dwt
}
// aggregate max DWtNorm over sending synapses
if pj.Learn.Norm.On {
maxNorm := float32(0)
for ci := range syns {
sy := &syns[ci]
if sy.Norm > maxNorm {
maxNorm = sy.Norm
}
}
for ci := range syns {
sy := &syns[ci]
sy.Norm = maxNorm
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// PrjnType
// PrjnType has the DeepLeabra extensions to the emer.PrjnType types, for gui
type PrjnType emer.PrjnType
//go:generate stringer -type=PrjnType
var KiT_PrjnType = kit.Enums.AddEnumExt(emer.KiT_PrjnType, PrjnTypeN, kit.NotBitFlag, nil)
// The DeepLeabra prjn types
const (
// CTCtxt are projections from Superficial layers to CT layers that
// send Burst activations drive updating of CtxtGe excitatory conductance,
// at end of a DeepBurst quarter. These projections also use a special learning
// rule that takes into account the temporal delays in the activation states.
// Can also add self context from CT for deeper temporal context.
CTCtxt emer.PrjnType = emer.PrjnTypeN + iota
)
// gui versions
const (
CTCtxt_ PrjnType = PrjnType(emer.PrjnTypeN) + iota
PrjnTypeN
)
var PrjnProps = ki.Props{
"EnumType:Typ": KiT_PrjnType,
}