-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathneuron_test.go
133 lines (117 loc) · 3.2 KB
/
neuron_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
package wann
import (
"fmt"
"math"
"math/rand"
"testing"
)
func TestNeuron(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork()
n, _ := net.NewBlankNeuron()
n.ActivationFunction = Swish
result := n.GetActivationFunction()(0.5)
diff := math.Abs(result - 0.311287)
if diff > 0.00001 { // 0.0000001 {
t.Errorf("default swish activation function, expected a number close to 0.311287, got %f:", result)
}
fmt.Printf("Neurons in network: %d\n", len(net.AllNodes))
}
func TestString(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork()
n, _ := net.NewBlankNeuron()
_ = n.String()
}
func TestHasInput(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork() // 0
a, _ := net.NewBlankNeuron() // 1
b, _ := net.NewBlankNeuron() // 2
fmt.Println("a is 1?", a)
fmt.Println("b is 2?", b)
a.AddInput(0)
if !a.HasInput(0) {
t.Errorf("a should have b as an input")
}
if b.HasInput(0) {
t.Errorf("b should not have a as an input")
}
}
func TestFindInput(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork()
a, _ := net.NewBlankNeuron() // a, 1
_, bi := net.NewBlankNeuron() // b, 2
c, ci := net.NewBlankNeuron() // c, 3
_, di := net.NewBlankNeuron() // d, 4
a.AddInput(bi) // b
a.AddInputNeuron(c) // c
if _, found := a.FindInput(di); found {
t.Errorf("a should not have d as an input")
}
if pos, found := a.FindInput(bi); !found {
t.Errorf("a should have b as an input")
} else if found && pos != 0 {
t.Errorf("a should have b as an input at position 0")
}
if pos, found := a.FindInput(ci); !found {
t.Errorf("a should have c as an input")
} else if found && pos != 1 {
t.Errorf("a should have c as an input at position 1")
}
}
func TestRemoveInput(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork(&Config{
inputs: 5,
InitialConnectionRatio: 0.5,
sharedWeight: 0.5,
})
a, _ := net.NewBlankNeuron() // 0
a.AddInput(1)
a.AddInput(2)
if a.RemoveInput(1) != nil {
t.Errorf("could not remove input b from a")
}
if a.RemoveInput(2) != nil {
t.Errorf("could not remove input c from a")
}
if a.HasInput(1) {
t.Errorf("a should not have b as an input")
}
if a.HasInput(2) {
t.Errorf("a should not have c as an input")
}
}
// func (neuron *Neuron) RemoveInput(e *Neuron) error {
func TestEvaluate(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork(&Config{
inputs: 7,
InitialConnectionRatio: 0.5,
sharedWeight: 0.5,
})
// Set a few activation functions
net.AllNodes[net.InputNodes[0]].ActivationFunction = Linear
net.AllNodes[net.InputNodes[1]].ActivationFunction = Swish
net.AllNodes[net.InputNodes[2]].ActivationFunction = Gauss
net.AllNodes[net.InputNodes[3]].ActivationFunction = Sigmoid
net.AllNodes[net.InputNodes[4]].ActivationFunction = ReLU
net.AllNodes[net.InputNodes[5]].ActivationFunction = Step
net.AllNodes[net.InputNodes[6]].ActivationFunction = Inv
result := net.Evaluate([]float64{0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5})
fmt.Println(result)
}
func TestIn(t *testing.T) {
rand.Seed(commonSeed)
net := NewNetwork()
n, ni := net.NewNeuron()
if ni != 1 {
t.Fail()
}
outputNeuronIndex := NeuronIndex(0)
if !n.In([]NeuronIndex{outputNeuronIndex, 1}) {
t.Fail()
}
}