/
cntl1.go
245 lines (201 loc) · 5.51 KB
/
cntl1.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
package ntm
import (
"fmt"
"github.com/gonum/blas"
"github.com/gonum/blas/blas64"
)
type controller1 struct {
weightsVal []float64
weightsGrad []float64
Reads []*memRead
X []float64
ReadsXVal blas64.Vector
H1Val []float64
H1Grad []float64
heads []*Head
outVal []float64
outGrad []float64
numHeads int
memoryM int
memoryN int
xSize int
h1Size int
ySize int
}
func (c *controller1) wh1Cols() int {
return c.numHeads*c.memoryM + c.xSize + 1
}
func (c *controller1) wyRows() int {
return c.ySize + c.numHeads*headUnitsLen(c.memoryM)
}
func (c *controller1) wyOffset() int {
return c.h1Size * c.wh1Cols()
}
func (c *controller1) wtm1Offset() int {
return c.wyOffset() + c.wyRows()*(c.h1Size+1)
}
func (c *controller1) mtm1Offset() int {
return c.wtm1Offset() + c.numHeads*c.memoryN
}
func (c *controller1) numWeights() int {
return c.mtm1Offset() + c.memoryN*c.memoryM
}
func (c *controller1) wh1(w []float64) blas64.General {
m := blas64.General{
Rows: c.h1Size,
Cols: c.wh1Cols(),
}
m.Stride = m.Cols
m.Data = w[0:c.wyOffset()]
return m
}
func (c *controller1) wh1Val() blas64.General {
return c.wh1(c.weightsVal)
}
func (c *controller1) wh1Grad() blas64.General {
return c.wh1(c.weightsGrad)
}
func (c *controller1) wy(w []float64) blas64.General {
m := blas64.General{
Rows: c.wyRows(),
Cols: c.h1Size + 1,
}
m.Stride = m.Cols
m.Data = w[c.wyOffset():c.wtm1Offset()]
return m
}
func (c *controller1) wyVal() blas64.General {
return c.wy(c.weightsVal)
}
func (c *controller1) wyGrad() blas64.General {
return c.wy(c.weightsGrad)
}
func (c *controller1) Wtm1BiasVal() []float64 {
return c.weightsVal[c.wtm1Offset():c.mtm1Offset()]
}
func (c *controller1) Wtm1BiasGrad() []float64 {
return c.weightsGrad[c.wtm1Offset():c.mtm1Offset()]
}
func (c *controller1) Mtm1BiasVal() []float64 {
return c.weightsVal[c.mtm1Offset():]
}
func (c *controller1) Mtm1BiasGrad() []float64 {
return c.weightsGrad[c.mtm1Offset():]
}
// NewEmptyController1 returns a new controller1 which is a single layer feedforward network.
// The returned controller1 is empty in that all its network weights are initialized as 0.
func NewEmptyController1(xSize, ySize, h1Size, numHeads, n, m int) *controller1 {
c := controller1{
numHeads: numHeads,
memoryM: m,
memoryN: n,
xSize: xSize,
h1Size: h1Size,
ySize: ySize,
}
c.weightsVal = make([]float64, c.numWeights())
c.weightsGrad = make([]float64, c.numWeights())
return &c
}
func (c *controller1) Heads() []*Head {
return c.heads
}
func (c *controller1) YVal() []float64 {
return c.outVal[0:c.ySize]
}
func (c *controller1) YGrad() []float64 {
return c.outGrad[0:c.ySize]
}
func (old *controller1) Forward(reads []*memRead, x []float64) Controller {
c := controller1{
weightsVal: old.weightsVal,
weightsGrad: old.weightsGrad,
Reads: reads,
X: x,
H1Val: make([]float64, old.h1Size+1),
H1Grad: make([]float64, old.h1Size+1),
heads: make([]*Head, len(reads)),
outVal: make([]float64, old.wyRows()),
outGrad: make([]float64, old.wyRows()),
numHeads: old.numHeads,
memoryM: old.memoryM,
memoryN: old.memoryN,
xSize: old.xSize,
h1Size: old.h1Size,
ySize: old.ySize,
}
ud := make([]float64, c.wh1Cols())
for i, read := range reads {
copy(ud[i*c.memoryM:], read.TopVal)
}
copy(ud[c.numHeads*c.memoryM:], c.X)
ud[c.numHeads*c.memoryM+c.xSize] = 1
c.ReadsXVal = blas64.Vector{Inc: 1, Data: ud}
h1 := blas64.Vector{Inc: 1, Data: c.H1Val[0:c.h1Size]}
blas64.Gemv(blas.NoTrans, 1, c.wh1Val(), c.ReadsXVal, 1, h1)
for i, h := range c.H1Val[0:c.h1Size] {
c.H1Val[i] = Sigmoid(h)
}
c.H1Val[c.h1Size] = 1
h1 = blas64.Vector{Inc: 1, Data: c.H1Val}
outV := blas64.Vector{Inc: 1, Data: c.outVal}
blas64.Gemv(blas.NoTrans, 1, c.wyVal(), h1, 1, outV)
hul := headUnitsLen(c.memoryM)
for i := range c.heads {
head := NewHead(c.memoryM)
c.heads[i] = head
start := c.ySize + i*hul
head.vals = c.outVal[start : start+hul]
head.grads = c.outGrad[start : start+hul]
}
return &c
}
func (c *controller1) Backward() {
out := blas64.Vector{Inc: 1, Data: c.outGrad}
h1Val := blas64.Vector{Inc: 1, Data: c.H1Val}
h1Grad := blas64.Vector{Inc: 1, Data: c.H1Grad}
blas64.Gemv(blas.Trans, 1, c.wyVal(), out, 1, h1Grad)
blas64.Ger(1, out, h1Val, c.wyGrad())
h1Val = blas64.Vector{Inc: 1, Data: c.H1Val[0:c.h1Size]}
h1Grad = blas64.Vector{Inc: 1, Data: c.H1Grad[0:c.h1Size]}
for i, v := range h1Val.Data {
h1Grad.Data[i] *= v * (1 - v)
}
u := blas64.Vector{Inc: 1, Data: make([]float64, c.wh1Cols())}
blas64.Gemv(blas.Trans, 1, c.wh1Val(), h1Grad, 1, u)
blas64.Ger(1, h1Grad, c.ReadsXVal, c.wh1Grad())
for i, read := range c.Reads {
copy(read.TopGrad, u.Data[i*c.memoryM:(i+1)*c.memoryM])
}
}
func (c *controller1) WeightsVal() []float64 {
return c.weightsVal
}
func (c *controller1) WeightsGrad() []float64 {
return c.weightsGrad
}
func (c *controller1) WeightsDesc(i int) string {
if i < c.wyOffset() {
return fmt.Sprintf("wh1[%d][%d]", i/c.wh1Cols(), i%c.wh1Cols())
}
if i < c.wtm1Offset() {
j := i - c.wyOffset()
cols := c.h1Size + 1
return fmt.Sprintf("wy[%d][%d]", j/cols, i%cols)
}
if i < c.mtm1Offset() {
j := i - c.wtm1Offset()
return fmt.Sprintf("wtm1[%d][%d]", j/c.memoryN, j%c.memoryN)
}
j := i - c.mtm1Offset()
return fmt.Sprintf("mtm1[%d][%d]", j/c.memoryM, j%c.memoryM)
}
func (c *controller1) NumHeads() int {
return c.numHeads
}
func (c *controller1) MemoryN() int {
return c.memoryN
}
func (c *controller1) MemoryM() int {
return c.memoryM
}