forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_reduction_test.go
197 lines (155 loc) · 3.96 KB
/
op_reduction_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gorgonia
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSumOp(t *testing.T) {
assert := assert.New(t)
// var g *ExprGraph
var z, sz *Node
var grads Nodes
var err error
var op sumOp
_, _, _, z = simpleVecEqn()
sz = Must(Sum(z))
// t.Logf(" %v %v %v %v", g, x, y, z)
diffWRT := sz.diffWRT()
assert.Equal([]bool{true}, diffWRT)
op = sz.op.(sumOp)
grads, err = op.SymDiff(Nodes{z}, sz, onef64)
assert.Nil(err)
assert.Equal(1, len(grads))
t.Logf("%v", grads[0])
}
func TestSumOpDiff(t *testing.T) {
defer runtime.GC()
assert := assert.New(t)
var g, g2 *ExprGraph
var x, y, z, a, b, c *Node
// var x, y, a, b *Node
var xG, yG, aG, bG Value
// var xG, aG Value
// var prog *program
// var locMap map[*Node]register
var m *tapeMachine
var m2 *lispMachine
var err error
// Basic Test case: a vector is summed
g = NewGraph()
x = NewVector(g, Float64, WithName("x"), WithShape(5), WithInit(RangedFrom(0)))
y = Must(Sum(x))
WithName("y")(y)
Grad(y, x)
// ioutil.WriteFile("SumOp.dot", []byte(g.ToDot()), 0644)
m = NewTapeMachine(g)
defer m.Close()
if err = m.RunAll(); err != nil {
t.Error(err)
}
g2 = NewGraph()
a = NewVector(g2, Float64, WithShape(5), WithInit(RangedFrom(0)))
b = Must(Sum(a))
m2 = NewLispMachine(g2, WithWatchlist())
defer m2.Close()
if err = m2.RunAll(); err != nil {
t.Error(err)
}
if aG, err = a.Grad(); err != nil {
t.Error(err)
}
if xG, err = x.Grad(); err != nil {
t.Error(err)
}
if bG, err = b.Grad(); err != nil {
t.Error(err)
}
if yG, err = y.Grad(); err != nil {
t.Error(err)
}
assert.True(ValueEq(x.Value(), a.Value()))
assert.True(ValueEq(xG, aG))
assert.True(ValueEq(y.Value(), b.Value()))
assert.True(ValueEq(yG, bG))
// long standing bug: sometimes the derivation will get executed in the machine first
// for example, the deriv of y is 1, and occasionally, the machine will choose to
// execute const 1 into register 0
// It would then fail to bind to y's boundTo, because at that point in time, y is still unknown.
// assert.Equal(y.Grad(), b.Grad())
// Slightly more advanced test case: A matrix is summed
g = NewGraph()
x = NewMatrix(g, Float64, WithName("x"), WithShape(11, 7), WithInit(RangedFrom(0)))
y = Must(Sum(x))
WithName("y")(y)
Grad(y, x)
m = NewTapeMachine(g)
defer m.Close()
if err = m.RunAll(); err != nil {
t.Error(err)
}
g2 = NewGraph()
a = NewMatrix(g2, Float64, WithName("x"), WithShape(11, 7), WithInit(RangedFrom(0)))
b = Must(Sum(a))
m2 = NewLispMachine(g2)
defer m2.Close()
if err = m2.RunAll(); err != nil {
t.Error(err)
}
if aG, err = a.Grad(); err != nil {
t.Error(err)
}
if xG, err = x.Grad(); err != nil {
t.Error(err)
}
if bG, err = b.Grad(); err != nil {
t.Error(err)
}
if yG, err = y.Grad(); err != nil {
t.Error(err)
}
assert.True(ValueEq(x.Value(), a.Value()))
assert.True(ValueEq(xG, aG))
assert.True(ValueEq(y.Value(), b.Value()))
assert.True(ValueEq(yG, bG))
/* Sum is not the root node */
g = NewGraph()
x = NewMatrix(g, Float64, WithName("x"), WithShape(11, 7), WithInit(RangedFrom(0)))
y = Must(Sum(x))
z = Must(Add(y, twof64))
if _, err = Grad(z, x); err != nil {
t.Fatal(err)
}
m = NewTapeMachine(g)
defer m.Close()
if err = m.RunAll(); err != nil {
t.Errorf("%v", m.Prog())
t.Error(err)
}
g2 = NewGraph()
a = NewMatrix(g2, Float64, WithName("x"), WithShape(11, 7), WithInit(RangedFrom(0)))
b = Must(Sum(a))
c = Must(Add(b, twof64))
m2 = NewLispMachine(g2)
defer m2.Close()
if err = m2.RunAll(); err != nil {
t.Fatalf("%+v", err)
}
if aG, err = a.Grad(); err != nil {
t.Error(err)
}
if xG, err = x.Grad(); err != nil {
t.Error(err)
}
if bG, err = b.Grad(); err != nil {
t.Error(err)
}
if yG, err = b.Grad(); err != nil {
t.Error(err)
}
assert.True(ValueEq(x.Value(), a.Value()))
assert.True(ValueEq(xG, aG))
assert.True(ValueEq(y.Value(), b.Value()))
assert.True(ValueEq(yG, bG))
assert.True(ValueEq(z.Value(), c.Value()))
runtime.GC()
}