/
pvlv_run.go
294 lines (273 loc) · 7.09 KB
/
pvlv_run.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
// Copyright (c) 2020, 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 main
import (
"fmt"
"github.com/emer/leabra/examples/pvlv/data"
"github.com/emer/leabra/leabra"
"github.com/goki/ki/kit"
)
////////////////////////////////////////////////////////////////////////////////
// Running the network..
type StepGrain int
const (
Cycle StepGrain = iota
Quarter
AlphaMinus
AlphaFull
SGTrial // Trial
TrialBlock // Block
Condition
StepGrainN
)
var KiT_StepGrain = kit.Enums.AddEnum(StepGrainN, kit.NotBitFlag, nil)
func (ss *Sim) SettleMinus(train bool) {
ev := &ss.Env
viewUpdt := ss.TrainUpdt
if !train {
viewUpdt = ss.TestUpdt
}
for qtr := 0; qtr < 3; qtr++ {
for cyc := 0; cyc < ss.Time.CycPerQtr; cyc++ {
ss.Net.Cycle(&ss.Time)
if ss.CycleLogUpdt == leabra.Cycle {
ev.GlobalStep++
ss.LogCycleData()
}
ss.Time.CycleInc()
if ss.Stepper.StepPoint(int(Cycle)) {
return
}
//ss.MaybeUpdate(train, false, leabra.FastSpike)
if ss.ViewOn {
switch viewUpdt {
case leabra.Cycle:
if cyc != ss.Time.CycPerQtr-1 { // will be updated by quarter
ss.UpdateView(ss.Time.Cycle)
}
case leabra.FastSpike: // every 10 cycles
if (cyc+1)%10 == 0 {
ss.UpdateView(-1)
}
}
}
}
ss.Net.QuarterFinal(&ss.Time)
if ss.ViewOn {
switch viewUpdt {
case leabra.Cycle:
ss.UpdateView(ss.Time.Cycle)
case leabra.Quarter:
ss.UpdateView(-1)
case leabra.Phase:
if qtr >= 2 {
ss.UpdateView(-1)
}
}
}
ss.Time.QuarterInc()
if ss.CycleLogUpdt == leabra.Quarter {
ev.GlobalStep++
ss.LogCycleData()
}
if ss.Stepper.StepPoint(int(Quarter)) {
return
}
}
}
func (ss *Sim) SettlePlus(train bool) {
ev := &ss.Env
viewUpdt := ss.TrainUpdt
if !train {
viewUpdt = ss.TestUpdt
}
for cyc := 0; cyc < ss.Time.CycPerQtr; cyc++ {
ss.Net.Cycle(&ss.Time)
if ss.CycleLogUpdt == leabra.Cycle {
ev.GlobalStep++
ss.LogCycleData()
}
ss.Time.CycleInc()
if ss.Stepper.StepPoint(int(Cycle)) {
return
}
if ss.ViewOn {
switch viewUpdt {
case leabra.Cycle:
if cyc != ss.Time.CycPerQtr-1 { // will be updated by quarter
ss.UpdateView(ss.Time.Cycle)
}
case leabra.FastSpike:
if (cyc+1)%10 == 0 {
ss.UpdateView(-1)
}
}
}
}
ss.Net.QuarterFinal(&ss.Time)
if ss.ViewOn {
switch viewUpdt {
case leabra.Quarter, leabra.Phase:
ss.UpdateView(-1)
}
}
ss.Time.QuarterInc()
if ss.CycleLogUpdt == leabra.Quarter {
ev.GlobalStep++
ss.LogCycleData()
}
if ss.Stepper.StepPoint(int(Quarter)) {
return
}
}
func (ss *Sim) TrialStart(train bool) {
// update prior weight changes at start, so any DWt values remain visible at end
// you might want to do this less frequently to achieve a mini-batch update
// in which case, move it out to the TrainTrial method where the relevant
// counters are being dealt with.
if train {
ss.Net.WtFmDWt()
}
ss.Net.AlphaCycInit(train)
ss.Time.AlphaCycStart()
}
func (ss *Sim) TrialEnd(_ *PVLVEnv, train bool) {
viewUpdt := ss.TrainUpdt
if !train {
viewUpdt = ss.TestUpdt
}
if ss.ViewOn && viewUpdt == leabra.Trial {
ss.UpdateView(-1)
}
}
// ApplyInputs applies input patterns from given environment.
// It is good practice to have this be a separate method with appropriate
// args so that it can be used for various different contexts
// (training, testing, etc).
func (ss *Sim) ApplyInputs() {
ev := &ss.Env
ss.Net.InitExt() // clear any existing inputs -- not strictly necessary if always
// going to the same layers, but good practice and cheap anyway
lays := []string{"StimIn", "ContextIn", "USTimeIn"}
for _, lnm := range lays {
ly := ss.Net.LayerByName(lnm).(leabra.LeabraLayer).AsLeabra()
pats := ev.State(ly.Nm)
if pats == nil {
continue
}
ly.ApplyExt(pats)
}
}
func (ss *Sim) ApplyPVInputs() {
ev := &ss.Env
lays := []string{"PosPV", "NegPV"}
for _, lnm := range lays {
ly := ss.Net.LayerByName(lnm).(leabra.LeabraLayer).AsLeabra()
pats := ev.State(ly.Nm)
if pats == nil {
continue
}
ly.ApplyExt(pats)
}
}
// SingleTrial and functions -- SingleTrial has been consolidated into this
// A block is a set of trials, whose length is set by the current ConditionParams record
func (ev *PVLVEnv) RunOneTrialBlk(ss *Sim) {
blockDone := false
var curTG *data.TrialInstance
ev.BlockStart(ss)
ev.SetActiveTrialList(ss) // sets up one block's worth of data
blockDone = ev.TrialCt.Cur >= ev.TrialCt.Max
for !blockDone {
if ev.TrialInstances.AtEnd() {
panic(fmt.Sprintf("ran off end of TrialInstances list"))
}
curTG = ev.TrialInstances.ReadNext()
ev.AlphaCycle.Max = curTG.AlphaTicksPerTrialGp
blockDone = ev.RunOneTrial(ss, curTG) // run one instantiated trial type (aka "trial group")
if ss.ViewOn && ss.TrainUpdt == leabra.Trial {
ss.UpdateView(-1)
}
if ss.Stepper.StepPoint(int(SGTrial)) {
return
}
}
ev.TrialBlockCt.Incr()
ev.BlockEnded = true
ev.BlockEnd(ss) // run monitoring and analysis, maybe save weights
if ss.Stepper.StepPoint(int(TrialBlock)) {
return
}
if ss.ViewOn && ss.TrainUpdt >= leabra.Epoch {
ss.UpdateView(-1)
}
}
// run through a complete trial, consisting of a number of ticks as specified in the Trial spec
func (ev *PVLVEnv) RunOneTrial(ss *Sim, curTrial *data.TrialInstance) (blockDone bool) {
var train bool
trialDone := false
ss.Net.ClearModActs(&ss.Time)
for !trialDone {
ev.SetupOneAlphaTrial(curTrial, 0)
train = !ev.IsTestTrial(curTrial)
ev.RunOneAlphaCycle(ss, curTrial)
trialDone = ev.AlphaCycle.Incr()
if ss.Stepper.StepPoint(int(AlphaFull)) {
return
}
if ss.ViewOn && ss.TrainUpdt <= leabra.Quarter {
ss.UpdateView(-1)
}
}
ss.Net.ClearMSNTraces(&ss.Time)
blockDone = ev.TrialCt.Incr()
ss.TrialEnd(ev, train)
//ss.LogTrialData(ev) // accumulate
if ss.ViewOn && ss.TrainUpdt == leabra.Trial {
ss.UpdateView(-1)
}
return blockDone
}
// AlphaCyc runs one alpha-cycle (100 msec, 4 quarters) of processing.
// External inputs must have already been applied prior to calling,
// using ApplyExt method on relevant layers (see TrainTrial, TestTrial).
// If train is true, then learning DWt or WtFmDWt calls are made.
// Handles netview updating within scope of AlphaCycle
func (ev *PVLVEnv) RunOneAlphaCycle(ss *Sim, trial *data.TrialInstance) {
train := !ev.IsTestTrial(trial)
ss.TrialStart(train)
ev.SetState()
ss.ApplyInputs()
ss.SettleMinus(train)
if ss.Stepper.StepPoint(int(AlphaMinus)) {
return
}
ss.ApplyInputs()
ss.ApplyPVInputs()
ss.SettlePlus(train)
if train {
ss.Net.DWt()
if ss.NetView != nil && ss.NetView.IsVisible() {
ss.NetView.RecordSyns()
}
}
if ss.ViewOn && ss.TrainUpdt == leabra.AlphaCycle {
ss.UpdateView(-1)
}
ss.LogTrialTypeData()
//_ = ss.Stepper.StepPoint(int(AlphaPlus))
}
// brought over from cemer. This was named StepStopTest in cemer
func (ev *PVLVEnv) TrialNameStopTest(_ *Sim) bool {
return false
}
// end SingleTrial and functions
// TrainEnd
func (ev *PVLVEnv) TrainEnd(ss *Sim) {
if ev.CurConditionParams.SaveFinalWts {
ev.SaveWeights(ss)
}
ss.Stepper.Stop()
}
// end TrainEnd