-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
51 lines (43 loc) · 1.44 KB
/
example_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
package golgi_test
import (
"fmt"
. "gorgonia.org/golgi"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
func softmax(a *gorgonia.Node) (*gorgonia.Node, error) { return gorgonia.SoftMax(a) }
func Example() {
n := 100
of := tensor.Float64
g := gorgonia.NewGraph()
x := gorgonia.NewTensor(g, of, 4, gorgonia.WithName("X"), gorgonia.WithShape(n, 1, 28, 28), gorgonia.WithInit(gorgonia.GlorotU(1)))
y := gorgonia.NewMatrix(g, of, gorgonia.WithName("Y"), gorgonia.WithShape(n, 10), gorgonia.WithInit(gorgonia.GlorotU(1)))
nn, err := ComposeSeq(
x,
L(ConsReshape, ToShape(n, 784)),
L(ConsFC, WithSize(50), WithName("l0"), AsBatched(true), WithActivation(gorgonia.Tanh), WithBias(true)),
L(ConsDropout, WithProbability(0.5)),
L(ConsFC, WithSize(150), WithName("l1"), AsBatched(true), WithActivation(gorgonia.Rectify)), // by default WithBias is true
L(ConsLayerNorm, WithSize(20), WithName("Norm"), WithEps(0.001)),
L(ConsFC, WithSize(10), WithName("l2"), AsBatched(true), WithActivation(softmax), WithBias(false)),
)
if err != nil {
panic(err)
}
out := nn.Fwd(x)
if err = gorgonia.CheckOne(out); err != nil {
panic(err)
}
cost := gorgonia.Must(RMS(out, y))
model := nn.Model()
if _, err = gorgonia.Grad(cost, model...); err != nil {
panic(err)
}
m := gorgonia.NewTapeMachine(g)
if err := m.RunAll(); err != nil {
panic(err)
}
fmt.Printf("Model: %v\n", model)
// Output:
// Model: [l0_W, l0_B, l1_W, l1_B, Norm_W, Norm_B, l2_W]
}