Skip to content

Commit

Permalink
in progress fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed May 1, 2024
1 parent 21a082a commit 452fae9
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 96 deletions.
32 changes: 20 additions & 12 deletions decoder/linear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,50 +94,58 @@ func TestLinearLogistic(t *testing.T) {

func TestInputPool1D(t *testing.T) {
dec := Linear{}
shape := tensor.NewShape([]int{1, 5, 6, 6}, nil, nil)
shape := tensor.NewShape([]int{1, 5, 6, 6})
vals := make([]float32, shape.Len())
for i := range vals {
vals[i] = float32(i)
}
tensor := tensor.NewFloat32Shape(shape, vals)
layer := TestLayer{tensors: map[string]tensor.Tensor{"var0": tensor}}
tsr := tensor.NewFloat32(shape.Sizes)
tsr.SetNumRows(1)
for i := range tsr.Values {
tsr.Values[i] = vals[i]
}
layer := TestLayer{tensors: map[string]tensor.Tensor{"var0": tsr}}
dec.InitPool(2, &layer, 0, IdentityFunc)
dec.Input("var0", 0)
expected := tensor.SubSpace([]int{0, 0}).(*tensor.Float32).Values
expected := tsr.SubSpace([]int{0, 0}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)

dec.InitPool(2, &layer, 1, IdentityFunc)
dec.Input("var0", 0)
expected = tensor.SubSpace([]int{0, 1}).(*tensor.Float32).Values
expected = tsr.SubSpace([]int{0, 1}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
}

func TestInputPool2D(t *testing.T) {
dec := Linear{}
shape := tensor.NewShape([]int{2, 5, 6, 6}, nil, nil)
shape := tensor.NewShape([]int{2, 5, 6, 6})
vals := make([]float32, shape.Len())
for i := range vals {
vals[i] = float32(i)
}
tensor := tensor.NewFloat32Shape(shape, vals)
layer := TestLayer{tensors: map[string]tensor.Tensor{"var0": tensor}}
tsr := tensor.NewFloat32(shape.Sizes)
for i := range tsr.Values {
tsr.Values[i] = vals[i]
}

layer := TestLayer{tensors: map[string]tensor.Tensor{"var0": tsr}}
dec.InitPool(2, &layer, 0, IdentityFunc)
dec.Input("var0", 0)
expected := tensor.SubSpace([]int{0, 0}).(*tensor.Float32).Values
expected := tsr.SubSpace([]int{0, 0}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)

dec.InitPool(2, &layer, 1, IdentityFunc)
dec.Input("var0", 0)
expected = tensor.SubSpace([]int{0, 1}).(*tensor.Float32).Values
expected = tsr.SubSpace([]int{0, 1}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)

dec.InitPool(2, &layer, 5, IdentityFunc)
dec.Input("var0", 0)
expected = tensor.SubSpace([]int{1, 0}).(*tensor.Float32).Values
expected = tsr.SubSpace([]int{1, 0}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)

dec.InitPool(2, &layer, 9, IdentityFunc)
dec.Input("var0", 0)
expected = tensor.SubSpace([]int{1, 4}).(*tensor.Float32).Values
expected = tsr.SubSpace([]int{1, 4}).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
}
2 changes: 1 addition & 1 deletion empi/mpi/printf.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Println(fs ...any) {
if WorldRank() > 0 {
AllPrintln(fs...)
} else {
fmt.Println(fs)
fmt.Println(fs...)
}
}

Expand Down
101 changes: 45 additions & 56 deletions erand/dists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ import (
"math"
"testing"

"cogentcore.org/core/tensor"
"cogentcore.org/core/tensor/stats/stats"
"cogentcore.org/core/tensor/table"
)

func TestGaussianGen(t *testing.T) {
nsamp := int(1e6)
sch := table.Schema{
{"Val", reflect.Float32, nil, nil},
}
dt := &table.Table{}
dt.SetFromSchema(sch, nsamp)
dt.AddFloat32Column("Val")
dt.SetNumRows(nsamp)

mean := 0.5
sig := 0.25
Expand All @@ -30,15 +27,15 @@ func TestGaussianGen(t *testing.T) {
dt.SetFloat("Val", i, vl)
}
ix := table.NewIndexView(dt)
desc := agg.DescAll(ix)
desc := stats.DescAll(ix)

meanRow := desc.RowsByString("Agg", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Agg", "Std", table.Equals, table.UseCase)[0]
// minRow := desc.RowsByString("Agg", "Min", table.Equals, table.UseCase)[0]
// maxRow := desc.RowsByString("Agg", "Max", table.Equals, table.UseCase)[0]
meanRow := desc.RowsByString("Stat", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Stat", "Std", table.Equals, table.UseCase)[0]
// minRow := desc.RowsByString("Stat", "Min", table.Equals, table.UseCase)[0]
// maxRow := desc.RowsByString("Stat", "Max", table.Equals, table.UseCase)[0]

actMean := desc.CellFloat("Val", meanRow)
actStd := desc.CellFloat("Val", stdRow)
actMean := desc.Float("Val", meanRow)
actStd := desc.Float("Val", stdRow)

if math.Abs(actMean-mean) > tol {
t.Errorf("Gaussian: mean %g\t out of tolerance vs target: %g\n", actMean, mean)
Expand All @@ -53,11 +50,9 @@ func TestGaussianGen(t *testing.T) {

func TestBinomialGen(t *testing.T) {
nsamp := int(1e6)
sch := table.Schema{
{"Val", reflect.Float32, nil, nil},
}
dt := &table.Table{}
dt.SetFromSchema(sch, nsamp)
dt.AddFloat32Column("Val")
dt.SetNumRows(nsamp)

n := 1.0
p := 0.5
Expand All @@ -68,17 +63,17 @@ func TestBinomialGen(t *testing.T) {
dt.SetFloat("Val", i, vl)
}
ix := table.NewIndexView(dt)
desc := agg.DescAll(ix)
desc := stats.DescAll(ix)

meanRow := desc.RowsByString("Agg", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Agg", "Std", table.Equals, table.UseCase)[0]
minRow := desc.RowsByString("Agg", "Min", table.Equals, table.UseCase)[0]
maxRow := desc.RowsByString("Agg", "Max", table.Equals, table.UseCase)[0]
meanRow := desc.RowsByString("Stat", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Stat", "Std", table.Equals, table.UseCase)[0]
minRow := desc.RowsByString("Stat", "Min", table.Equals, table.UseCase)[0]
maxRow := desc.RowsByString("Stat", "Max", table.Equals, table.UseCase)[0]

actMean := desc.CellFloat("Val", meanRow)
actStd := desc.CellFloat("Val", stdRow)
actMin := desc.CellFloat("Val", minRow)
actMax := desc.CellFloat("Val", maxRow)
actMean := desc.Float("Val", meanRow)
actStd := desc.Float("Val", stdRow)
actMin := desc.Float("Val", minRow)
actMax := desc.Float("Val", maxRow)

mean := n * p
if math.Abs(actMean-mean) > tol {
Expand All @@ -101,11 +96,9 @@ func TestBinomialGen(t *testing.T) {

func TestPoissonGen(t *testing.T) {
nsamp := int(1e6)
sch := table.Schema{
{"Val", reflect.Float32, nil, nil},
}
dt := &table.Table{}
dt.SetFromSchema(sch, nsamp)
dt.AddFloat32Column("Val")
dt.SetNumRows(nsamp)

lambda := 10.0
tol := 1e-2
Expand All @@ -115,17 +108,17 @@ func TestPoissonGen(t *testing.T) {
dt.SetFloat("Val", i, vl)
}
ix := table.NewIndexView(dt)
desc := agg.DescAll(ix)
desc := stats.DescAll(ix)

meanRow := desc.RowsByString("Agg", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Agg", "Std", table.Equals, table.UseCase)[0]
minRow := desc.RowsByString("Agg", "Min", table.Equals, table.UseCase)[0]
// maxRow := desc.RowsByString("Agg", "Max", table.Equals, table.UseCase)[0]
meanRow := desc.RowsByString("Stat", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Stat", "Std", table.Equals, table.UseCase)[0]
minRow := desc.RowsByString("Stat", "Min", table.Equals, table.UseCase)[0]
// maxRow := desc.RowsByString("Stat", "Max", table.Equals, table.UseCase)[0]

actMean := desc.CellFloat("Val", meanRow)
actStd := desc.CellFloat("Val", stdRow)
actMin := desc.CellFloat("Val", minRow)
// actMax := desc.CellFloat("Val", maxRow)
actMean := desc.Float("Val", meanRow)
actStd := desc.Float("Val", stdRow)
actMin := desc.Float("Val", minRow)
// actMax := desc.Float("Val", maxRow)

mean := lambda
if math.Abs(actMean-mean) > tol {
Expand All @@ -148,11 +141,9 @@ func TestPoissonGen(t *testing.T) {

func TestGammaGen(t *testing.T) {
nsamp := int(1e6)
sch := table.Schema{
{"Val", reflect.Float32, nil, nil},
}
dt := &table.Table{}
dt.SetFromSchema(sch, nsamp)
dt.AddFloat32Column("Val")
dt.SetNumRows(nsamp)

alpha := 0.5
beta := 0.8
Expand All @@ -163,13 +154,13 @@ func TestGammaGen(t *testing.T) {
dt.SetFloat("Val", i, vl)
}
ix := table.NewIndexView(dt)
desc := agg.DescAll(ix)
desc := stats.DescAll(ix)

meanRow := desc.RowsByString("Agg", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Agg", "Std", table.Equals, table.UseCase)[0]
meanRow := desc.RowsByString("Stat", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Stat", "Std", table.Equals, table.UseCase)[0]

actMean := desc.CellFloat("Val", meanRow)
actStd := desc.CellFloat("Val", stdRow)
actMean := desc.Float("Val", meanRow)
actStd := desc.Float("Val", stdRow)

mean := alpha / beta
if math.Abs(actMean-mean) > tol {
Expand All @@ -186,11 +177,9 @@ func TestGammaGen(t *testing.T) {

func TestBetaGen(t *testing.T) {
nsamp := int(1e6)
sch := table.Schema{
{"Val", reflect.Float32, nil, nil},
}
dt := &table.Table{}
dt.SetFromSchema(sch, nsamp)
dt.AddFloat32Column("Val")
dt.SetNumRows(nsamp)

alpha := 0.5
beta := 0.8
Expand All @@ -201,13 +190,13 @@ func TestBetaGen(t *testing.T) {
dt.SetFloat("Val", i, vl)
}
ix := table.NewIndexView(dt)
desc := agg.DescAll(ix)
desc := stats.DescAll(ix)

meanRow := desc.RowsByString("Agg", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Agg", "Std", table.Equals, table.UseCase)[0]
meanRow := desc.RowsByString("Stat", "Mean", table.Equals, table.UseCase)[0]
stdRow := desc.RowsByString("Stat", "Std", table.Equals, table.UseCase)[0]

actMean := desc.CellFloat("Val", meanRow)
actStd := desc.CellFloat("Val", stdRow)
actMean := desc.Float("Val", meanRow)
actStd := desc.Float("Val", stdRow)

mean := alpha / (alpha + beta)
if math.Abs(actMean-mean) > tol {
Expand Down
Loading

0 comments on commit 452fae9

Please sign in to comment.