-
Notifications
You must be signed in to change notification settings - Fork 24
/
linear_test.go
151 lines (133 loc) · 3.47 KB
/
linear_test.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
// Copyright (c) 2021, 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.
package decoder
import (
"fmt"
"testing"
"cogentcore.org/lab/tensor"
"github.com/stretchr/testify/assert"
)
// TestLayer implements a Layer
type TestLayer struct {
tensors map[string]tensor.Values
}
func (tl *TestLayer) Name() string {
return "TestLayer"
}
func (tl *TestLayer) UnitValuesTensor(tsr tensor.Values, varNm string, di int) error {
src, ok := tl.tensors[varNm]
if !ok {
return fmt.Errorf("bad key: %s", varNm)
}
tensor.SetShapeFrom(tsr, src)
tsr.CopyFrom(src)
return nil
}
func (tl *TestLayer) Shape() *tensor.Shape {
for _, v := range tl.tensors {
return v.Shape()
}
return nil
}
func testLinear(t *testing.T, activationFn ActivationFunc) {
const tol = 1.0e-6
dec := Linear{}
dec.Init(2, 2, -1, activationFn)
trgs := []float32{0, 1}
outs := []float32{0, 0}
var lastSSE float32
for i := 0; i < 100; i++ {
if i%2 == 0 {
dec.Inputs[0] = 1
dec.Inputs[1] = 0
trgs[0] = 1
trgs[1] = 0
} else {
dec.Inputs[0] = 0
dec.Inputs[1] = 1
trgs[0] = 0
trgs[1] = 1
}
dec.Forward()
dec.Output(&outs)
if i > 2 {
if i%2 == 0 {
if outs[0] < outs[1] {
t.Errorf("err: %d\t output: %g !> other: %g\n", i, outs[0], outs[1])
}
} else {
if outs[1] < outs[0] {
t.Errorf("err: %d\t output: %g !> other: %g\n", i, outs[1], outs[0])
}
}
}
sse, err := dec.Train(trgs)
if err != nil {
t.Error(err)
}
if i > 2 {
if (sse - lastSSE) > tol {
t.Errorf("error: %d\t sse now is *larger* than previoust: %g > %g\n", i, sse, lastSSE)
}
}
lastSSE = sse
}
}
func TestLinearIdentity(t *testing.T) {
testLinear(t, IdentityFunc)
}
func TestLinearLogistic(t *testing.T) {
testLinear(t, LogisticFunc)
}
func TestInputPool1D(t *testing.T) {
dec := Linear{}
shape := tensor.NewShape(1, 5, 6, 6)
vals := make([]float32, shape.Len())
for i := range vals {
vals[i] = float32(i)
}
tsr := tensor.NewFloat32(shape.Sizes...)
tsr.SetNumRows(1)
for i := range tsr.Values {
tsr.Values[i] = vals[i]
}
layer := TestLayer{tensors: map[string]tensor.Values{"var0": tsr}}
dec.InitPool(2, &layer, 0, IdentityFunc)
dec.Input("var0", 0)
expected := tsr.SubSpace(0, 0).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
dec.InitPool(2, &layer, 1, IdentityFunc)
dec.Input("var0", 0)
expected = tsr.SubSpace(0, 1).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
}
func TestInputPool2D(t *testing.T) {
dec := Linear{}
shape := tensor.NewShape(2, 5, 6, 6)
vals := make([]float32, shape.Len())
for i := range vals {
vals[i] = float32(i)
}
tsr := tensor.NewFloat32(shape.Sizes...)
for i := range tsr.Values {
tsr.Values[i] = vals[i]
}
layer := TestLayer{tensors: map[string]tensor.Values{"var0": tsr}}
dec.InitPool(2, &layer, 0, IdentityFunc)
dec.Input("var0", 0)
expected := tsr.SubSpace(0, 0).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
dec.InitPool(2, &layer, 1, IdentityFunc)
dec.Input("var0", 0)
expected = tsr.SubSpace(0, 1).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
dec.InitPool(2, &layer, 5, IdentityFunc)
dec.Input("var0", 0)
expected = tsr.SubSpace(1, 0).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
dec.InitPool(2, &layer, 9, IdentityFunc)
dec.Input("var0", 0)
expected = tsr.SubSpace(1, 4).(*tensor.Float32).Values
assert.Equal(t, expected, dec.Inputs)
}