forked from gorgonia/gorgonia
/
main.go
387 lines (323 loc) · 10.4 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"net/http"
_ "net/http/pprof"
G "github.com/m8u/gorgonia"
"github.com/m8u/gorgonia/examples/mnist"
"github.com/pkg/errors"
"gorgonia.org/tensor"
"time"
"gopkg.in/cheggaaa/pb.v1"
)
var (
epochs = flag.Int("epochs", 100, "Number of epochs to train for")
dataset = flag.String("dataset", "train", "Which dataset to train on? Valid options are \"train\" or \"test\"")
dtype = flag.String("dtype", "float64", "Which dtype to use")
batchsize = flag.Int("batchsize", 100, "Batch size")
cpuprofile = flag.String("cpuprofile", "", "CPU profiling")
)
const loc = "../testdata/mnist/"
var dt tensor.Dtype
func parseDtype() {
switch *dtype {
case "float64":
dt = tensor.Float64
case "float32":
dt = tensor.Float32
default:
log.Fatalf("Unknown dtype: %v", *dtype)
}
}
type convnet struct {
g *G.ExprGraph
w0, w1, w1r, w2, w2r, w3, w4 *G.Node // weights. the number at the back indicates which layer it's used for
d0, d1, d2, d3 float64 // dropout probabilities
out *G.Node
}
func newResNet(g *G.ExprGraph) *convnet {
w0 := G.NewTensor(g, dt, 4, G.WithShape(32, 1, 3, 3), G.WithName("w0"), G.WithInit(G.GlorotN(1.0)))
w1 := G.NewTensor(g, dt, 4, G.WithShape(64, 32, 3, 3), G.WithName("w1"), G.WithInit(G.GlorotN(1.0)))
w1r := G.NewMatrix(g, dt, G.WithShape(3136, 12544), G.WithName("w1r"), G.WithInit(G.GlorotN(1.0)))
w2 := G.NewTensor(g, dt, 4, G.WithShape(128, 64, 3, 3), G.WithName("w2"), G.WithInit(G.GlorotN(1.0)))
w2r := G.NewMatrix(g, dt, G.WithShape(6272, 25088), G.WithName("w2r"), G.WithInit(G.GlorotN(1.0)))
w3 := G.NewMatrix(g, dt, G.WithShape(25088, 625), G.WithName("w3"), G.WithInit(G.GlorotN(1.0)))
w4 := G.NewMatrix(g, dt, G.WithShape(625, 10), G.WithName("w4"), G.WithInit(G.GlorotN(1.0)))
return &convnet{
g: g,
w0: w0,
w1: w1,
w1r: w1r,
w2: w2,
w2r: w2r,
w3: w3,
w4: w4,
d0: 0.3,
d1: 0.3,
d2: 0.3,
d3: 0.2,
}
}
func (m *convnet) learnables() G.Nodes {
return G.Nodes{m.w0, m.w1, m.w1r, m.w2, m.w2r, m.w3, m.w4}
}
// This function is particularly verbose for educational reasons. In reality, you'd wrap up the layers within a layer struct type and perform per-layer activations
func (m *convnet) fwd(x *G.Node) (err error) {
var c0, c1, c2, fc *G.Node
var a0, a1, a2, a3 *G.Node
var p0, p1, p2 *G.Node
var l0, l1, l2, l3 *G.Node
// LAYER 0
// here we convolve with stride = (1, 1) and padding = (1, 1),
// which is your bog standard convolution for convnet
if c0, err = G.Conv2d(x, m.w0, tensor.Shape{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}); err != nil {
return errors.Wrap(err, "Layer 0 Convolution failed")
}
if a0, err = G.Rectify(c0); err != nil {
return errors.Wrap(err, "Layer 0 activation failed")
}
if p0, err = G.MaxPool2D(a0, tensor.Shape{2, 2}, []int{0, 0}, []int{2, 2}); err != nil {
return errors.Wrap(err, "Layer 0 Maxpooling failed")
}
log.Printf("p0 shape %v", p0.Shape())
if l0, err = G.Dropout(p0, m.d0); err != nil {
return errors.Wrap(err, "Unable to apply a dropout")
}
// Layer 1
if c1, err = G.Conv2d(l0, m.w1, tensor.Shape{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}); err != nil {
return errors.Wrap(err, "Layer 1 Convolution failed")
}
if a1, err = G.Rectify(c1); err != nil {
return errors.Wrap(err, "Layer 1 activation failed")
}
if p1, err = G.MaxPool2D(a1, tensor.Shape{2, 2}, []int{0, 0}, []int{2, 2}); err != nil {
return errors.Wrap(err, "Layer 1 Maxpooling failed")
}
b, c, h, w := p1.Shape()[0], p1.Shape()[1], p1.Shape()[2], p1.Shape()[3]
log.Printf("Reshaping p1 %v to %v", p1.Shape(), tensor.Shape{b, c * h * w})
r1, err := G.Reshape(p1, tensor.Shape{b, c * h * w})
if err != nil {
return fmt.Errorf("layer 1 reshaping failed: %w", err)
}
m1, err := G.Mul(r1, m.w1r)
if err != nil {
return fmt.Errorf("layer 1 FC failed: %w", err)
}
log.Printf("Layer 1: reshape(%v, %v)", m1.Shape(), c1.Shape())
r1, err = G.Reshape(m1, c1.Shape())
if err != nil {
return fmt.Errorf("layer 1 reshaping failed: %w", err)
}
s1, err := G.Add(c1, r1)
if err != nil {
return fmt.Errorf("layer 1 Add failed: %w", err)
}
l1, err = G.Dropout(s1, m.d1)
if err != nil {
return fmt.Errorf("layer 1 dropout failed: %w", err)
}
// Layer 2
if c2, err = G.Conv2d(l1, m.w2, tensor.Shape{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}); err != nil {
return errors.Wrap(err, "Layer 2 Convolution failed")
}
if a2, err = G.Rectify(c2); err != nil {
return errors.Wrap(err, "Layer 2 activation failed")
}
if p2, err = G.MaxPool2D(a2, tensor.Shape{2, 2}, []int{0, 0}, []int{2, 2}); err != nil {
return errors.Wrap(err, "Layer 2 Maxpooling failed")
}
var r2 *G.Node
b, c, h, w = p2.Shape()[0], p2.Shape()[1], p2.Shape()[2], p2.Shape()[3]
if r2, err = G.Reshape(p2, tensor.Shape{b, c * h * w}); err != nil {
return errors.Wrap(err, "Unable to reshape layer 2")
}
m2, err := G.Mul(r2, m.w2r)
if err != nil {
return fmt.Errorf("layer 2 FC failed: %w", err)
}
log.Printf("Layer 2: reshape(%v, %v)", m2.Shape(), c2.Shape())
r2, err = G.Reshape(m2, c2.Shape())
if err != nil {
return fmt.Errorf("layer 2 reshaping failed: %w", err)
}
s2, err := G.Add(c2, r2)
if err != nil {
return fmt.Errorf("layer 2 Add failed: %w", err)
}
log.Printf("Layer2: Add(%v, %v) -> %v", c2.Shape(), r2.Shape(), s2.Shape())
if l2, err = G.Dropout(s2, m.d2); err != nil {
return errors.Wrap(err, "Unable to apply a dropout on layer 2")
}
log.Printf("Layer2: Reshape(%v, %v)", l2.Shape(), tensor.Shape{b, 128 * 14 * 14})
if l2, err = G.Reshape(l2, tensor.Shape{b, 128 * 14 * 14}); err != nil {
return errors.Wrap(err, "Unable to apply a reshape on layer 2")
}
// Layer 3
log.Printf("Layer 3 %v x %v", l2.Shape(), m.w3.Shape())
if fc, err = G.Mul(l2, m.w3); err != nil {
return errors.Wrapf(err, "Unable to multiply l2 and w3")
}
if a3, err = G.Rectify(fc); err != nil {
return errors.Wrapf(err, "Unable to activate fc")
}
if l3, err = G.Dropout(a3, m.d3); err != nil {
return errors.Wrapf(err, "Unable to apply a dropout on layer 3")
}
// output decode
var out *G.Node
if out, err = G.Mul(l3, m.w4); err != nil {
return errors.Wrapf(err, "Unable to multiply l3 and w4")
}
m.out, err = G.SoftMax(out)
return
}
func main() {
flag.Parse()
parseDtype()
rand.Seed(1337)
// intercept Ctrl+C
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
doneChan := make(chan bool, 1)
var inputs, targets tensor.Tensor
var err error
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
trainOn := *dataset
if inputs, targets, err = mnist.Load(trainOn, loc, dt); err != nil {
log.Fatal(err)
}
// the data is in (numExamples, 784).
// In order to use a convnet, we need to massage the data
// into this format (batchsize, numberOfChannels, height, width).
//
// This translates into (numExamples, 1, 28, 28).
//
// This is because the convolution operators actually understand height and width.
//
// The 1 indicates that there is only one channel (MNIST data is black and white).
numExamples := inputs.Shape()[0]
bs := *batchsize
// todo - check bs not 0
if err := inputs.Reshape(numExamples, 1, 28, 28); err != nil {
log.Fatal(err)
}
g := G.NewGraph()
x := G.NewTensor(g, dt, 4, G.WithShape(bs, 1, 28, 28), G.WithName("x"))
y := G.NewMatrix(g, dt, G.WithShape(bs, 10), G.WithName("y"))
m := newResNet(g)
if err = m.fwd(x); err != nil {
log.Fatalf("%+v", err)
}
// Note: the correct losses should look like that
//
// The losses that are not commented out is used to test the stabilization function of Gorgonia.
//losses := G.Must(G.HadamardProd(G.Must(G.Neg(G.Must(G.Log(m.out)))), y))
losses := G.Must(G.Log(G.Must(G.HadamardProd(m.out, y))))
cost := G.Must(G.Mean(losses))
cost = G.Must(G.Neg(cost))
// we wanna track costs
var costVal G.Value
G.Read(cost, &costVal)
if _, err = G.Grad(cost, m.learnables()...); err != nil {
log.Fatal(err)
}
// debug
// ioutil.WriteFile("fullGraph.dot", []byte(g.ToDot()), 0644)
// log.Printf("%v", prog)
// logger := log.New(os.Stderr, "", 0)
// vm := gorgonia.NewTapeMachine(g, gorgonia.BindDualValues(m.learnables()...), gorgonia.WithLogger(logger), gorgonia.WithWatchlist())
prog, locMap, _ := G.Compile(g)
//log.Printf("%v", prog)
vm := G.NewTapeMachine(g, G.WithPrecompiled(prog, locMap), G.BindDualValues(m.learnables()...))
solver := G.NewRMSPropSolver(G.WithBatchSize(float64(bs)))
defer vm.Close()
// pprof
// handlePprof(sigChan, doneChan)
var profiling bool
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
profiling = true
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
go cleanup(sigChan, doneChan, profiling)
batches := numExamples / bs
log.Printf("Batches %d", batches)
bar := pb.New(batches)
bar.SetRefreshRate(time.Second)
bar.SetMaxWidth(80)
for i := 0; i < *epochs; i++ {
bar.Prefix(fmt.Sprintf("Epoch %d", i))
bar.Set(0)
bar.Start()
for b := 0; b < batches; b++ {
start := b * bs
end := start + bs
if start >= numExamples {
break
}
if end > numExamples {
end = numExamples
}
var xVal, yVal tensor.Tensor
if xVal, err = inputs.Slice(G.S(start, end)); err != nil {
log.Fatal("Unable to slice x")
}
if yVal, err = targets.Slice(G.S(start, end)); err != nil {
log.Fatal("Unable to slice y")
}
if err = xVal.(*tensor.Dense).Reshape(bs, 1, 28, 28); err != nil {
log.Fatalf("Unable to reshape %v", err)
}
G.Let(x, xVal)
G.Let(y, yVal)
if err = vm.RunAll(); err != nil {
log.Fatalf("Failed at epoch %d, batch %d. Error: %v", i, b, err)
}
if err = solver.Step(G.NodesToValueGrads(m.learnables())); err != nil {
log.Fatalf("Failed to update nodes with gradients at epoch %d, batch %d. Error %v", i, b, err)
}
vm.Reset()
bar.Increment()
}
log.Printf("Epoch %d | cost %v", i, costVal)
}
}
func cleanup(sigChan chan os.Signal, doneChan chan bool, profiling bool) {
select {
case <-sigChan:
log.Println("EMERGENCY EXIT!")
if profiling {
log.Println("Stop profiling")
pprof.StopCPUProfile()
}
os.Exit(1)
case <-doneChan:
return
}
}
func handlePprof(sigChan chan os.Signal, doneChan chan bool) {
var profiling bool
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
profiling = true
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
go cleanup(sigChan, doneChan, profiling)
}