Skip to content

Commit

Permalink
weights saving and loading test, and update all tests to build at least
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Aug 13, 2024
1 parent d3173c7 commit 1547e5c
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 209 deletions.
167 changes: 79 additions & 88 deletions axon/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,58 +47,51 @@ const NLrnPars = 1

// Note: subsequent params applied after Base
var ParamSets = params.Sets{
"Base": {Desc: "base testing", Sheets: params.Sheets{
"Network": &params.Sheet{
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Gbar.L": "0.2",
"Layer.Learn.RLRate.On": "false",
"Layer.Inhib.Layer.FB": "0.5",
}},
{Sel: "Path", Desc: "for reproducibility, identical weights",
Params: params.Params{
"Path.SWts.Init.Var": "0",
}},
{Sel: ".BackPath", Desc: "top-down back-pathways MUST have lower relative weight scale, otherwise network hallucinates",
Params: params.Params{
"Path.PathScale.Rel": "0.2",
}},
},
"InhibOff": &params.Sheet{
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Gbar.L": "0.2",
"Layer.Inhib.Layer.On": "false",
}},
{Sel: ".InhibPath", Desc: "weaker inhib",
Params: params.Params{
"Path.PathScale.Abs": "0.1",
}},
},
}},
"FullDecay": {Desc: "decay state completely for ndata testing", Sheets: params.Sheets{
"Network": &params.Sheet{
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Decay.Act": "1",
"Layer.Acts.Decay.Glong": "1",
"Layer.Acts.Decay.AHP": "1",
}},
},
}},
"SubMean": {Desc: "submean on Path dwt", Sheets: params.Sheets{
"Network": &params.Sheet{
{Sel: "Path", Desc: "submean used in some models but not by default",
Params: params.Params{
"Path.Learn.Trace.SubMean": "1",
}},
},
}},
"Base": {
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Gbar.L": "0.2",
"Layer.Learn.RLRate.On": "false",
"Layer.Inhib.Layer.FB": "0.5",
}},
{Sel: "Path", Desc: "for reproducibility, identical weights",
Params: params.Params{
"Path.SWts.Init.Var": "0",
}},
{Sel: ".BackPath", Desc: "top-down back-pathways MUST have lower relative weight scale, otherwise network hallucinates",
Params: params.Params{
"Path.PathScale.Rel": "0.2",
}},
},
"InhibOff": &params.Sheet{
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Gbar.L": "0.2",
"Layer.Inhib.Layer.On": "false",
}},
{Sel: ".InhibPath", Desc: "weaker inhib",
Params: params.Params{
"Path.PathScale.Abs": "0.1",
}},
},
"FullDecay": {
{Sel: "Layer", Desc: "layer defaults",
Params: params.Params{
"Layer.Acts.Decay.Act": "1",
"Layer.Acts.Decay.Glong": "1",
"Layer.Acts.Decay.AHP": "1",
}},
},
"SubMean": {
{Sel: "Path", Desc: "submean used in some models but not by default",
Params: params.Params{
"Path.Learn.Trace.SubMean": "1",
}},
},
}

