/
rl_cond.go
962 lines (834 loc) · 30.5 KB
/
rl_cond.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
// 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.
/*
rl_cond explores the temporal differences (TD) reinforcement learning algorithm under some basic Pavlovian conditioning environments.
*/
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/emer/emergent/emer"
"github.com/emer/emergent/env"
"github.com/emer/emergent/netview"
"github.com/emer/emergent/params"
"github.com/emer/emergent/prjn"
"github.com/emer/emergent/relpos"
"github.com/emer/etable/eplot"
"github.com/emer/etable/etable"
"github.com/emer/etable/etensor"
"github.com/emer/etable/etview" // include to get gui views
"github.com/emer/leabra/leabra"
"github.com/emer/leabra/rl"
"github.com/goki/gi/gi"
"github.com/goki/gi/gimain"
"github.com/goki/gi/giv"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
)
// this is the stub main for gogi that calls our actual mainrun function, at end of file
func main() {
gimain.Main(func() {
mainrun()
})
}
// LogPrec is precision for saving float values in logs
const LogPrec = 4
// ParamSets is the default set of parameters -- Base is always applied, and others can be optionally
// selected to apply on top of that
var ParamSets = params.Sets{
{Name: "Base", Desc: "these are the best params", Sheets: params.Sheets{
"Network": ¶ms.Sheet{
{Sel: "Prjn", Desc: "no extra learning factors",
Params: params.Params{
"Prjn.Learn.Norm.On": "false",
"Prjn.Learn.Momentum.On": "false",
"Prjn.Learn.WtBal.On": "false",
}},
{Sel: "Layer", Desc: "faster average",
Params: params.Params{
"Layer.Act.Dt.AvgTau": "200",
}},
{Sel: "#Input", Desc: "input fixed act",
Params: params.Params{
"Layer.Inhib.ActAvg.Fixed": "true", // critical for ensuring weights have same impact!
"Layer.Inhib.ActAvg.Init": "0.015",
}},
{Sel: ".TDRewToInteg", Desc: "rew to integ",
Params: params.Params{
"Prjn.Learn.Learn": "false",
"Prjn.WtInit.Mean": "1",
"Prjn.WtInit.Var": "0",
"Prjn.WtInit.Sym": "false",
}},
{Sel: "#InputToRewPred", Desc: "input to rewpred",
Params: params.Params{
"Prjn.WtInit.Mean": "0",
"Prjn.WtInit.Var": "0",
"Prjn.WtInit.Sym": "false",
"Prjn.Learn.Lrate": "0.5",
}},
{Sel: "#Rew", Desc: "allow negative",
Params: params.Params{
"Layer.Act.Clamp.Range.Min": "-1",
"Layer.Act.Clamp.Range.Max": "1",
"Layer.Inhib.ActAvg.Fixed": "true", // critical for ensuring weights have same impact!
"Layer.Inhib.ActAvg.Init": "1",
}},
},
}},
}
// Sim encapsulates the entire simulation model, and we define all the
// functionality as methods on this struct. This structure keeps all relevant
// state information organized and available without having to pass everything around
// as arguments to methods, and provides the core GUI interface (note the view tags
// for the fields which provide hints to how things should be displayed).
type Sim struct {
Discount float32 `def:"0.9" desc:"discount factor for future rewards"`
Lrate float32 `def:"0.5" desc:"learning rate"`
Net *leabra.Network `view:"no-inline" desc:"the network -- click to view / edit parameters for layers, prjns, etc"`
TrainEnv CondEnv `desc:"Training environment -- conditioning environment"`
TrnEpcLog *etable.Table `view:"no-inline" desc:"training epoch-level log data"`
TrnTrlLog *etable.Table `view:"no-inline" desc:"testing trial-level log data"`
RewPredInputWts etensor.Tensor `view:"no-inline" desc:"weights from input to hidden layer"`
Params params.Sets `view:"no-inline" desc:"full collection of param sets"`
ParamSet string `view:"-" desc:"which set of *additional* parameters to use -- always applies Base and optionaly this next if set -- can use multiple names separated by spaces (don't put spaces in ParamSet names!)"`
MaxRuns int `desc:"maximum number of model runs to perform"`
MaxEpcs int `desc:"maximum number of epochs to run per model run"`
MaxTrls int `desc:"maximum number of training trials per epoch"`
Time leabra.Time `desc:"leabra timing parameters and state"`
ViewOn bool `desc:"whether to update the network view while running"`
TrainUpdt leabra.TimeScales `desc:"at what time scale to update the display during training? Anything longer than Epoch updates at Epoch in this model"`
TestUpdt leabra.TimeScales `desc:"at what time scale to update the display during testing? Anything longer than Epoch updates at Epoch in this model"`
TstRecLays []string `desc:"names of layers to record activations etc of during testing"`
// internal state - view:"-"
Win *gi.Window `view:"-" desc:"main GUI window"`
NetView *netview.NetView `view:"-" desc:"the network viewer"`
ToolBar *gi.ToolBar `view:"-" desc:"the master toolbar"`
WtsGrid *etview.TensorGrid `view:"-" desc:"the weights grid view"`
TrnEpcPlot *eplot.Plot2D `view:"-" desc:"the training epoch plot"`
TrnTrlPlot *eplot.Plot2D `view:"-" desc:"the test-trial plot"`
RunPlot *eplot.Plot2D `view:"-" desc:"the run plot"`
TrnEpcFile *os.File `view:"-" desc:"log file"`
RunFile *os.File `view:"-" desc:"log file"`
ValsTsrs map[string]*etensor.Float32 `view:"-" desc:"for holding layer values"`
IsRunning bool `view:"-" desc:"true if sim is running"`
StopNow bool `view:"-" desc:"flag to stop running"`
NeedsNewRun bool `view:"-" desc:"flag to initialize NewRun if last one finished"`
RndSeed int64 `view:"-" desc:"the current random seed"`
}
// this registers this Sim Type and gives it properties that e.g.,
// prompt for filename for save methods.
var KiT_Sim = kit.Types.AddType(&Sim{}, SimProps)
// TheSim is the overall state for this simulation
var TheSim Sim
// New creates new blank elements and initializes defaults
func (ss *Sim) New() {
ss.Net = &leabra.Network{}
ss.TrnEpcLog = &etable.Table{}
ss.TrnTrlLog = &etable.Table{}
ss.RewPredInputWts = &etensor.Float32{}
ss.Params = ParamSets
ss.RndSeed = 1
ss.ViewOn = true
ss.TrainUpdt = leabra.AlphaCycle
ss.TestUpdt = leabra.AlphaCycle
ss.TstRecLays = []string{"Input"}
ss.Defaults()
}
func (ss *Sim) Defaults() {
ss.Discount = 0.9
ss.Lrate = 0.5
}
////////////////////////////////////////////////////////////////////////////////////////////
// Configs
// Config configures all the elements using the standard functions
func (ss *Sim) Config() {
ss.ConfigEnv()
ss.ConfigNet(ss.Net)
ss.ConfigTrnEpcLog(ss.TrnEpcLog)
ss.ConfigTrnTrlLog(ss.TrnTrlLog)
}
func (ss *Sim) ConfigEnv() {
if ss.MaxRuns == 0 { // allow user override
ss.MaxRuns = 1
}
if ss.MaxEpcs == 0 { // allow user override
ss.MaxEpcs = 30
}
if ss.MaxTrls == 0 { // allow user override
ss.MaxTrls = 10
}
ss.TrainEnv.Nm = "TrainEnv"
ss.TrainEnv.Dsc = "training params and state"
ss.TrainEnv.Defaults()
ss.TrainEnv.RewVal = 1
ss.TrainEnv.NoRewVal = 0
ss.TrainEnv.Validate()
ss.TrainEnv.Run.Max = ss.MaxRuns // note: we are not setting epoch max -- do that manually
ss.TrainEnv.Trial.Max = ss.MaxTrls
ss.TrainEnv.Init(0)
}
func (ss *Sim) ConfigNet(net *leabra.Network) {
net.InitName(net, "RLCond")
rew, rp, ri, td := rl.AddTDLayers(net, "", relpos.RightOf, 4)
_ = rew
_ = ri
inp := net.AddLayer2D("Input", 3, 20, emer.Input)
inp.SetRelPos(relpos.Rel{Rel: relpos.Above, Other: "Rew", YAlign: relpos.Front, XAlign: relpos.Left})
net.ConnectLayersPrjn(inp, rp, prjn.NewFull(), emer.Forward, &rl.TDRewPredPrjn{})
td.(*rl.TDDaLayer).SendDA.AddAllBut(net) // send dopamine to all layers..
net.Defaults()
ss.SetParams("Network", false) // only set Network params
err := net.Build()
if err != nil {
log.Println(err)
return
}
net.InitWts()
}
////////////////////////////////////////////////////////////////////////////////
// Init, utils
// Init restarts the run, and initializes everything, including network weights
// and resets the epoch log table
func (ss *Sim) Init() {
rand.Seed(ss.RndSeed)
ss.StopNow = false
ss.SetParams("", false) // all sheets
ss.NewRun()
ss.UpdateView(true, -1)
if ss.NetView != nil && ss.NetView.IsVisible() {
ss.NetView.RecordSyns()
}
}
// NewRndSeed gets a new random seed based on current time -- otherwise uses
// the same random seed for every run
func (ss *Sim) NewRndSeed() {
ss.RndSeed = time.Now().UnixNano()
}
// Counters returns a string of the current counter state
// use tabs to achieve a reasonable formatting overall
// and add a few tabs at the end to allow for expansion..
func (ss *Sim) Counters(train bool) string {
return fmt.Sprintf("Run:\t%d\tEpoch:\t%d\tTrial:\t%d\tEvent:\t%d\tCycle:\t%d\tName:\t%s\t\t\t", ss.TrainEnv.Run.Cur, ss.TrainEnv.Epoch.Cur, ss.TrainEnv.Trial.Cur, ss.TrainEnv.Event.Cur, ss.Time.Cycle, ss.TrainEnv.String())
}
func (ss *Sim) UpdateView(train bool, cyc int) {
if ss.NetView != nil && ss.NetView.IsVisible() {
ss.NetView.Record(ss.Counters(train), cyc)
// note: essential to use Go version of update when called from another goroutine
ss.NetView.GoUpdate() // note: using counters is significantly slower..
}
}
////////////////////////////////////////////////////////////////////////////////
// Running the Network, starting bottom-up..
// 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 (ss *Sim) AlphaCyc(train bool) {
// ss.Win.PollEvents() // this can be used instead of running in a separate goroutine
viewUpdt := ss.TrainUpdt
if !train {
viewUpdt = ss.TestUpdt
}
ss.Net.AlphaCycInit(train)
ss.Time.AlphaCycStart()
for qtr := 0; qtr < 4; qtr++ {
for cyc := 0; cyc < ss.Time.CycPerQtr; cyc++ {
ss.Net.Cycle(&ss.Time)
ss.Time.CycleInc()
if ss.ViewOn {
switch viewUpdt {
case leabra.Cycle:
if cyc != ss.Time.CycPerQtr-1 { // will be updated by quarter
ss.UpdateView(train, ss.Time.Cycle)
}
case leabra.FastSpike:
if (cyc+1)%10 == 0 {
ss.UpdateView(train, -1)
}
}
}
}
ss.Net.QuarterFinal(&ss.Time)
ss.Time.QuarterInc()
if ss.ViewOn {
switch {
case viewUpdt == leabra.Cycle:
ss.UpdateView(train, ss.Time.Cycle)
case viewUpdt <= leabra.Quarter:
ss.UpdateView(train, -1)
case viewUpdt == leabra.Phase:
if qtr >= 2 {
ss.UpdateView(train, -1)
}
}
}
}
if train {
ss.Net.DWt()
if ss.NetView != nil && ss.NetView.IsVisible() {
ss.NetView.RecordSyns()
}
ss.Net.WtFmDWt()
}
if ss.ViewOn && viewUpdt == leabra.AlphaCycle {
ss.UpdateView(train, -1)
}
}
// ApplyInputs applies input patterns from given envirbonment.
// 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(en env.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{"Input"}
for _, lnm := range lays {
ly := ss.Net.LayerByName(lnm).(leabra.LeabraLayer).AsLeabra()
pats := en.State(ly.Nm)
if pats == nil {
continue
}
ly.ApplyExt(pats)
}
pats := en.State("Reward")
ly := ss.Net.LayerByName("Rew").(leabra.LeabraLayer).AsLeabra()
ly.ApplyExt1DTsr(pats)
}
// TrainEvent runs one event of training using TrainEnv
func (ss *Sim) TrainEvent() {
if ss.NeedsNewRun {
ss.NewRun()
}
ss.TrainEnv.Step() // the Env encapsulates and manages all counter state
_, _, tchg := ss.TrainEnv.Counter(env.Trial)
if tchg && ss.TrnTrlPlot != nil {
ss.TrnTrlPlot.GoUpdate()
}
// Key to query counters FIRST because current state is in NEXT epoch
// if epoch counter has changed
epc, _, chg := ss.TrainEnv.Counter(env.Epoch)
if chg {
if ss.ViewOn && ss.TrainUpdt > leabra.AlphaCycle {
ss.UpdateView(true, -1)
}
ss.LogTrnEpc(ss.TrnEpcLog)
if epc >= ss.MaxEpcs {
// done with training..
ss.RunEnd()
if ss.TrainEnv.Run.Incr() { // we are done!
ss.StopNow = true
return
} else {
ss.NeedsNewRun = true
return
}
}
}
ss.ApplyInputs(&ss.TrainEnv)
ss.AlphaCyc(true) // train
ss.TrialStats(true) // accumulate
ss.LogTrnTrl(ss.TrnTrlLog)
}
// RunEnd is called at the end of a run -- save weights, record final log, etc here
func (ss *Sim) RunEnd() {
}
// NewRun intializes a new run of the model, using the TrainEnv.Run counter
// for the new run value
func (ss *Sim) NewRun() {
run := ss.TrainEnv.Run.Cur
ss.TrainEnv.Init(run)
ss.Time.Reset()
ss.Net.InitWts()
ss.InitStats()
ss.TrnEpcLog.SetNumRows(0)
ss.TrnTrlLog.SetNumRows(0)
ss.NeedsNewRun = false
}
// InitStats initializes all the statistics, especially important for the
// cumulative epoch stats -- called at start of new run
func (ss *Sim) InitStats() {
}
func (ss *Sim) TrialStats(accum bool) {
}
// TrainTrial runs training events for remainder of this trial
func (ss *Sim) TrainTrial() {
ss.StopNow = false
curTrl := ss.TrainEnv.Trial.Cur
for {
ss.TrainEvent()
if ss.StopNow || ss.TrainEnv.Trial.Cur != curTrl {
break
}
}
ss.Stopped()
}
// TrainEpoch runs training trials for remainder of this epoch
func (ss *Sim) TrainEpoch() {
ss.StopNow = false
curEpc := ss.TrainEnv.Epoch.Cur
for {
ss.TrainEvent()
if ss.StopNow || ss.TrainEnv.Epoch.Cur != curEpc {
break
}
}
ss.Stopped()
}
// TrainRun runs training trials for remainder of run
func (ss *Sim) TrainRun() {
ss.StopNow = false
curRun := ss.TrainEnv.Run.Cur
for {
ss.TrainEvent()
if ss.StopNow || ss.TrainEnv.Run.Cur != curRun {
break
}
}
ss.Stopped()
}
// Train runs the full training from this point onward
func (ss *Sim) Train() {
ss.StopNow = false
for {
ss.TrainEvent()
if ss.StopNow {
break
}
}
ss.Stopped()
}
// Stop tells the sim to stop running
func (ss *Sim) Stop() {
ss.StopNow = true
}
// Stopped is called when a run method stops running -- updates the IsRunning flag and toolbar
func (ss *Sim) Stopped() {
ss.IsRunning = false
if ss.Win != nil {
vp := ss.Win.WinViewport2D()
if ss.ToolBar != nil {
ss.ToolBar.UpdateActions()
}
vp.SetNeedsFullRender()
}
}
// SaveWeights saves the network weights -- when called with giv.CallMethod
// it will auto-prompt for filename
func (ss *Sim) SaveWeights(filename gi.FileName) {
ss.Net.SaveWtsJSON(filename)
}
/////////////////////////////////////////////////////////////////////////
// Params setting
// SetParams sets the params for "Base" and then current ParamSet.
// If sheet is empty, then it applies all avail sheets (e.g., Network, Sim)
// otherwise just the named sheet
// if setMsg = true then we output a message for each param that was set.
func (ss *Sim) SetParams(sheet string, setMsg bool) error {
if sheet == "" {
// this is important for catching typos and ensuring that all sheets can be used
ss.Params.ValidateSheets([]string{"Network", "Sim"})
}
err := ss.SetParamsSet("Base", sheet, setMsg)
if ss.ParamSet != "" && ss.ParamSet != "Base" {
sps := strings.Fields(ss.ParamSet)
for _, ps := range sps {
err = ss.SetParamsSet(ps, sheet, setMsg)
}
}
ri := ss.Net.LayerByName("RewInteg").(*rl.TDRewIntegLayer)
ri.RewInteg.Discount = ss.Discount
rp := ss.Net.LayerByName("RewPred").(*rl.TDRewPredLayer)
fmi := rp.SendName("Input").(leabra.LeabraPrjn).AsLeabra()
fmi.Learn.Lrate = ss.Lrate
return err
}
// SetParamsSet sets the params for given params.Set name.
// If sheet is empty, then it applies all avail sheets (e.g., Network, Sim)
// otherwise just the named sheet
// if setMsg = true then we output a message for each param that was set.
func (ss *Sim) SetParamsSet(setNm string, sheet string, setMsg bool) error {
pset, err := ss.Params.SetByNameTry(setNm)
if err != nil {
return err
}
if sheet == "" || sheet == "Network" {
netp, ok := pset.Sheets["Network"]
if ok {
ss.Net.ApplyParams(netp, setMsg)
}
}
if sheet == "" || sheet == "Sim" {
simp, ok := pset.Sheets["Sim"]
if ok {
simp.Apply(ss, setMsg)
}
}
// note: if you have more complex environments with parameters, definitely add
// sheets for them, e.g., "TrainEnv", "TrainEnv" etc
return err
}
////////////////////////////////////////////////////////////////////////////////////////////
// Logging
//////////////////////////////////////////////
// TrnEpcLog
// LogTrnEpc adds data from current epoch to the TrnEpcLog table.
// computes epoch averages prior to logging.
func (ss *Sim) LogTrnEpc(dt *etable.Table) {
row := dt.Rows
dt.SetNumRows(row + 1)
epc := ss.TrainEnv.Epoch.Prv // this is triggered by increment so use previous value
// nt := float64(ss.TrainEnv.Table.Len()) // number of trials in view
ss.RewPredInput(ss.RewPredInputWts)
if ss.WtsGrid != nil {
ss.WtsGrid.UpdateSig()
}
dt.SetCellFloat("Run", row, float64(ss.TrainEnv.Run.Cur))
dt.SetCellFloat("Epoch", row, float64(epc))
dt.SetCellTensor("RewPredInputWts", row, ss.RewPredInputWts)
// note: essential to use Go version of update when called from another goroutine
ss.TrnEpcPlot.GoUpdate()
if ss.TrnEpcFile != nil {
if ss.TrainEnv.Run.Cur == 0 && epc == 0 {
dt.WriteCSVHeaders(ss.TrnEpcFile, etable.Tab)
}
dt.WriteCSVRow(ss.TrnEpcFile, row, etable.Tab)
}
}
func (ss *Sim) ConfigTrnEpcLog(dt *etable.Table) {
dt.SetMetaData("name", "TrnEpcLog")
dt.SetMetaData("desc", "Record of performance over epochs of training")
dt.SetMetaData("read-only", "true")
dt.SetMetaData("precision", strconv.Itoa(LogPrec))
sch := etable.Schema{
{"Run", etensor.INT64, nil, nil},
{"Epoch", etensor.INT64, nil, nil},
{"RewPredInputWts", etensor.FLOAT32, []int{6, 1, 1, 6}, nil},
}
dt.SetFromSchema(sch, 0)
ss.ConfigRewPredInput(ss.RewPredInputWts)
}
func (ss *Sim) ConfigTrnEpcPlot(plt *eplot.Plot2D, dt *etable.Table) *eplot.Plot2D {
plt.Params.Title = "Reinforcement Learning Epoch Plot"
plt.Params.XAxisCol = "Epoch"
plt.SetTable(dt)
// order of params: on, fixMin, min, fixMax, max
plt.SetColParams("Run", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Epoch", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("RewPredInputWts", eplot.On, eplot.FixMin, 0, eplot.FixMax, 1)
return plt
}
func (ss *Sim) RewPredInput(dt etensor.Tensor) {
col := dt.(*etensor.Float32)
vals := col.Values
inp := ss.Net.LayerByName("Input").(leabra.LeabraLayer).AsLeabra()
isz := inp.Shape().Len()
hid := ss.Net.LayerByName("RewPred").(leabra.LeabraLayer).AsLeabra()
ysz := hid.Shape().Dim(0)
xsz := hid.Shape().Dim(1)
for y := 0; y < ysz; y++ {
for x := 0; x < xsz; x++ {
ui := (y*xsz + x)
ust := ui * isz
vls := vals[ust : ust+isz]
inp.SendPrjnVals(&vls, "Wt", hid, ui, "")
}
}
}
func (ss *Sim) ConfigRewPredInput(dt etensor.Tensor) {
dt.SetShape([]int{1, 1, 3, 20}, nil, nil)
}
//////////////////////////////////////////////
// TrnTrlLog
// ValsTsr gets value tensor of given name, creating if not yet made
func (ss *Sim) ValsTsr(name string) *etensor.Float32 {
if ss.ValsTsrs == nil {
ss.ValsTsrs = make(map[string]*etensor.Float32)
}
tsr, ok := ss.ValsTsrs[name]
if !ok {
tsr = &etensor.Float32{}
ss.ValsTsrs[name] = tsr
}
return tsr
}
// LogTrnTrl adds data from current trial to the TrnTrlLog table.
// log always contains number of testing items
func (ss *Sim) LogTrnTrl(dt *etable.Table) {
epc := ss.TrainEnv.Epoch.Prv // this is triggered by increment so use previous value
evt := ss.TrainEnv.Event.Cur
trl := ss.TrainEnv.Trial.Cur
row := dt.Rows
if dt.Rows <= row {
dt.SetNumRows(row + 1)
}
dt.SetCellFloat("Run", row, float64(ss.TrainEnv.Run.Cur))
dt.SetCellFloat("Epoch", row, float64(epc))
dt.SetCellFloat("Trial", row, float64(trl))
dt.SetCellFloat("Event", row, float64(evt))
td := ss.Net.LayerByName("TD").(leabra.LeabraLayer).AsLeabra()
rp := ss.Net.LayerByName("RewPred").(leabra.LeabraLayer).AsLeabra()
dt.SetCellFloat("TD", row, float64(td.Neurons[0].Act))
dt.SetCellFloat("RewPred", row, float64(rp.Neurons[0].Act))
for _, lnm := range ss.TstRecLays {
tsr := ss.ValsTsr(lnm)
ly := ss.Net.LayerByName(lnm).(leabra.LeabraLayer).AsLeabra()
ly.UnitValsTensor(tsr, "ActAvg")
dt.SetCellTensor(lnm, row, tsr)
}
// note: essential to use Go version of update when called from another goroutine
// ss.TrnTrlPlot.GoUpdate()
}
func (ss *Sim) ConfigTrnTrlLog(dt *etable.Table) {
dt.SetMetaData("name", "TrnTrlLog")
dt.SetMetaData("desc", "Record of training per input event (time step)")
dt.SetMetaData("read-only", "true")
dt.SetMetaData("precision", strconv.Itoa(LogPrec))
nt := 0
sch := etable.Schema{
{"Run", etensor.INT64, nil, nil},
{"Epoch", etensor.INT64, nil, nil},
{"Trial", etensor.INT64, nil, nil},
{"Event", etensor.INT64, nil, nil},
{"TD", etensor.FLOAT64, nil, nil},
{"RewPred", etensor.FLOAT64, nil, nil},
}
for _, lnm := range ss.TstRecLays {
ly := ss.Net.LayerByName(lnm).(leabra.LeabraLayer).AsLeabra()
sch = append(sch, etable.Column{lnm, etensor.FLOAT64, ly.Shp.Shp, nil})
}
dt.SetFromSchema(sch, nt)
}
func (ss *Sim) ConfigTrnTrlPlot(plt *eplot.Plot2D, dt *etable.Table) *eplot.Plot2D {
plt.Params.Title = "Reinforcement Learning Test Trial Plot"
plt.Params.XAxisCol = "Event"
plt.SetTable(dt)
// order of params: on, fixMin, min, fixMax, max
plt.SetColParams("Run", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Epoch", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Trial", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("Event", eplot.Off, eplot.FixMin, 0, eplot.FloatMax, 0)
plt.SetColParams("TD", eplot.On, eplot.FixMin, -1, eplot.FixMax, 1)
plt.SetColParams("RewPred", eplot.Off, eplot.FixMin, -1, eplot.FixMax, 1)
for _, lnm := range ss.TstRecLays {
plt.SetColParams(lnm, eplot.Off, eplot.FixMin, 0, eplot.FixMax, 1)
}
return plt
}
////////////////////////////////////////////////////////////////////////////////////////////
// Gui
// ConfigGui configures the GoGi gui interface for this simulation,
func (ss *Sim) ConfigGui() *gi.Window {
width := 1600
height := 1200
gi.SetAppName("rl_cond")
gi.SetAppAbout(`rl_cond explores the temporal differences (TD) reinforcement learning algorithm under some basic Pavlovian conditioning environments. See <a href="https://github.com/CompCogNeuro/sims/blob/master/ch7/rl_cond/README.md">README.md on GitHub</a>.</p>`)
win := gi.NewMainWindow("rl_cond", "Reinforcement Learning", width, height)
ss.Win = win
vp := win.WinViewport2D()
updt := vp.UpdateStart()
mfr := win.SetMainFrame()
tbar := gi.AddNewToolBar(mfr, "tbar")
tbar.SetStretchMaxWidth()
ss.ToolBar = tbar
split := gi.AddNewSplitView(mfr, "split")
split.Dim = mat32.X
split.SetStretchMax()
sv := giv.AddNewStructView(split, "sv")
sv.SetStruct(ss)
tv := gi.AddNewTabView(split, "tv")
nv := tv.AddNewTab(netview.KiT_NetView, "NetView").(*netview.NetView)
nv.Var = "Act"
nv.SetNet(ss.Net)
nv.Params.Raster.Max = 100
ss.NetView = nv
plt := tv.AddNewTab(eplot.KiT_Plot2D, "TrnTrlPlot").(*eplot.Plot2D)
ss.TrnTrlPlot = ss.ConfigTrnTrlPlot(plt, ss.TrnTrlLog)
tg := tv.AddNewTab(etview.KiT_TensorGrid, "Weights").(*etview.TensorGrid)
tg.SetStretchMax()
ss.WtsGrid = tg
tg.SetTensor(ss.RewPredInputWts)
plt = tv.AddNewTab(eplot.KiT_Plot2D, "TrnEpcPlot").(*eplot.Plot2D)
ss.TrnEpcPlot = ss.ConfigTrnEpcPlot(plt, ss.TrnEpcLog)
split.SetSplits(.2, .8)
tbar.AddAction(gi.ActOpts{Label: "Init", Icon: "update", Tooltip: "Initialize everything including network weights, and start over. Also applies current params.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.Init()
vp.SetNeedsFullRender()
})
tbar.AddAction(gi.ActOpts{Label: "Train", Icon: "run", Tooltip: "Starts the network training, picking up from wherever it may have left off. If not stopped, training will complete the specified number of Runs through the full number of Epochs of training, with testing automatically occuring at the specified interval.",
UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if !ss.IsRunning {
ss.IsRunning = true
tbar.UpdateActions()
// ss.Train()
go ss.Train()
}
})
tbar.AddAction(gi.ActOpts{Label: "Stop", Icon: "stop", Tooltip: "Interrupts running. Hitting Train again will pick back up where it left off.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.Stop()
})
tbar.AddAction(gi.ActOpts{Label: "Step Event", Icon: "step-fwd", Tooltip: "Advances one training event (time step) at a time.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if !ss.IsRunning {
ss.IsRunning = true
ss.TrainEvent()
ss.IsRunning = false
vp.SetNeedsFullRender()
}
})
tbar.AddAction(gi.ActOpts{Label: "Step Trial", Icon: "step-fwd", Tooltip: "Advances one training trial at a time.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if !ss.IsRunning {
ss.IsRunning = true
tbar.UpdateActions()
go ss.TrainTrial()
}
})
tbar.AddAction(gi.ActOpts{Label: "Step Epoch", Icon: "fast-fwd", Tooltip: "Advances one epoch (complete set of training patterns) at a time.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if !ss.IsRunning {
ss.IsRunning = true
tbar.UpdateActions()
go ss.TrainEpoch()
}
})
tbar.AddAction(gi.ActOpts{Label: "Step Run", Icon: "fast-fwd", Tooltip: "Advances one full training Run at a time.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if !ss.IsRunning {
ss.IsRunning = true
tbar.UpdateActions()
go ss.TrainRun()
}
})
tbar.AddSeparator("views")
tbar.AddAction(gi.ActOpts{Label: "Reset Trl Log", Icon: "update", Tooltip: "Reset trial log.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.TrnTrlLog.SetNumRows(0)
ss.TrnTrlPlot.Update()
})
tbar.AddAction(gi.ActOpts{Label: "Weights Updt", Icon: "update", Tooltip: "Update the Weights grid display to reflect the current weights.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.RewPredInput(ss.RewPredInputWts)
if ss.WtsGrid != nil {
ss.WtsGrid.UpdateSig()
}
})
tbar.AddSeparator("misc")
tbar.AddAction(gi.ActOpts{Label: "New Seed", Icon: "new", Tooltip: "Generate a new initial random seed to get different results. By default, Init re-establishes the same initial seed every time."}, win.This(),
func(recv, send ki.Ki, sig int64, data interface{}) {
ss.NewRndSeed()
})
tbar.AddAction(gi.ActOpts{Label: "Defaults", Icon: "update", Tooltip: "Restore initial default parameters.", UpdateFunc: func(act *gi.Action) {
act.SetActiveStateUpdt(!ss.IsRunning)
}}, win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
ss.Defaults()
ss.Init()
vp.SetNeedsFullRender()
})
tbar.AddAction(gi.ActOpts{Label: "README", Icon: "file-markdown", Tooltip: "Opens your browser on the README file that contains instructions for how to run this model."}, win.This(),
func(recv, send ki.Ki, sig int64, data interface{}) {
gi.OpenURL("https://github.com/CompCogNeuro/sims/blob/master/ch7/rl_cond/README.md")
})
vp.UpdateEndNoSig(updt)
// main menu
appnm := gi.AppName()
mmen := win.MainMenu
mmen.ConfigMenus([]string{appnm, "File", "Edit", "Window"})
amen := win.MainMenu.ChildByName(appnm, 0).(*gi.Action)
amen.Menu.AddAppMenu(win)
emen := win.MainMenu.ChildByName("Edit", 1).(*gi.Action)
emen.Menu.AddCopyCutPaste(win)
// note: Command in shortcuts is automatically translated into Control for
// Linux, Windows or Meta for MacOS
// fmen := win.MainMenu.ChildByName("File", 0).(*gi.Action)
// fmen.Menu.AddAction(gi.ActOpts{Label: "Open", Shortcut: "Command+O"},
// win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
// FileViewOpenSVG(vp)
// })
// fmen.Menu.AddSeparator("csep")
// fmen.Menu.AddAction(gi.ActOpts{Label: "Close Window", Shortcut: "Command+W"},
// win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
// win.Close()
// })
inQuitPrompt := false
gi.SetQuitReqFunc(func() {
if inQuitPrompt {
return
}
inQuitPrompt = true
gi.PromptDialog(vp, gi.DlgOpts{Title: "Really Quit?",
Prompt: "Are you <i>sure</i> you want to quit and lose any unsaved params, weights, logs, etc?"}, gi.AddOk, gi.AddCancel,
win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
gi.Quit()
} else {
inQuitPrompt = false
}
})
})
// gi.SetQuitCleanFunc(func() {
// fmt.Printf("Doing final Quit cleanup here..\n")
// })
inClosePrompt := false
win.SetCloseReqFunc(func(w *gi.Window) {
if inClosePrompt {
return
}
inClosePrompt = true
gi.PromptDialog(vp, gi.DlgOpts{Title: "Really Close Window?",
Prompt: "Are you <i>sure</i> you want to close the window? This will Quit the App as well, losing all unsaved params, weights, logs, etc"}, gi.AddOk, gi.AddCancel,
win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
gi.Quit()
} else {
inClosePrompt = false
}
})
})
win.SetCloseCleanFunc(func(w *gi.Window) {
go gi.Quit() // once main window is closed, quit
})
win.MainMenuUpdated()
return win
}
// These props register Save methods so they can be used
var SimProps = ki.Props{
"CallMethods": ki.PropSlice{
{"SaveWeights", ki.Props{
"desc": "save network weights to file",
"icon": "file-save",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"ext": ".wts,.wts.gz",
}},
},
}},
},
}
func mainrun() {
TheSim.New()
TheSim.Config()
TheSim.Init()
win := TheSim.ConfigGui()
win.StartEventLoop()
}