-
Notifications
You must be signed in to change notification settings - Fork 0
/
neurons_test.go
567 lines (549 loc) · 13.7 KB
/
neurons_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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package goDeep
import (
"reflect"
"testing"
)
func Test_inputDense_forward(t *testing.T) {
type fields struct {
synapses [][]float64
nextLayerSize int
currLayerSize int
nextBias bool
}
type args struct {
input []float64
}
tests := []struct {
name string
fields fields
args args
wantOutput [][]float64
wantErr bool
}{
{
name: "testForwardProp",
fields: fields{
synapses: [][]float64{
{1.0, 10.0, 100.0, 1000.0},
{2.0, 20.0, 200.0, 2000.0},
{3.0, 30.0, 300.0, 3000.0},
},
nextLayerSize: 5,
currLayerSize: 3,
nextBias: true,
},
args: args{[]float64{1, 2, 3}},
wantOutput: [][]float64{
{1.0, 4.0, 9.0},
{10.0, 40.0, 90.0},
{100.0, 400.0, 900.0},
{1000.0, 4000.0, 9000.0},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &inputDense{
synapses: tt.fields.synapses,
nextLayerSize: tt.fields.nextLayerSize,
currLayerSize: tt.fields.currLayerSize,
nextBias: tt.fields.nextBias,
}
gotOutput, err := l.forward(tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("inputDense.forward() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOutput, tt.wantOutput) {
t.Errorf("inputDense.forward() = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}
type mockActivation struct{}
func (ma *mockActivation) activate(n float64) (float64, error) {
return n, nil
}
func (ma *mockActivation) actDerivative(n float64) (float64, error) {
return n, nil
}
func Test_hiddenDense_forward(t *testing.T) {
type fields struct {
activation activation
synapseInitializer synapseInitializer
prevLayerSize int
currLayerSize int
nextLayerSize int
learningRate float64
synapses [][]float64
nextBias, bias bool
}
type args struct {
input [][]float64
}
tests := []struct {
name string
fields fields
args args
wantOutput [][]float64
wantErr bool
}{
{
name: "forwardLastHidden",
fields: fields{
activation: new(mockActivation),
prevLayerSize: 4,
currLayerSize: 5,
nextLayerSize: 3,
synapses: [][]float64{
{1, 10, 100},
{2, 20, 200},
{3, 30, 300},
{4, 40, 400},
{5, 5, 5},
},
nextBias: false,
bias: true,
},
args: args{
[][]float64{
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
},
},
wantOutput: [][]float64{{10, 20, 30, 40, 5}, {100, 200, 300, 400, 5}, {1000, 2000, 3000, 4000, 5}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &hiddenDense{
activation: tt.fields.activation,
prevLayerSize: tt.fields.prevLayerSize,
currLayerSize: tt.fields.currLayerSize,
nextLayerSize: tt.fields.nextLayerSize,
synapses: tt.fields.synapses,
nextBias: tt.fields.nextBias,
bias: tt.fields.bias,
}
gotOutput, err := l.forward(tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("hiddenDense.forward() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOutput, tt.wantOutput) {
t.Errorf("hiddenDense.forward() = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}
type mockCost struct{ coeff float64 }
func (c *mockCost) costDerivative(pred, label float64) float64 {
return pred - label
}
func (c *mockCost) countCost([]float64, []float64) float64 {
return 1
}
func Test_outputDense_forward(t *testing.T) {
type fields struct {
activation activation
cost cost
currLayerSize, prevLayerSize int
}
type args struct {
rowInput [][]float64
}
tests := []struct {
name string
fields fields
args args
wantOutput []float64
wantErr bool
}{
{
name: "outputForward",
fields: fields{
activation: new(mockActivation),
cost: new(mockCost),
prevLayerSize: 5,
currLayerSize: 3,
},
args: args{[][]float64{{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}},
wantOutput: []float64{15, 15, 15},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &outputDense{
activation: tt.fields.activation,
cost: tt.fields.cost,
prevLayerSize: tt.fields.prevLayerSize,
currLayerSize: tt.fields.currLayerSize,
}
gotOutput, err := l.forward(tt.args.rowInput)
if (err != nil) != tt.wantErr {
t.Errorf("outputDense.forward() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOutput, tt.wantOutput) {
t.Errorf("outputDense.forward() = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}
func Test_outputDense_forwardMeasure(t *testing.T) {
type fields struct {
activation activation
input []float64
cost cost
prevLayerSize int
}
type args struct {
rowInput [][]float64
labels []float64
}
tests := []struct {
name string
fields fields
args args
wantPrediction []float64
wantCost float64
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &outputDense{
activation: tt.fields.activation,
input: tt.fields.input,
cost: tt.fields.cost,
prevLayerSize: tt.fields.prevLayerSize,
}
gotPrediction, gotCost, err := l.forwardMeasure(tt.args.rowInput, tt.args.labels)
if (err != nil) != tt.wantErr {
t.Errorf("outputDense.forwardMeasure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotPrediction, tt.wantPrediction) {
t.Errorf("outputDense.forwardMeasure() gotPrediction = %v, want %v", gotPrediction, tt.wantPrediction)
}
if gotCost != tt.wantCost {
t.Errorf("outputDense.forwardMeasure() gotCost = %v, want %v", gotCost, tt.wantCost)
}
})
}
}
func Test_inputDense_backward(t *testing.T) {
type fields struct {
nextLayerSize int
currLayerSize int
input []float64
bias, nextBias bool
}
type args struct {
eRRors []float64
}
tests := []struct {
name string
fields fields
args args
wantErr bool
want [][]float64
}{
{
name: "inputBackward",
fields: fields{
nextLayerSize: 5,
currLayerSize: 4,
input: []float64{1, 2, 3},
nextBias: true,
bias: true,
},
args: args{[]float64{1, 2, 3, 4}},
want: [][]float64{
{1, 2, 3, 4}, {2, 4, 6, 8}, {3, 6, 9, 12}, {1, 2, 3, 4},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &inputDense{
nextLayerSize: tt.fields.nextLayerSize,
currLayerSize: tt.fields.currLayerSize,
input: tt.fields.input,
bias: tt.fields.bias,
nextBias: tt.fields.nextBias,
}
if err := l.backward(tt.args.eRRors); (err != nil) != tt.wantErr {
t.Errorf("hiddenDense.applyCorrections() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(l.corrections, tt.want) {
t.Errorf("hiddenDense.corrections = %v, want %v", l.corrections, tt.want)
}
})
}
}
func Test_hiddenDense_backward(t *testing.T) {
type fields struct {
activation activation
prevLayerSize int
currLayerSize int
nextLayerSize int
synapses [][]float64
activated []float64
input []float64
nextBias, bias bool
}
type args struct {
eRRors []float64
}
tests := []struct {
name string
fields fields
args args
wantPrevLayerErrors []float64
wantErr bool
}{
{
name: "hiddenBackward",
fields: fields{
activation: new(mockActivation),
prevLayerSize: 4,
currLayerSize: 5,
nextLayerSize: 3,
input: []float64{1, 2, 3, 4},
activated: []float64{1, 2, 3, 4},
synapses: [][]float64{
{1, 2, 3},
{10, 20, 30},
{100, 200, 300},
{1000, 2000, 3000},
{5, 5, 5},
},
bias: true,
},
args: args{[]float64{1, 2, 3}},
wantPrevLayerErrors: []float64{14, 280, 4200, 56000},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &hiddenDense{
activation: tt.fields.activation,
currLayerSize: tt.fields.currLayerSize,
nextLayerSize: tt.fields.nextLayerSize,
synapses: tt.fields.synapses,
activated: tt.fields.activated,
input: tt.fields.input,
nextBias: tt.fields.nextBias,
bias: tt.fields.bias,
}
gotPrevLayerErrors, err := l.backward(tt.args.eRRors)
if (err != nil) != tt.wantErr {
t.Errorf("hiddenDense.backward() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotPrevLayerErrors, tt.wantPrevLayerErrors) {
t.Errorf("hiddenDense.backward() = %v, want %v", gotPrevLayerErrors, tt.wantPrevLayerErrors)
}
})
}
}
func Test_outputDense_backward(t *testing.T) {
type fields struct {
activation activation
cost cost
prevLayerSize int
input []float64
}
type args struct {
prediction []float64
labels []float64
}
tests := []struct {
name string
fields fields
args args
wantERRors []float64
wantErr bool
}{
{
name: "backwardOutput",
fields: fields{
activation: new(mockActivation),
cost: new(mockCost),
prevLayerSize: 5,
input: []float64{1, 2, 3},
},
args: args{
prediction: []float64{2, 3, 4},
labels: []float64{1, 1, 1},
},
wantERRors: []float64{1, 4, 9},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &outputDense{
activation: tt.fields.activation,
cost: tt.fields.cost,
prevLayerSize: tt.fields.prevLayerSize,
input: tt.fields.input,
}
gotERRors, err := l.backward(tt.args.prediction, tt.args.labels)
if (err != nil) != tt.wantErr {
t.Errorf("outputDense.backward() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotERRors, tt.wantERRors) {
t.Errorf("outputDense.backward() = %v, want %v", gotERRors, tt.wantERRors)
}
})
}
}
func Test_hiddenDense_updateCorrections(t *testing.T) {
type fields struct {
currLayerSize int
nextLayerSize int
activated []float64
bias bool
}
type args struct {
eRRors []float64
}
tests := []struct {
name string
fields fields
args args
want [][]float64
}{
{
name: "updateCorrectionsHiddenToOutput",
fields: fields{
currLayerSize: 5,
nextLayerSize: 3,
activated: []float64{2, 3, 4, 5},
bias: true,
},
args: args{[]float64{1, 4, 9}},
want: [][]float64{
{2, 8, 18}, {3, 12, 27}, {4, 16, 36}, {5, 20, 45}, {1, 4, 9},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &hiddenDense{
currLayerSize: tt.fields.currLayerSize,
nextLayerSize: tt.fields.nextLayerSize,
activated: tt.fields.activated,
bias: tt.fields.bias,
}
if got := l.updateCorrections(tt.args.eRRors); !reflect.DeepEqual(got, tt.want) {
t.Errorf("hiddenDense.updateCorrections() = %v, want %v", got, tt.want)
}
})
}
}
func Test_inputDense_applyCorrections(t *testing.T) {
type fields struct {
synapseInitializer synapseInitializer
corrections [][]float64
synapses [][]float64
nextLayerSize int
currLayerSize int
learningRate float64
input []float64
bias bool
}
type args struct {
batchSize float64
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &inputDense{
synapseInitializer: tt.fields.synapseInitializer,
corrections: tt.fields.corrections,
synapses: tt.fields.synapses,
nextLayerSize: tt.fields.nextLayerSize,
currLayerSize: tt.fields.currLayerSize,
learningRate: tt.fields.learningRate,
input: tt.fields.input,
bias: tt.fields.bias,
}
if err := l.applyCorrections(tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("inputDense.applyCorrections() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_hiddenDense_applyCorrections(t *testing.T) {
type fields struct {
currLayerSize int
nextLayerSize int
learningRate float64
corrections [][]float64
synapses [][]float64
nextBias, bias bool
}
type args struct {
batchSize float64
}
tests := []struct {
name string
fields fields
args args
wantErr bool
want [][]float64
}{
{
name: "applyCorrections",
fields: fields{
currLayerSize: 5,
nextLayerSize: 3,
learningRate: 1,
corrections: [][]float64{
{2, 8, 18}, {3, 12, 27}, {4, 16, 36}, {5, 20, 45}, {1, 4, 9},
},
synapses: [][]float64{
{2, 8, 18}, {3, 12, 27}, {4, 16, 36}, {5, 20, 45}, {1, 4, 9},
},
bias: true,
},
args: args{1},
want: [][]float64{
{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &hiddenDense{
currLayerSize: tt.fields.currLayerSize,
nextLayerSize: tt.fields.nextLayerSize,
learningRate: tt.fields.learningRate,
corrections: tt.fields.corrections,
synapses: tt.fields.synapses,
nextBias: tt.fields.nextBias,
bias: tt.fields.bias,
}
if err := l.applyCorrections(tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("hiddenDense.applyCorrections() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(l.synapses, tt.want) {
t.Errorf("hiddenDense.synapses = %v, want %v", l.synapses, tt.want)
}
})
}
}