forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_concurrent_training_test.go
235 lines (198 loc) · 5.15 KB
/
example_concurrent_training_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gorgonia_test
import (
"fmt"
"runtime"
"sync"
. "gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
const (
// rows = 373127
// cols = 53
// We'll use a nice even sized batch size, instead of weird prime numbers
rows = 30000
cols = 5
batchSize = 100
epochs = 10
)
type concurrentTrainer struct {
g *ExprGraph
x, y *Node
vm VM
// cost Value
batchSize int
epoch int // number of epochs done
}
func newConcurrentTrainer() *concurrentTrainer {
g := NewGraph()
x := NewMatrix(g, Float64, WithShape(batchSize, cols), WithName("x"))
y := NewVector(g, Float64, WithShape(batchSize), WithName("y"))
xT := Must(Transpose(x))
z := Must(Mul(xT, y))
sz := Must(Sum(z))
// Read(sz, &ct.cost)
Grad(sz, x, y)
vm := NewTapeMachine(g, BindDualValues(x, y))
return &concurrentTrainer{
g: g,
x: x,
y: y,
vm: vm,
batchSize: batchSize,
epoch: -1,
}
}
type cost struct {
Nodes []ValueGrad
VM // placed for debugging purposes. In real life use you can just use a channel of Nodes
// cost Value
}
func (t *concurrentTrainer) train(x, y Value, costChan chan cost, wg *sync.WaitGroup) {
Let(t.x, x)
Let(t.y, y)
if err := t.vm.RunAll(); err != nil {
panic("HELP")
}
costChan <- cost{
[]ValueGrad{t.x, t.y},
t.vm,
// t.cost,
}
t.vm.Reset()
wg.Done()
}
func trainEpoch(bs []batch, ts []*concurrentTrainer, threads int) {
// costs := make([]float64, 0, len(bs))
chunks := len(bs) / len(ts)
for chunk := 0; chunk < chunks; chunk++ {
costChan := make(chan cost, len(bs))
var wg sync.WaitGroup
for i, t := range ts {
idx := chunk*threads + i
if idx >= len(bs) {
break
}
b := bs[idx]
wg.Add(1)
go t.train(b.xs, b.ys, costChan, &wg)
}
wg.Wait()
close(costChan)
solver := NewVanillaSolver(WithLearnRate(0.01), WithBatchSize(batchSize))
for cost := range costChan {
// y := cost.Nodes[1].Value()
// yG, _ := cost.Nodes[1].Grad()
// c := cost.cost.Data().(float64)
// costs = append(costs, c)
solver.Step(cost.Nodes)
}
}
// var avg float64
// for _, c := range costs {
// avg += c
// }
// avg /= float64(len(costs))
}
type batch struct {
xs Value
ys Value
}
func prep() (x, y Value, bs []batch) {
xV := tensor.New(tensor.WithShape(rows, cols), tensor.WithBacking(tensor.Range(Float64, 0, cols*rows)))
yV := tensor.New(tensor.WithShape(rows), tensor.WithBacking(tensor.Range(Float64, 0, rows)))
// prep the data: y = ΣnX, where n = col ID, x ∈ X = colID / 100
xData := xV.Data().([]float64)
yData := yV.Data().([]float64)
for r := 0; r < rows; r++ {
var sum float64
for c := 0; c < cols; c++ {
idx := r*cols + c
fc := float64(c)
v := fc * fc / 100
xData[idx] = v
sum += v
}
yData[r] = sum
}
// batch the examples up into their respective batchSize
for i := 0; i < rows; i += batchSize {
xVS, _ := xV.Slice(S(i, i+batchSize))
yVS, _ := yV.Slice(S(i, i+batchSize))
b := batch{xVS, yVS}
bs = append(bs, b)
}
return xV, yV, bs
}
func concurrentTraining(xV, yV Value, bs []batch, es int) {
threads := runtime.GOMAXPROCS(-1) // reserve one thread for the CPU locked thread
ts := make([]*concurrentTrainer, threads)
for chunk := 0; chunk < threads; chunk++ {
trainer := newConcurrentTrainer()
ts[chunk] = trainer
defer trainer.vm.Close()
}
for e := 0; e < es; e++ {
trainEpoch(bs, ts, threads)
}
}
func nonConcurrentTraining(xV, yV Value, es int) {
g := NewGraph()
x := NewMatrix(g, Float64, WithValue(xV))
y := NewVector(g, Float64, WithValue(yV))
xT := Must(Transpose(x))
z := Must(Mul(xT, y))
sz := Must(Sum(z))
Grad(sz, x, y)
vm := NewTapeMachine(g, BindDualValues(x, y))
Let(x, xV)
Let(y, yV)
solver := NewVanillaSolver(WithLearnRate(0.01), WithBatchSize(batchSize))
for i := 0; i < es; i++ {
vm.RunAll()
solver.Step([]ValueGrad{x, y})
vm.Reset()
runtime.GC()
}
}
func Example_concurrentTraining() {
xV, yV, bs := prep()
concurrentTraining(xV, yV, bs, epochs)
fmt.Printf("x:\n%1.1v", xV)
fmt.Printf("y:\n%1.1v", yV)
// Output:
// x:
// ⎡-0.0003 0.01 0.04 0.09 0.2⎤
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// .
// .
// .
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎣-0.0003 0.01 0.04 0.09 0.2⎦
// y:
// [0.3 0.3 0.3 0.3 ... 0.3 0.3 0.3 0.3]
}
func Example_nonConcurrentTraining() {
xV, yV, _ := prep()
nonConcurrentTraining(xV, yV, epochs)
fmt.Printf("x:\n%1.1v", xV)
fmt.Printf("y:\n%1.1v", yV)
//Output:
// x:
// ⎡-0.0003 0.01 0.04 0.09 0.2⎤
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// .
// .
// .
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎢-0.0003 0.01 0.04 0.09 0.2⎥
// ⎣-0.0003 0.01 0.04 0.09 0.2⎦
// y:
// [0.3 0.3 0.3 0.3 ... 0.3 0.3 0.3 0.3]
}