-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhopfield_test.go
162 lines (144 loc) · 2.92 KB
/
hopfield_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
package automata_test
import (
"github.com/Kegsay/automata"
"strings"
"testing"
)
const hopfieldImageOne = `
0001000
0001000
0001000
0001000
0001000
0001000
0001000`
const hopfieldImageTwo = `
1111111
0000001
0000001
1111111
1000000
1000000
1111111`
const hopfieldImageThree = `
1111111
0000001
0000001
1111111
0000001
0000001
1111111`
const hopfieldImageFour = `
1000001
1000001
1000001
1111111
0000001
0000001
0000001`
const hopfieldImageFive = `
1111111
1000000
1000000
1111111
0000001
0000001
1111111`
func TestHopfieldImages(t *testing.T) {
testLookupTable := &automata.LookupTable{}
gridSize := 7
hopfield := automata.NewHopfieldNetwork(testLookupTable, gridSize*gridSize) // 7x7 grid
// Train it with "images" of numbers 1 -> 5.
trainer := automata.Trainer{
Network: hopfield,
MaxErrorRate: 0.00001,
LearnRate: 1,
Iterations: 10000,
CostFunction: &automata.MeanSquaredErrorCost{},
}
trainingSet := []automata.TrainSet{
imageToTrainSet(t, hopfieldImageOne),
imageToTrainSet(t, hopfieldImageTwo),
imageToTrainSet(t, hopfieldImageThree),
imageToTrainSet(t, hopfieldImageFour),
imageToTrainSet(t, hopfieldImageFive),
}
err := trainer.Train(trainingSet)
if err != nil {
t.Fatalf("trainer.Train threw error: %s", err.Error())
}
// test it with a serif one
activateNetwork(
t, &hopfield.Network, imageToInput(t, `
0011000
0001000
0001000
0001000
0001000
0001000
0011100`), imageToInput(t, hopfieldImageOne))
// test it with a two
activateNetwork(
t, &hopfield.Network, imageToInput(t, `
0011100
1100011
0000010
0111000
1000000
1000001
0111110`), imageToInput(t, hopfieldImageTwo))
// test it with a three
activateNetwork(
t, &hopfield.Network, imageToInput(t, `
0111110
1000001
0000001
0011110
0000001
0000001
0011110`), imageToInput(t, hopfieldImageThree))
// test it with a four
activateNetwork(
t, &hopfield.Network, imageToInput(t, `
1000001
0100001
0010010
0011110
0000001
0000001
0000010`), imageToInput(t, hopfieldImageFour))
// test it with a five
activateNetwork(
t, &hopfield.Network, imageToInput(t, `
1111110
1100000
1100000
0111110
0000001
1000001
0111110`), imageToInput(t, hopfieldImageFive))
}
func imageToTrainSet(t *testing.T, img string) automata.TrainSet {
input := imageToInput(t, img)
return automata.TrainSet{
Input: input,
Output: input,
}
}
func imageToInput(t *testing.T, img string) []float64 {
// strip new lines
img = strings.Replace(img, "\r", "", -1)
img = strings.Replace(img, "\n", "", -1)
// convert 0 and 1 to actual floats and return
set := make([]float64, len(img))
for i := 0; i < len(img); i++ {
if img[i] == '0' {
set[i] = float64(0)
} else if img[i] == '1' {
set[i] = float64(1)
} else {
t.Fatalf("imageToTrainSet: bad test image char: %q", img[i])
}
}
return set
}