-
Notifications
You must be signed in to change notification settings - Fork 26
/
neural_net_test.go
122 lines (96 loc) · 2.74 KB
/
neural_net_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
package ml
import (
"testing"
"fmt"
)
func TestRollUnroll(t *testing.T) {
fmt.Println("Testing Roll & Unroll neural networks...")
nn := NewNeuralNetFromCsv(
"test_data/nn/x.csv",
"test_data/nn/y.csv",
[]string{},
)
nn.InitializeThetas([]int{400, 50, 10})
initTheta := make([][][]float64, len(nn.Theta))
for i := 0; i < len(nn.Theta); i++ {
initTheta[i] = make([][]float64, len(nn.Theta[i]))
for j := 0; j < len(nn.Theta); j++ {
initTheta[i][j] = make([]float64, len(nn.Theta[i][j]))
for k := 0; k < len(nn.Theta); k++ {
initTheta[i][j][k] = nn.Theta[i][j][k]
}
}
}
finalTheta := nn.unrollThetasGrad(nn.rollThetasGrad(nn.Theta))
for i := 0; i < len(nn.Theta); i++ {
for j := 0; j < len(nn.Theta); j++ {
for k := 0; k < len(nn.Theta); k++ {
if finalTheta[i][j][k] != initTheta[i][j][k] {
t.Error("Theta val after roll and unroll: ", finalTheta[i][j][k], "init theta:", initTheta[i][j][k], "pos:", i, j, k)
}
}
}
}
}
// Using a predefined dataset stored in test_data/test_linear.dat, calculates
// the cost and gradient for different lambda y theta
func TestNeuralNet(t *testing.T) {
fmt.Println("Testing Neural Network Fmincg function...")
nn := NewNeuralNetFromCsv(
"test_data/nn/x.csv",
"test_data/nn/y.csv",
[]string{"test_data/nn/initial_Theta1.csv", "test_data/nn/initial_Theta2.csv"},
)
fx, i, err := Fmincg(nn, 1, 3, false)
if err != nil {
t.Error(err)
}
if i != 0 {
t.Error("Some error happened on fmincgNn calculation :'(")
}
if fx[0] > 3.3 {
t.Error("Expected J too hight on first iteration:", fx[0])
}
if fx[1] > 3.25 {
t.Error("Expected J too hight on first iteration:", fx[1])
}
if fx[2] > 3.21 {
t.Error("Expected J too hight on first iteration:", fx[2])
}
}
func TestNeuralNetMinimizeCost(t *testing.T) {
fmt.Println("Testing Neural Network training...")
nn := NewNeuralNetFromCsv(
"test_data/nn/x.csv",
"test_data/nn/y.csv",
[]string{},
)
nn.InitializeThetas([]int{400, 25, 10})
j, performance, _, _ := nn.MinimizeCost(30, true, true)
if j > 0.8 {
t.Error("Cost bigger than 0.8:", j)
}
if performance < 0.8 {
t.Error("Performance worstest than 0.8:", performance)
}
}
func TestNeuralNetSaveLoad(t *testing.T) {
fmt.Println("Testing Save / Load of thetas for Neural Networks...")
nn := NewNeuralNetFromCsv(
"test_data/nn/x.csv",
"test_data/nn/y.csv",
[]string{},
)
nn.InitializeThetas([]int{400, 25, 10})
files := nn.SaveThetas("test_data/nn/")
nn2 := NewNeuralNetFromCsv(
"test_data/nn/x.csv",
"test_data/nn/y.csv",
files,
)
j1, _, _ := nn.CostFunction(1, false)
j2, _, _ := nn2.CostFunction(1, false)
if j1 != j2 {
t.Error("The cost returned after store and read the Thetas from files, doesn't match with the initial theta")
}
}