func newTestNet(ctx *Context, nData int) *Network {
var testNet Network
testNet.InitName(&testNet, "testNet")
testNet := NewNetwork("testNet")
testNet.SetRandSeed(42) // critical for ActAvg values
testNet.MaxData = uint32(nData)

Expand All @@ -117,16 +110,15 @@ func newTestNet(ctx *Context, nData int) *Network {
testNet.Build(ctx)
ctx.NetIndexes.NData = uint32(nData)
testNet.Defaults()
testNet.ApplyParams(ParamSets["Base"].Sheets["Network"], false) // false) // true) // no msg
testNet.InitWeights(ctx) // get GScale here
testNet.ApplyParams(ParamSets["Base"], false) // false) // true) // no msg
testNet.InitWeights(ctx) // get GScale here
testNet.NewState(ctx)
return &testNet
return testNet
}

// full connectivity
func newTestNetFull(ctx *Context, nData int) *Network {
var testNet Network
testNet.InitName(&testNet, "testNet")
testNet := NewNetwork("testNetFull")
testNet.SetRandSeed(42) // critical for ActAvg values
testNet.MaxData = uint32(nData)

Expand All @@ -143,18 +135,18 @@ func newTestNetFull(ctx *Context, nData int) *Network {
testNet.Build(ctx)
ctx.NetIndexes.NData = uint32(nData)
testNet.Defaults()
testNet.ApplyParams(ParamSets["Base"].Sheets["Network"], false) // false) // true) // no msg
testNet.InitWeights(ctx) // get GScale here
testNet.ApplyParams(ParamSets["Base"], false) // false) // true) // no msg
testNet.InitWeights(ctx) // get GScale here
testNet.NewState(ctx)
return &testNet
return testNet
}

func TestSynValues(t *testing.T) {
tol := Tol8
ctx := NewContext()
testNet := newTestNet(ctx, 1)
hidLay := testNet.LayerByName("Hidden")
p, err := hidLay.SendNameTry("Input")
p, err := hidLay.RecvPathBySendName("Input")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -218,7 +210,7 @@ func TestSpikeProp(t *testing.T) {

net.Build(ctx)
net.Defaults()
net.ApplyParams(ParamSets["Base"].Sheets["Network"], false)
net.ApplyParams(ParamSets["Base"], false)

net.InitExt(ctx)

Expand Down Expand Up @@ -564,7 +556,7 @@ func ReportValDiffs(t *testing.T, tolerance float32, va, vb map[string]float32,
func NetDebugAct(t *testing.T, printValues bool, gpu bool, nData int, initWts bool) map[string]float32 {
ctx := NewContext()
testNet := newTestNet(ctx, nData)
testNet.ApplyParams(ParamSets.SetByName("FullDecay").Sheets["Network"], false)
testNet.ApplyParams(ParamSets["FullDecay"], false)
return RunDebugAct(t, ctx, testNet, printValues, gpu, initWts)
}

Expand Down Expand Up @@ -739,7 +731,7 @@ func NetTestLearn(t *testing.T, tol float32, gpu bool) {
cycPerQtr := 50

testNet.Defaults()
testNet.ApplyParams(ParamSets["Base"].Sheets["Network"], false) // always apply base
testNet.ApplyParams(ParamSets["Base"], false) // always apply base
testNet.InitWeights(ctx)
testNet.InitExt(ctx)

Expand Down Expand Up @@ -911,7 +903,7 @@ func NetTestRLRate(t *testing.T, tol float32, gpu bool) {
cycPerQtr := 50

testNet.Defaults()
testNet.ApplyParams(ParamSets["Base"].Sheets["Network"], false) // always apply base
testNet.ApplyParams(ParamSets["Base"], false) // always apply base
hidLay.Params.Learn.RLRate.On.SetBool(true)
testNet.InitWeights(ctx)
testNet.InitExt(ctx)
Expand Down Expand Up @@ -1029,10 +1021,10 @@ func NetDebugLearn(t *testing.T, printValues bool, gpu bool, maxData, nData int,
} else {
testNet = newTestNet(ctx, maxData)
}
testNet.ApplyParams(ParamSets.SetByName("FullDecay").Sheets["Network"], false)
testNet.ApplyParams(ParamSets["FullDecay"], false)

if submean {
testNet.ApplyParams(ParamSets.SetByName("SubMean").Sheets["Network"], false)
testNet.ApplyParams(ParamSets["SubMean"], false)
}

ctx.NetIndexes.NData = uint32(nData)
Expand Down Expand Up @@ -1267,32 +1259,31 @@ func TestInhibAct(t *testing.T) {
tol := Tol6

inPats := newInPats()
var InhibNet Network
InhibNet.InitName(&InhibNet, "InhibNet")
InhibNet.SetRandSeed(42) // critical for ActAvg values
inhibNet := NewNetwork("InhibNet")
inhibNet.SetRandSeed(42) // critical for ActAvg values

inLay := InhibNet.AddLayer("Input", []int{4, 1}, InputLayer)
hidLay := InhibNet.AddLayer("Hidden", []int{4, 1}, SuperLayer)
outLay := InhibNet.AddLayer("Output", []int{4, 1}, TargetLayer)
inLay := inhibNet.AddLayer("Input", []int{4, 1}, InputLayer)
hidLay := inhibNet.AddLayer("Hidden", []int{4, 1}, SuperLayer)
outLay := inhibNet.AddLayer("Output", []int{4, 1}, TargetLayer)

one2one := paths.NewOneToOne()

InhibNet.ConnectLayers(inLay, hidLay, one2one, ForwardPath)
InhibNet.ConnectLayers(inLay, hidLay, one2one, InhibPath)
InhibNet.ConnectLayers(hidLay, outLay, one2one, ForwardPath)
InhibNet.ConnectLayers(outLay, hidLay, one2one, BackPath)
inhibNet.ConnectLayers(inLay, hidLay, one2one, ForwardPath)
inhibNet.ConnectLayers(inLay, hidLay, one2one, InhibPath)
inhibNet.ConnectLayers(hidLay, outLay, one2one, ForwardPath)
inhibNet.ConnectLayers(outLay, hidLay, one2one, BackPath)

ctx := NewContext()

InhibNet.Build(ctx)
InhibNet.Defaults()
InhibNet.ApplyParams(ParamSets["Base"].Sheets["Network"], false)
InhibNet.ApplyParams(ParamSets["Base"].Sheets["InhibOff"], false)
InhibNet.InitWeights(ctx) // get GScale
InhibNet.NewState(ctx)
inhibNet.Build(ctx)
inhibNet.Defaults()
inhibNet.ApplyParams(ParamSets["Base"], false)
inhibNet.ApplyParams(ParamSets["Base"], false)
inhibNet.InitWeights(ctx) // get GScale
inhibNet.NewState(ctx)

InhibNet.InitWeights(ctx)
InhibNet.InitExt(ctx)
inhibNet.InitWeights(ctx)
inhibNet.InitExt(ctx)

printCycs := false
printQtrs := false
Expand Down Expand Up @@ -1326,11 +1317,11 @@ func TestInhibAct(t *testing.T) {
inLay.ApplyExt(ctx, 0, inpat)
outLay.ApplyExt(ctx, 0, inpat)

InhibNet.NewState(ctx)
inhibNet.NewState(ctx)
ctx.NewState(etime.Train)
for qtr := 0; qtr < 4; qtr++ {
for cyc := 0; cyc < cycPerQtr; cyc++ {
InhibNet.Cycle(ctx)
inhibNet.Cycle(ctx)
ctx.CycleInc()

if printCycs {
Expand All @@ -1345,9 +1336,9 @@ func TestInhibAct(t *testing.T) {
}
}
if qtr == 2 {
InhibNet.MinusPhase(ctx)
inhibNet.MinusPhase(ctx)
ctx.NewPhase(false)
InhibNet.PlusPhaseStart(ctx)
inhibNet.PlusPhaseStart(ctx)
}

if printCycs && printQtrs {
Expand Down Expand Up @@ -1387,7 +1378,7 @@ func TestInhibAct(t *testing.T) {
CompareFloats(tol, outGis, qtr3OutGis, "qtr3OutGis", t)
}
}
InhibNet.PlusPhase(ctx)
inhibNet.PlusPhase(ctx)

if printQtrs {
fmt.Printf("=============================\n")
Expand All @@ -1397,7 +1388,7 @@ func TestInhibAct(t *testing.T) {

func saveToFile(net *Network, t *testing.T) {
var buf bytes.Buffer
net.WriteWtsJSON(&buf)
net.WriteWeightsJSON(&buf)
wb := buf.Bytes()
fmt.Printf("testNet Trained Weights:\n\n%v\n", string(wb))

Expand Down
8 changes: 4 additions & 4 deletions axon/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ func TestLayerToJson(t *testing.T) {
fh, err := os.Create(filename)
require.NoError(t, err)
bw := bufio.NewWriter(fh)
hiddenLayer.WriteWtsJSON(bw, 0)
hiddenLayer.WriteWeightsJSON(bw, 0)
assert.NoError(t, bw.Flush())
assert.NoError(t, fh.Close())

// load from JSON
fh, err = os.Open(filename)
require.NoError(t, err)
br := bufio.NewReader(fh)
assert.NoError(t, hiddenLayerC.ReadWtsJSON(br))
assert.NoError(t, hiddenLayerC.ReadWeightsJSON(br))
assert.NoError(t, fh.Close())

// make sure the synapse weights are the same
Expand All @@ -146,8 +146,8 @@ func TestLayerToJson(t *testing.T) {
varIndex, _ := origProj.SynVarIndex("Wt")
assert.Equal(t, origProj.NumSyns(), copyProj.NumSyns())
for idx := 0; idx < origProj.NumSyns(); idx++ {
origWeight := origProj.SynVal1D(varIndex, idx)
copyWeight := copyProj.SynVal1D(varIndex, idx)
origWeight := origProj.SynValue1D(varIndex, idx)
copyWeight := copyProj.SynValue1D(varIndex, idx)
assert.InDelta(t, origWeight, copyWeight, 0.001)
}

Expand Down
4 changes: 2 additions & 2 deletions axon/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
func TestNewNetwork(t *testing.T) {
testNet := NewNetwork("testNet")
assert.Equal(t, "testNet", testNet.Name)
assert.Equal(t, 0, testNet.NLayers())
assert.Equal(t, 0, testNet.NumLayers())
assert.IsType(t, &Network{}, testNet)
}

func TestInterfaceType(t *testing.T) {
testNet := NewNetwork("testNet")
_, ok := testNet.EmerNet.(emer.Network)
_, ok := testNet.EmerNetwork.(emer.Network)
assert.True(t, ok)
}
Loading

0 comments on commit 1547e5c

Please sign in to comment.