-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgomind_test.go
176 lines (152 loc) · 3.98 KB
/
gomind_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
package gomind
import (
"errors"
"testing"
"github.com/surenderthakran/gomind/layer"
"github.com/google/go-cmp/cmp"
)
type fakeNeuralNetwork struct{}
func (fakeNeuralNetwork) CalculateOutput(input []float64) []float64 {
return []float64{0}
}
func (fakeNeuralNetwork) LastOutput() []float64 {
return []float64{0}
}
func (fakeNeuralNetwork) HiddenLayer() *layer.Layer {
return nil
}
func (fakeNeuralNetwork) OutputLayer() *layer.Layer {
return nil
}
func (fakeNeuralNetwork) CalculateNewOutputLayerWeights(outputs, targetOutputs []float64) error {
return nil
}
func (fakeNeuralNetwork) CalculateNewHiddenLayerWeights() error {
return nil
}
func (fakeNeuralNetwork) CalculateError(targetOutput []float64) (float64, error) {
return 0, nil
}
func (fakeNeuralNetwork) UpdateWeights() {}
func TestModel(t *testing.T) {
model := &Model{
numberOfInputs: 2,
numberOfHiddenNeurons: 16,
hiddenLayerActivationFunctionName: "RELU",
numberOfOutputs: 1,
outputLayerActivationFunctionName: "SIGMOID",
learningRate: 0.5,
network: &fakeNeuralNetwork{},
}
t.Run("LearnSample", func(t *testing.T) {
if err := model.LearnSample([]float64{0}, []float64{0}); err != nil {
t.Fatalf("Model.LearnSample(%v) = %v", []float64{0}, err)
}
})
t.Run("LastOutput", func(t *testing.T) {
lastOutput := model.LastOutput()
if !cmp.Equal([]float64{0}, lastOutput) {
t.Errorf("Model.LastOutput() = %v, want %v", lastOutput, []float64{0})
}
})
t.Run("CalculateError", func(t *testing.T) {
error, err := model.CalculateError([]float64{0})
if err != nil {
t.Fatalf("Model.CalculateError(%v) = %v", []float64{0}, err)
}
if error != 0 {
t.Errorf("Model.CalculateError(%v) = %v, want %v", []float64{0}, error, 0)
}
})
}
func TestEstimateIdealNumberOfHiddenLayerNeurons(t *testing.T) {
testCases := []struct {
inputs int
outputs int
want int
}{
{
inputs: 2,
outputs: 1,
want: 2,
},
{
inputs: 20,
outputs: 1,
want: 14,
},
{
inputs: 1,
outputs: 5,
want: 1,
},
}
for _, test := range testCases {
hidden := estimateIdealNumberOfHiddenLayerNeurons(test.inputs, test.outputs)
if hidden != test.want {
t.Errorf("estimateIdealNumberOfHiddenLayerNeurons(%d, %d) = %d, want %d", test.inputs, test.outputs, hidden, test.want)
}
}
}
func TestNew(t *testing.T) {
testCases := []struct {
modelConfig *ModelConfiguration
model *Model
err error
}{
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 0,
},
err: errors.New("NumberOfInputs field in ModelConfiguration is a mandatory field which cannot be zero."),
},
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 2,
NumberOfOutputs: 0,
},
err: errors.New("NumberOfOutputs field in ModelConfiguration is a mandatory field which cannot be zero."),
},
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 2,
NumberOfOutputs: 1,
LearningRate: 1.5,
},
err: errors.New("LearningRate cannot be less than 0 or greater than 1."),
},
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 2,
NumberOfOutputs: 1,
LearningRate: 0,
},
err: nil,
},
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 2,
NumberOfOutputs: 1,
LearningRate: 0.5,
},
err: nil,
},
{
modelConfig: &ModelConfiguration{
NumberOfInputs: 2,
NumberOfOutputs: 1,
NumberOfHiddenLayerNeurons: 5,
LearningRate: 0.5,
HiddenLayerActivationFunctionName: "sigmoid",
OutputLayerActivationFunctionName: "linear",
},
err: nil,
},
}
for _, test := range testCases {
_, err := New(test.modelConfig)
if (err == nil && test.err != nil) || (err != nil && test.err == nil) || (err != nil && test.err != nil && err.Error() != test.err.Error()) {
t.Errorf("New(%v) = _, %v, want _, %v", test.modelConfig, err, test.err)
}
}
}