-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequential.go
81 lines (64 loc) · 1.51 KB
/
sequential.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
package model
import (
"fmt"
randv2 "math/rand/v2"
"github.com/itsubaki/neu/layer"
"github.com/itsubaki/neu/math/matrix"
)
type Sequential struct {
Layer []Layer
Source randv2.Source
}
func NewSequential(layer []Layer, s randv2.Source) *Sequential {
return &Sequential{
Layer: layer,
Source: s,
}
}
func (m *Sequential) Predict(x matrix.Matrix, opts ...layer.Opts) matrix.Matrix {
for _, l := range m.Layer[:len(m.Layer)-1] {
x = l.Forward(x, nil, opts...)
}
return x
}
func (m *Sequential) Forward(x, t matrix.Matrix) matrix.Matrix {
opts := layer.Opts{Train: true, Source: m.Source}
y := m.Predict(x, opts)
return m.Layer[len(m.Layer)-1].Forward(y, t, opts)
}
func (m *Sequential) Backward() matrix.Matrix {
dout := matrix.New([]float64{1})
for i := len(m.Layer) - 1; i > -1; i-- {
dout, _ = m.Layer[i].Backward(dout)
}
return dout
}
func (m *Sequential) Summary() []string {
s := []string{fmt.Sprintf("%T", m)}
for _, l := range m.Layers() {
s = append(s, l.String())
}
return s
}
func (m *Sequential) Layers() []Layer {
return m.Layer
}
func (m *Sequential) Params() [][]matrix.Matrix {
params := make([][]matrix.Matrix, 0)
for _, l := range m.Layer {
params = append(params, l.Params())
}
return params
}
func (m *Sequential) Grads() [][]matrix.Matrix {
grads := make([][]matrix.Matrix, 0)
for _, l := range m.Layer {
grads = append(grads, l.Grads())
}
return grads
}
func (m *Sequential) SetParams(p [][]matrix.Matrix) {
for i, l := range m.Layers() {
l.SetParams(p[i]...)
}
}