/
nn.go
374 lines (341 loc) · 10.5 KB
/
nn.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
package nn
import (
"os"
"fmt"
"math"
"io/ioutil"
"math/rand"
"encoding/json"
"github.com/garretraziel/matrices"
)
// NN represents neural network to be used with backpropagation
type NN struct {
layers []int
weights []matrices.Matrix
biases []matrices.Matrix
}
// InitNN creates new neural network with given number of layers, neurons in each layer and initalizes them randomly
func InitNN(layers []int) NN {
biases := make([]matrices.Matrix, len(layers) - 1)
weights := make([]matrices.Matrix, len(layers) - 1)
for i := range layers[1:] {
biases[i] = matrices.RandInitMatrix(1, layers[i + 1])
}
for i := range layers[1:] {
weights[i] = matrices.RandInitMatrixNormalized(layers[i], layers[i + 1])
}
return NN{layers, weights, biases}
}
// Copy creates copy if given network
func (network NN) Copy() NN {
layers := make([]int, len(network.layers))
copy(layers, network.layers)
biases := make([]matrices.Matrix, len(network.biases))
for i, bias := range network.biases {
biases[i] = bias.Copy()
}
weights := make([]matrices.Matrix, len(network.weights))
for i, weight := range network.weights {
weights[i] = weight.Copy()
}
return NN{layers, biases, weights}
}
func (network NN) String() (result string) {
result = "Neural network:\n"
result += "layers:"
for _, layer := range network.layers {
result += fmt.Sprintf(" %d", layer)
}
for i, weights := range network.weights {
result += fmt.Sprintf("\nweights layer %d to %d:\n%s", i + 1, i, weights.String())
}
for i, biases := range network.biases {
result += fmt.Sprintf("\nbiases layer %d:\n%s", i + 1, biases.String())
}
return
}
// FeedForward returns output of given Network on given input
func (network NN) FeedForward(input matrices.Matrix) matrices.Matrix {
lastOutput := input
for i := range network.weights {
weights := network.weights[i]
biases := network.biases[i]
multiplied, err := lastOutput.Dot(weights)
if err != nil {
panic(err)
}
added, err := multiplied.Add(biases)
if err != nil {
panic(err)
}
lastOutput = added.Sigmoid()
}
return lastOutput
}
// Evaluate returns ratio of correctly clasified inputs
func (network NN) Evaluate(inputs []TrainItem) float64 {
correct := 0
for _, input := range inputs {
output := network.FeedForward(input.Values)
max, err := output.MaxAt()
if err != nil {
panic(err)
}
if float64(max) == input.Label {
correct++
}
}
return float64(correct) / float64(len(inputs))
}
// Cost returns total cost of input training items for cross-entropy
func (network NN) Cost(inputs []TrainItem) float64 {
cost := 0.0
for _, input := range inputs {
output := network.FeedForward(input.Values)
y, err := matrices.OneHotMatrix(1, input.Distinct, 0, int(input.Label))
if err != nil {
panic(err)
}
first, err := y.Apply(matrices.Negate).Mult(output.Apply(math.Log2))
if err != nil {
panic(err)
}
second, err := y.Apply(matrices.OneMinus).Mult(output.Apply(matrices.OneMinus).Apply(math.Log2))
if err != nil {
panic(err)
}
together, err := first.Sub(second)
if err != nil {
panic(err)
}
cost += together.Sum()
}
return cost / float64(len(inputs))
}
// Train trains Network on given input with given settings
func (network NN) Train(inputs []TrainItem, epochs, miniBatchSize int, eta, etaFraction, lmbda float64, testData []TrainItem, printCost bool) {
oldEta := eta
inputCount := len(inputs)
i := 0
doingBestOfN := false
if epochs < 0 {
doingBestOfN = true
epochs = -epochs
}
bestCost := network.Cost(testData)
bestNetwork := network.Copy()
bestBefore := 0
for {
if !doingBestOfN && i >= epochs {
break
} else if doingBestOfN && bestBefore >= epochs {
if etaFraction > 0 && eta * etaFraction > oldEta {
bestBefore = 0
eta /= 2.0
} else {
network = bestNetwork
break
}
}
shuffled := make([]TrainItem, inputCount)
perm := rand.Perm(inputCount)
for i, v := range perm {
shuffled[v] = inputs[i]
}
batchesCount := int(float64(inputCount) / float64(miniBatchSize) + 0.5)
batches := make([][]TrainItem, batchesCount)
for i := 0; i < batchesCount; i++ {
if i + miniBatchSize >= inputCount {
batches[i] = shuffled[i*miniBatchSize:]
} else {
batches[i] = shuffled[i*miniBatchSize:i*miniBatchSize + miniBatchSize]
}
}
for _, batch := range batches {
network.updateMiniBatch(batch, eta, lmbda, len(inputs))
}
cost := network.Cost(testData)
if doingBestOfN {
if cost < bestCost {
bestCost = cost
bestNetwork = network.Copy()
bestBefore = 0
} else {
bestBefore++
}
}
if len(testData) > 0 {
fmt.Printf("Epoch %d: %f\n", i, network.Evaluate(testData))
if printCost {
fmt.Printf("Cost: %f\n", cost)
}
} else {
fmt.Printf("Epoch %d finished.\n", i)
}
i++
}
}
func (network NN) updateMiniBatch(batch []TrainItem, eta, lmbda float64, n int) {
var err error
cxw := make([]matrices.Matrix, len(network.weights))
cxb := make([]matrices.Matrix, len(network.biases))
for i, m := range network.weights {
cxw[i] = matrices.InitMatrix(m.Rows(), m.Cols())
}
for i, m := range network.biases {
cxb[i] = matrices.InitMatrix(m.Rows(), m.Cols())
}
for _, item := range batch {
nablaW, nablaB := network.backprop(item)
for i, nabla := range nablaW {
cxw[i], err = cxw[i].Add(nabla)
if err != nil {
panic(err)
}
}
for i, nabla := range nablaB {
cxb[i], err = cxb[i].Add(nabla)
if err != nil {
panic(err)
}
}
}
multByConst := matrices.Mult(eta / float64(len(batch)))
for i, w := range cxw {
regularization := matrices.Mult(1-eta*lmbda/float64(n))
reduced := w.Apply(multByConst)
network.weights[i], err = network.weights[i].Apply(regularization).Sub(reduced)
if err != nil {
panic(err)
}
}
for i, b := range cxb {
reduced := b.Apply(multByConst)
network.biases[i], err = network.biases[i].Sub(reduced)
if err != nil {
panic(err)
}
}
}
func (network NN) backprop(item TrainItem) ([]matrices.Matrix, []matrices.Matrix) {
nablaW := make([]matrices.Matrix, len(network.weights))
nablaB := make([]matrices.Matrix, len(network.biases))
for i, m := range network.weights {
nablaW[i] = matrices.InitMatrix(m.Rows(), m.Cols())
}
for i, m := range network.biases {
nablaB[i] = matrices.InitMatrix(m.Rows(), m.Cols())
}
activation := item.Values
activations := make([]matrices.Matrix, len(network.weights) + 1)
activations[0] = activation
zs := make([]matrices.Matrix, len(network.weights))
for i := range network.weights {
weights := network.weights[i]
biases := network.biases[i]
multiplied, err := activation.Dot(weights)
if err != nil {
panic(err)
}
z, err := multiplied.Add(biases)
if err != nil {
panic(err)
}
zs[i] = z
activation = z.Sigmoid()
activations[i + 1] = activation
}
y, err := matrices.OneHotMatrix(1, item.Distinct, 0, int(item.Label))
if err != nil {
panic(err)
}
// old code with MSE
// costDerivative, err := activations[len(activations) - 1].Sub(y)
// if err != nil {
// panic(err)
// }
// delta, err := costDerivative.Mult(zs[len(zs) - 1].SigmoidPrime())
// if err != nil {
// panic(err)
// }
// new code with cross-entropy
delta, err := activations[len(activations) - 1].Sub(y)
if err != nil {
panic(err)
}
nablaB[len(nablaB) - 1] = delta
nablaW[len(nablaW) - 1], err = activations[len(activations) - 2].Transpose().Dot(delta)
if err != nil {
panic(err)
}
for l := 2; l < len(network.layers); l++ {
z := zs[len(zs) - l]
sp := z.SigmoidPrime()
dotted, err := delta.Dot(network.weights[len(network.weights) - l + 1].Transpose())
if err != nil {
panic(err)
}
delta, err = dotted.Mult(sp)
if err != nil {
panic(err)
}
nablaB[len(nablaB) - l] = delta
nablaW[len(nablaW) - l], err = activations[len(activations) - l - 1].Transpose().Dot(delta)
if err != nil {
panic(err)
}
}
return nablaW, nablaB
}
// MarshalJSON implements Marshaler interface
func (network NN) MarshalJSON() ([]byte, error) {
exportedNetwork := struct {
Layers []int
Weights []matrices.Matrix
Biases []matrices.Matrix
}{
network.layers,
network.weights,
network.biases,
}
return json.Marshal(exportedNetwork)
}
// UnmarshalJSON implements Unmarshaler interface
func (network *NN) UnmarshalJSON(serialized []byte) error {
var exportedNetwork struct {
Layers []int
Weights []matrices.Matrix
Biases []matrices.Matrix
}
if err := json.Unmarshal(serialized, &exportedNetwork); err != nil {
return err
}
network.layers = exportedNetwork.Layers
network.weights = exportedNetwork.Weights
network.biases = exportedNetwork.Biases
return nil
}
// Save exports network to file as JSON
func (network NN) Save(path string) error {
res, err := json.Marshal(network)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(res)
return err
}
// LoadNetwork loads network from JSON file
func LoadNetwork(path string) (NN, error) {
var network NN
dat, err := ioutil.ReadFile(path)
if err != nil {
return network, err
}
err = json.Unmarshal(dat, &network)
return network, err
}