From c76a6a1f6fef79b4ef0f24388a9d99609b8db161 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Sat, 23 Nov 2024 02:36:42 -0800 Subject: [PATCH] using cli instead of econfig -- works.. --- axon/act-net.goal | 3 +- examples/ra25/enumgen.go | 2 +- examples/ra25/ra25.go | 94 ++++++++++++++++++++-------------------- examples/ra25/typegen.go | 8 +++- 4 files changed, 56 insertions(+), 51 deletions(-) diff --git a/axon/act-net.goal b/axon/act-net.goal index 43e94819..0fbf227e 100644 --- a/axon/act-net.goal +++ b/axon/act-net.goal @@ -70,6 +70,7 @@ func (nt *Network) NewState(mode enums.Enum, testing bool) { RunNewStateLayer(int(nix.NLayers)) RunNewStateNeuron(nd) RunInitGBuffsPath(int(nix.NPaths)) + // note: not completed until run cycles } // InitExt initializes external input state. @@ -98,7 +99,7 @@ func (nt *Network) ApplyExts() { ctx := nt.Context() nd := int(nix.NNeurons * ctx.NData) RunApplyExtsNeuron(nd) - // note: not completed + // note: not completed until cycle is run } // MinusPhase does updating after end of minus phase. diff --git a/examples/ra25/enumgen.go b/examples/ra25/enumgen.go index 94d5db9e..daae20c8 100644 --- a/examples/ra25/enumgen.go +++ b/examples/ra25/enumgen.go @@ -1,4 +1,4 @@ -// Code generated by "core generate -add-types"; DO NOT EDIT. +// Code generated by "core generate -add-types -add-funcs"; DO NOT EDIT. package main diff --git a/examples/ra25/ra25.go b/examples/ra25/ra25.go index fcd378af..52c3f8af 100644 --- a/examples/ra25/ra25.go +++ b/examples/ra25/ra25.go @@ -8,16 +8,16 @@ // defined over 5x5 input / output layers (i.e., 25 units) package main -//go:generate core generate -add-types +//go:generate core generate -add-types -add-funcs import ( "fmt" - "log" - "os" + "cogentcore.org/core/base/errors" "cogentcore.org/core/base/metadata" "cogentcore.org/core/base/mpi" "cogentcore.org/core/base/randx" + "cogentcore.org/core/cli" "cogentcore.org/core/core" "cogentcore.org/core/enums" "cogentcore.org/core/icons" @@ -30,22 +30,17 @@ import ( "cogentcore.org/core/tensor/tensorfs" "cogentcore.org/core/tree" "github.com/emer/axon/v2/axon" - "github.com/emer/emergent/v2/econfig" "github.com/emer/emergent/v2/egui" "github.com/emer/emergent/v2/env" "github.com/emer/emergent/v2/looper" + "github.com/emer/emergent/v2/patgen" "github.com/emer/emergent/v2/paths" ) func main() { - sim := &Sim{} - sim.New() - sim.ConfigAll() - if sim.Config.GUI { - sim.RunGUI() - } else { - sim.RunNoGUI() - } + opts := cli.DefaultOptions("ra25", "Random associator.") + cfg := &Config{} + cli.Run(opts, cfg, RunSim) } // Modes are the looping modes (Stacks) for running and statistics. @@ -106,7 +101,7 @@ type ParamConfig struct { type RunConfig struct { // use the GPU for computation -- generally faster even for small models if NData ~16 - GPU bool `default:"false"` + GPU bool `default:"true"` // number of data-parallel items to process in parallel per trial -- works (and is significantly faster) for both CPU and GPU. Results in an effective mini-batch of learning. NData int `default:"16" min:"1"` @@ -159,9 +154,6 @@ type LogConfig struct { // if true, save testing trial log to file, as .tst_trl.tsv typically. May be large. TestTrial bool `default:"false" nest:"+"` - - // if true, save network activation etc data from testing trials, for later viewing in netview - NetData bool } // Config is a standard Sim config -- use as a starting point. @@ -196,7 +188,7 @@ func (cfg *Config) IncludesPtr() *[]string { return &cfg.Includes } type Sim struct { // simulation configuration parameters -- set by .toml config file and / or args - Config Config `new-window:"+"` + Config *Config `new-window:"+"` // the network -- click to view / edit parameters for layers, paths, etc Net *axon.Network `new-window:"+" display:"no-inline"` @@ -208,7 +200,7 @@ type Sim struct { Loops *looper.Stacks `new-window:"+" display:"no-inline"` // the training patterns to use - Pats *table.Table `display:"no-inline"` + Pats *tensorfs.Node `display:"-"` // Environments Envs env.Envs `new-window:"+" display:"no-inline"` @@ -238,21 +230,20 @@ type Sim struct { RandSeeds randx.Seeds `display:"-"` } -// New creates new blank elements and initializes defaults -func (ss *Sim) New() { - econfig.Config(&ss.Config, "config.toml") +// RunSim runs the simulation. +func RunSim(cfg *Config) error { //cli:cmd -root + sim := &Sim{} + sim.Config = cfg + sim.Run() + return nil +} + +func (ss *Sim) Run() { ss.Root, _ = tensorfs.NewDir("Root") ss.Net = axon.NewNetwork("RA25") ss.Params.Config(LayerParams, PathParams, ss.Config.Params.Sheet, ss.Config.Params.Tag) - ss.Pats = table.New() ss.RandSeeds.Init(100) // max 100 runs ss.InitRandSeed(0) -} - -//////// Configs - -// ConfigAll configures all the elements using the standard functions -func (ss *Sim) ConfigAll() { if ss.Config.Run.GPU { axon.GPUInit() axon.UseGPU = true @@ -266,7 +257,12 @@ func (ss *Sim) ConfigAll() { if ss.Config.Params.SaveAll { ss.Config.Params.SaveAll = false ss.Net.SaveParamsSnapshot(&ss.Config, ss.Config.Params.Good) - os.Exit(0) + return + } + if ss.Config.GUI { + ss.RunGUI() + } else { + ss.RunNoGUI() } } @@ -281,13 +277,15 @@ func (ss *Sim) ConfigEnv() { tst = ss.Envs.ByMode(Test).(*env.FixedTable) } + pats := tensorfs.DirTable(ss.Pats, nil) + // note: names must be standard here! trn.Name = Train.String() - trn.Config(table.NewView(ss.Pats)) + trn.Config(table.NewView(pats)) trn.Validate() tst.Name = Test.String() - tst.Config(table.NewView(ss.Pats)) + tst.Config(table.NewView(pats)) tst.Sequential = true tst.Validate() @@ -510,27 +508,29 @@ func (ss *Sim) TestAll() { //////// Pats func (ss *Sim) ConfigPats() { - // dt := ss.Pats - // dt.SetMetaData("name", "TrainPats") - // dt.SetMetaData("desc", "Training patterns") - // dt.AddStringColumn("Name") - // dt.AddFloat32TensorColumn("Input", []int{5, 5}, "Y", "X") - // dt.AddFloat32TensorColumn("Output", []int{5, 5}, "Y", "X") - // dt.SetNumRows(25) - // - // patgen.PermutedBinaryMinDiff(dt.Columns[1].(*tensor.Float32), 6, 1, 0, 3) - // patgen.PermutedBinaryMinDiff(dt.Columns[2].(*tensor.Float32), 6, 1, 0, 3) - // dt.SaveCSV("random_5x5_25_gen.tsv", table.Tab, table.Headers) + dt := table.New() + metadata.SetName(dt, "TrainPats") + metadata.SetDoc(dt, "Training patterns") + dt.AddStringColumn("Name") + dt.AddFloat32Column("Input", 5, 5) + dt.AddFloat32Column("Output", 5, 5) + dt.SetNumRows(25) + + patgen.PermutedBinaryMinDiff(dt.ColumnByIndex(1).Tensor.(*tensor.Float32), 6, 1, 0, 3) + patgen.PermutedBinaryMinDiff(dt.ColumnByIndex(2).Tensor.(*tensor.Float32), 6, 1, 0, 3) + dt.SaveCSV("random_5x5_25_gen.tsv", tensor.Tab, table.Headers) + + ss.Pats = ss.Root.RecycleDir("Pats") + tensorfs.DirFromTable(ss.Pats, dt) } func (ss *Sim) OpenPats() { - dt := ss.Pats + dt := table.New() metadata.SetName(dt, "TrainPats") metadata.SetDoc(dt, "Training patterns") - err := dt.OpenCSV("random_5x5_25.tsv", tensor.Tab) - if err != nil { - log.Println(err) - } + errors.Log(dt.OpenCSV("random_5x5_25.tsv", tensor.Tab)) + ss.Pats = ss.Root.RecycleDir("Pats") + tensorfs.DirFromTable(ss.Pats, dt) } //////// Stats diff --git a/examples/ra25/typegen.go b/examples/ra25/typegen.go index 727295a1..40488bab 100644 --- a/examples/ra25/typegen.go +++ b/examples/ra25/typegen.go @@ -1,4 +1,4 @@ -// Code generated by "core generate -add-types"; DO NOT EDIT. +// Code generated by "core generate -add-types -add-funcs"; DO NOT EDIT. package main @@ -16,8 +16,12 @@ var _ = types.AddType(&types.Type{Name: "main.ParamConfig", IDName: "param-confi var _ = types.AddType(&types.Type{Name: "main.RunConfig", IDName: "run-config", Doc: "RunConfig has config parameters related to running the sim", Fields: []types.Field{{Name: "GPU", Doc: "use the GPU for computation -- generally faster even for small models if NData ~16"}, {Name: "NData", Doc: "number of data-parallel items to process in parallel per trial -- works (and is significantly faster) for both CPU and GPU. Results in an effective mini-batch of learning."}, {Name: "NThreads", Doc: "number of parallel threads for CPU computation -- 0 = use default"}, {Name: "Run", Doc: "starting run number -- determines the random seed -- runs counts from there -- can do all runs in parallel by launching separate jobs with each run, runs = 1"}, {Name: "NRuns", Doc: "total number of runs to do when running Train"}, {Name: "NEpochs", Doc: "total number of epochs per run"}, {Name: "NZero", Doc: "stop run after this number of perfect, zero-error epochs"}, {Name: "NTrials", Doc: "total number of trials per epoch. Should be an even multiple of NData."}, {Name: "TestInterval", Doc: "how often to run through all the test patterns, in terms of training epochs -- can use 0 or -1 for no testing"}, {Name: "PCAInterval", Doc: "how frequently (in epochs) to compute PCA on hidden representations to measure variance?"}, {Name: "StartWts", Doc: "if non-empty, is the name of weights file to load at start of first run -- for testing"}}}) -var _ = types.AddType(&types.Type{Name: "main.LogConfig", IDName: "log-config", Doc: "LogConfig has config parameters related to logging data", Fields: []types.Field{{Name: "SaveWeights", Doc: "if true, save final weights after each run"}, {Name: "Epoch", Doc: "if true, save train epoch log to file, as .epc.tsv typically"}, {Name: "Run", Doc: "if true, save run log to file, as .run.tsv typically"}, {Name: "Trial", Doc: "if true, save train trial log to file, as .trl.tsv typically. May be large."}, {Name: "TestEpoch", Doc: "if true, save testing epoch log to file, as .tst_epc.tsv typically. In general it is better to copy testing items over to the training epoch log and record there."}, {Name: "TestTrial", Doc: "if true, save testing trial log to file, as .tst_trl.tsv typically. May be large."}, {Name: "NetData", Doc: "if true, save network activation etc data from testing trials, for later viewing in netview"}}}) +var _ = types.AddType(&types.Type{Name: "main.LogConfig", IDName: "log-config", Doc: "LogConfig has config parameters related to logging data", Fields: []types.Field{{Name: "SaveWeights", Doc: "if true, save final weights after each run"}, {Name: "Epoch", Doc: "if true, save train epoch log to file, as .epc.tsv typically"}, {Name: "Run", Doc: "if true, save run log to file, as .run.tsv typically"}, {Name: "Trial", Doc: "if true, save train trial log to file, as .trl.tsv typically. May be large."}, {Name: "TestEpoch", Doc: "if true, save testing epoch log to file, as .tst_epc.tsv typically. In general it is better to copy testing items over to the training epoch log and record there."}, {Name: "TestTrial", Doc: "if true, save testing trial log to file, as .tst_trl.tsv typically. May be large."}}}) var _ = types.AddType(&types.Type{Name: "main.Config", IDName: "config", Doc: "Config is a standard Sim config -- use as a starting point.", Fields: []types.Field{{Name: "Includes", Doc: "specify include files here, and after configuration, it contains list of include files added"}, {Name: "GUI", Doc: "open the GUI -- does not automatically run -- if false, then runs automatically and quits"}, {Name: "Debug", Doc: "log debugging information"}, {Name: "Params", Doc: "parameter related configuration options"}, {Name: "Run", Doc: "sim running related configuration options"}, {Name: "Log", Doc: "data logging related configuration options"}}}) 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: "Config", Doc: "simulation configuration parameters -- set by .toml config file and / or args"}, {Name: "Net", Doc: "the network -- click to view / edit parameters for layers, paths, etc"}, {Name: "Params", Doc: "network parameter management"}, {Name: "Loops", Doc: "contains looper control loops for running sim"}, {Name: "Pats", Doc: "the training patterns to use"}, {Name: "Envs", Doc: "Environments"}, {Name: "TrainUpdate", Doc: "train mode netview update parameters"}, {Name: "TestUpdate", Doc: "test mode netview update parameters"}, {Name: "Root", Doc: "Root is the root data dir."}, {Name: "StatFuncs", Doc: "StatFuncs are statistics functions, per stat, handles everything."}, {Name: "Stats", Doc: "Stats has the stats dir."}, {Name: "Current", Doc: "Current has the current stats values."}, {Name: "GUI", Doc: "manages all the gui elements"}, {Name: "RandSeeds", Doc: "a list of random seeds to use for each run"}}}) + +var _ = types.AddFunc(&types.Func{Name: "main.main"}) + +var _ = types.AddFunc(&types.Func{Name: "main.RunSim", Doc: "RunSim runs the simulation.", Directives: []types.Directive{{Tool: "cli", Directive: "cmd", Args: []string{"-root"}}}, Args: []string{"cfg"}, Returns: []string{"error"}})