From 21993cabe7f3349930ec343c7ec1024451103452 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Tue, 20 Aug 2024 01:11:25 -0700 Subject: [PATCH] neuron fully functional and ready for further testing --- ch2/neuron/neuron.go | 75 +++++++++++++++++++++++++++++++++++++++- ch2/neuron/neuron.params | 31 ----------------- ch2/neuron/typegen.go | 9 +++++ go.mod | 2 +- go.sum | 4 +-- 5 files changed, 86 insertions(+), 35 deletions(-) delete mode 100644 ch2/neuron/neuron.params create mode 100644 ch2/neuron/typegen.go diff --git a/ch2/neuron/neuron.go b/ch2/neuron/neuron.go index 5bf9abf..7e4f5a1 100644 --- a/ch2/neuron/neuron.go +++ b/ch2/neuron/neuron.go @@ -19,6 +19,7 @@ import ( "cogentcore.org/core/core" "cogentcore.org/core/icons" "cogentcore.org/core/math32/minmax" + "cogentcore.org/core/plot/plotcore" "cogentcore.org/core/tree" "github.com/emer/emergent/v2/egui" "github.com/emer/emergent/v2/elog" @@ -135,6 +136,7 @@ func (ss *Sim) New() { } func (ss *Sim) Defaults() { + ss.SpikeParams.Defaults() ss.Params.Config(ParamSets, "", "", ss.Net) ss.UpdateInterval = 10 ss.Cycle = 0 @@ -285,6 +287,54 @@ func (ss *Sim) Stop() { ss.GUI.StopNow = true } +// SpikeVsRate runs comparison between spiking vs. rate-code +func (ss *Sim) SpikeVsRate() { + row := 0 + nsamp := 100 + // ss.KNaAdapt = false + tcl := ss.Logs.Table(etime.Test, etime.Cycle) + svr := ss.Logs.MiscTable("SpikeVsRate") + for gbarE := 0.1; gbarE <= 0.7; gbarE += 0.025 { + ss.GbarE = float32(gbarE) + spike := float64(0) + ss.Noise = 0.1 // RunCycles calls SetParams to set this + ss.Spike = true + for ns := 0; ns < nsamp; ns++ { + tcl.Rows = 0 + ss.RunCycles() + if ss.GUI.StopNow { + break + } + act := tcl.Float("Act", 159) + spike += act + } + rate := float64(0) + ss.Spike = false + // ss.Noise = 0 // doesn't make much diff + for ns := 0; ns < nsamp; ns++ { + tcl.Rows = 0 + ss.RunCycles() + if ss.GUI.StopNow { + break + } + act := tcl.Float("Act", 159) + rate += act + } + if ss.GUI.StopNow { + break + } + spike /= float64(nsamp) + rate /= float64(nsamp) + svr.AddRows(1) + svr.SetFloat("GBarE", row, gbarE) + svr.SetFloat("Spike", row, spike) + svr.SetFloat("Rate", row, rate) + row++ + } + ss.Defaults() + ss.GUI.Plots[etime.ScopeKey("SpikeVsRate")].UpdatePlot() +} + ///////////////////////////////////////////////////////////////////////// // Params setting @@ -304,16 +354,23 @@ func (ss *Sim) SetParams(sheet string, setMsg bool) { ly.Act.Update() ss.SpikeParams.ActParams = ly.Act // keep sync'd ss.SpikeParams.KNa.On = ss.KNaAdapt + ly.UpdateParams() } func (ss *Sim) ConfigLogs() { ss.ConfigLogItems() ss.Logs.CreateTables() - ss.Logs.PlotItems("Vm", "Spike") + ss.Logs.PlotItems("Ge", "Inet", "Vm", "Act", "Spike", "Gk") ss.Logs.SetContext(&ss.Stats, ss.Net) ss.Logs.ResetLog(etime.Test, etime.Cycle) + + svr := ss.Logs.MiscTable("SpikeVsRate") + svr.AddFloat64Column("GBarE") + svr.AddFloat64Column("Spike") + svr.AddFloat64Column("Rate") + svr.SetMetaData("Rate:On", "+") } func (ss *Sim) ConfigLogItems() { @@ -375,6 +432,14 @@ func (ss *Sim) ConfigGUI() { ss.GUI.AddPlots(title, &ss.Logs) + svr := "SpikeVsRate" + dt := ss.Logs.MiscTable(svr) + plt := plotcore.NewSubPlot(ss.GUI.Tabs.NewTab(svr + " Plot")) + ss.GUI.Plots[etime.ScopeKey(svr)] = plt + plt.Options.Title = svr + plt.Options.XAxis = "GBarE" + plt.SetTable(dt) + ss.GUI.Body.AddAppBar(func(p *tree.Plan) { ss.GUI.AddToolbarItem(p, egui.ToolbarItem{Label: "Init", Icon: icons.Update, Tooltip: "Initialize everything including network weights, and start over. Also applies current params.", @@ -415,6 +480,14 @@ func (ss *Sim) ConfigGUI() { ss.GUI.UpdateWindow() }, }) + ss.GUI.AddToolbarItem(p, egui.ToolbarItem{Label: "Spike Vs Rate", Icon: icons.PlayArrow, + Tooltip: "Generate a plot of actual spiking rate vs computed NXX1 rate code.", + Active: egui.ActiveStopped, + Func: func() { + ss.SpikeVsRate() + ss.GUI.UpdateWindow() + }, + }) ss.GUI.AddToolbarItem(p, egui.ToolbarItem{Label: "Defaults", Icon: icons.Update, Tooltip: "Restore initial default parameters.", diff --git a/ch2/neuron/neuron.params b/ch2/neuron/neuron.params deleted file mode 100644 index 7fdb2ad..0000000 --- a/ch2/neuron/neuron.params +++ /dev/null @@ -1,31 +0,0 @@ -[ - { - "Name": "Base", - "Desc": "these are the best params", - "Sheets": { - "Network": [ - { - "Sel": "Prjn", - "Desc": "no learning", - "Params": { - "Prjn.Learn.Learn": "false" - } - }, - { - "Sel": "Layer", - "Desc": "generic params for all layers: lower gain, slower, soft clamp", - "Params": { - "Layer.Act.Init.Vm": "0.3", - "Layer.Act.Noise.Dist": "Gaussian", - "Layer.Act.Noise.Fixed": "false", - "Layer.Act.Noise.Type": "GeNoise", - "Layer.Act.Noise.Var": "0", - "Layer.Act.XX1.Gain": "30", - "Layer.Act.XX1.NVar": "0.01", - "Layer.Inhib.Layer.On": "false" - } - } - ] - } - } -] \ No newline at end of file diff --git a/ch2/neuron/typegen.go b/ch2/neuron/typegen.go new file mode 100644 index 0000000..6caa785 --- /dev/null +++ b/ch2/neuron/typegen.go @@ -0,0 +1,9 @@ +// Code generated by "core generate -add-types"; DO NOT EDIT. + +package main + +import ( + "cogentcore.org/core/types" +) + +var _ = types.AddType(&types.Type{Name: "main.Sim", IDName: "sim", Doc: "Sim encapsulates the entire simulation model, and we define all the\nfunctionality as methods on this struct. This structure keeps all relevant\nstate information organized and available without having to pass everything around\nas arguments to methods, and provides the core GUI interface (note the view tags\nfor the fields which provide hints to how things should be displayed).", Fields: []types.Field{{Name: "Spike", Doc: "use discrete spiking equations -- otherwise use Noisy X-over-X-plus-1 rate code activation function"}, {Name: "GbarE", Doc: "excitatory conductance multiplier -- determines overall value of Ge which drives neuron to be more excited -- pushes up over threshold to fire if strong enough"}, {Name: "GbarL", Doc: "leak conductance -- determines overall value of Gl which drives neuron to be less excited (inhibited) -- pushes back to resting membrane potential"}, {Name: "ErevE", Doc: "excitatory reversal (driving) potential -- determines where excitation pushes Vm up to"}, {Name: "ErevL", Doc: "leak reversal (driving) potential -- determines where excitation pulls Vm down to"}, {Name: "Noise", Doc: "the variance parameter for Gaussian noise added to unit activations on every cycle"}, {Name: "KNaAdapt", Doc: "apply sodium-gated potassium adaptation mechanisms that cause the neuron to reduce spiking over time"}, {Name: "NCycles", Doc: "total number of cycles to run"}, {Name: "OnCycle", Doc: "when does excitatory input into neuron come on?"}, {Name: "OffCycle", Doc: "when does excitatory input into neuron go off?"}, {Name: "UpdateInterval", Doc: "how often to update display (in cycles)"}, {Name: "Net", Doc: "the network -- click to view / edit parameters for layers, paths, etc"}, {Name: "SpikeParams"}, {Name: "Context", Doc: "leabra timing parameters and state"}, {Name: "Stats", Doc: "contains computed statistic values"}, {Name: "Logs", Doc: "logging"}, {Name: "Params", Doc: "all parameter management"}, {Name: "Cycle", Doc: "current cycle of updating"}, {Name: "ViewUpdate", Doc: "netview update parameters"}, {Name: "GUI", Doc: "manages all the gui elements"}, {Name: "ValMap", Doc: "map of values for detailed debugging / testing"}}}) diff --git a/go.mod b/go.mod index 04c2fa2..f7b109a 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 require ( cogentcore.org/core v0.3.3-0.20240819201856-14e5d78556c3 github.com/emer/emergent/v2 v2.0.0-dev0.1.0.0.20240819205648-250dd483d63c - github.com/emer/leabra/v2 v2.0.0-20240819212512-f7f5a1a9993a + github.com/emer/leabra/v2 v2.0.0-20240820071506-d51ea1f829b7 ) require ( diff --git a/go.sum b/go.sum index fe81b96..384c349 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxK github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/emer/emergent/v2 v2.0.0-dev0.1.0.0.20240819205648-250dd483d63c h1:lNof7cfU4uKjimEbe9+V8iZUOkU3WWr01PIAbBmcXwQ= github.com/emer/emergent/v2 v2.0.0-dev0.1.0.0.20240819205648-250dd483d63c/go.mod h1:xSMtRuFkyD27pk5+o9BupztSxz/B2rwVL8AGmTFK/6U= -github.com/emer/leabra/v2 v2.0.0-20240819212512-f7f5a1a9993a h1:PpBtaHDf4GUApN0NFN8fXXXJ9Y8bFEog3kQAgQCtBkg= -github.com/emer/leabra/v2 v2.0.0-20240819212512-f7f5a1a9993a/go.mod h1:+3sD+Lq2GLyd1bMRU9aNn3fiBkwyKrjfYUb6TBWn1ro= +github.com/emer/leabra/v2 v2.0.0-20240820071506-d51ea1f829b7 h1:+U/neEki4ohcFXxdcGKNtonLkS5z8wTlmErF0Kv7PUE= +github.com/emer/leabra/v2 v2.0.0-20240820071506-d51ea1f829b7/go.mod h1:+3sD+Lq2GLyd1bMRU9aNn3fiBkwyKrjfYUb6TBWn1ro= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=