forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolvers_test.go
155 lines (120 loc) · 3.13 KB
/
solvers_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
package gorgonia
import (
"math"
"testing"
"github.com/chewxy/gorgonia/tensor"
"github.com/chewxy/math32"
"github.com/stretchr/testify/assert"
)
func clampFloat64(v, min, max float64) float64 {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func clampFloat32(v, min, max float32) float32 {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func tf64Node() Nodes {
backingV := []float64{1, 2, 3, 4}
backingD := []float64{0.5, -10, 10, 0.5}
v := tensor.New(tensor.WithBacking(backingV), tensor.WithShape(2, 2))
d := tensor.New(tensor.WithBacking(backingD), tensor.WithShape(2, 2))
dv := dvUnit0(v)
dv.d = d
n := new(Node)
n.boundTo = dv
model := Nodes{n}
return model
}
func tf32Node() Nodes {
backingV := []float32{1, 2, 3, 4}
backingD := []float32{0.5, -10, 10, 0.5}
v := tensor.New(tensor.WithBacking(backingV), tensor.WithShape(2, 2))
d := tensor.New(tensor.WithBacking(backingD), tensor.WithShape(2, 2))
dv := dvUnit0(v)
dv.d = d
n := new(Node)
n.boundTo = dv
model := Nodes{n}
return model
}
func manualRMSProp64(t *testing.T, s *RMSPropSolver, model Nodes) {
assert := assert.New(t)
correct := make([]float64, 4)
cached := make([]float64, 4)
grad0, _ := model[0].Grad()
backingV := model[0].Value().Data().([]float64)
backingD := grad0.Data().([]float64)
for i := 0; i < 5; i++ {
for j, v := range backingV {
grad := backingD[j]
cw := cached[j]
decayed := cw*s.decay + (1.0-s.decay)*grad*grad
cached[j] = decayed
grad = clampFloat64(grad, -s.clip, s.clip)
upd := -s.eta*grad/math.Sqrt(decayed+s.eps) - s.l2reg*v
correct[j] = v + upd
}
err := s.Step(model)
if err != nil {
t.Error(err)
}
sCache := s.cache[0].Value.(tensor.Tensor)
assert.Equal(correct, backingV, "Iteration: %d", i)
assert.Equal(cached, sCache.Data(), "Iteration: %d", i)
}
}
func manualRMSProp32(t *testing.T, s *RMSPropSolver, model Nodes) {
assert := assert.New(t)
correct := make([]float32, 4)
cached := make([]float32, 4)
grad0, _ := model[0].Grad()
backingV := model[0].Value().Data().([]float32)
backingD := grad0.Data().([]float32)
decay := float32(s.decay)
l2reg := float32(s.l2reg)
eta := float32(s.eta)
eps := float32(s.eps)
clip := float32(s.clip)
for i := 0; i < 5; i++ {
for j, v := range backingV {
grad := backingD[j]
cw := cached[j]
decayed := cw*decay + (1.0-decay)*grad*grad
cached[j] = decayed
grad = clampFloat32(grad, -clip, clip)
upd := -eta*grad/math32.Sqrt(decayed+eps) - l2reg*v
correct[j] = v + upd
}
err := s.Step(model)
if err != nil {
t.Error(err)
}
sCache := s.cache[0].Value.(tensor.Tensor)
assert.True(floatsEqual32(correct, backingV))
assert.True(floatsEqual32(cached, sCache.Data().([]float32)))
}
}
func TestRMSPropSolver(t *testing.T) {
stepSize := 0.01
l2Reg := 0.000001
clip := 5.0
var s *RMSPropSolver
var model Nodes
s = NewRMSPropSolver(WithLearnRate(stepSize), WithL2Reg(l2Reg), WithClip(clip))
model = tf64Node()
manualRMSProp64(t, s, model)
s = NewRMSPropSolver(WithLearnRate(stepSize), WithL2Reg(l2Reg), WithClip(clip))
model = tf32Node()
manualRMSProp32(t, s, model)
}