-
Notifications
You must be signed in to change notification settings - Fork 1
/
backpropagation_test.go
75 lines (58 loc) · 1.53 KB
/
backpropagation_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
package neural
import (
"log"
"math/rand"
"testing"
)
func TestBPComputeDeltas(t *testing.T) {
}
func TestBPUpdateWeights(t *testing.T) {
}
func TestBPLearn(t *testing.T) {
net := NewNetwork(5, 2, []int{3, 4, 3}, SigmoidActivation, SigmoidActivation)
samples := [][]float64{
[]float64{0, 2, 2, 1, 2}, // []float64{0, 2, 3, 2, 2}, []float64{0, 2, 1, 3, 2},
}
targets := [][]float64{
[]float64{1, 2}, []float64{1, 1}, []float64{1, 3},
}
net.RandomizeWeights(-0.05, 0.05, rand.NewSource(1))
bp := NewBackPropagation(net, 0.3, 0.2)
for i, layer := range net.Layers {
for j, node := range layer.Nodes {
log.Println(i, j, node.Weights)
}
}
net.Train(samples, targets, bp)
for i, layer := range net.Layers {
for j, node := range layer.Nodes {
log.Println(i, j, node.Weights)
}
}
}
func BenchmarkBPTrain(b *testing.B) {
inputs := 5
outputs := 2
count := 2000
net := NewNetwork(inputs, outputs, []int{3, 4, 3}, SigmoidActivation, SigmoidActivation)
samples := make([][]float64, count)
for i := 0; i < len(samples); i++ {
samples[i] = make([]float64, inputs)
for j := 0; j < len(samples[i]); j++ {
samples[i][j] = rand.Float64()
}
}
targets := make([][]float64, count)
for i := 0; i < len(targets); i++ {
targets[i] = make([]float64, outputs)
for j := 0; j < len(targets[i]); j++ {
targets[i][j] = rand.Float64()
}
}
net.RandomizeWeights(-0.05, 0.05, rand.NewSource(1))
bp := NewBackPropagation(net, 0.3, 0.2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
net.Train(samples, targets, bp)
}
}