This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralnetwork.go
224 lines (199 loc) · 6.28 KB
/
neuralnetwork.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package bareml
import "math"
type CostFunction uint8
const (
COST_MSE CostFunction = 1
)
type NeuralNetwork struct {
topologySize int
hiddenActivationType ActivationType
outputActivationType ActivationType
costFunction CostFunction
topology []int
layers []*Layer
weightMatrices []*Matrix
gradientMatrices []*Matrix
input []float64
target []float64
errors []float64
derivedErrors []float64
error float64
bias float64
momentum float64
learningRate float64
}
func NewNeuralNetwork(topology []int) *NeuralNetwork {
nn := new(NeuralNetwork)
nn.topology = topology
nn.topologySize = len(topology)
nn.hiddenActivationType = RELU
nn.outputActivationType = SIGM
nn.costFunction = COST_MSE
nn.errors = make([]float64, topology[nn.topologySize-1])
nn.derivedErrors = make([]float64, topology[nn.topologySize-1])
nn.layers = make([]*Layer, nn.topologySize)
nn.weightMatrices = make([]*Matrix, nn.topologySize-1)
// initalize the topology
for i := 0; i < nn.topologySize; i++ {
if i > 0 && i < (nn.topologySize-1) {
nn.layers[i] = NewLayer(topology[i], nn.hiddenActivationType)
} else if i == (nn.topologySize - 1) {
nn.layers[i] = NewLayer(topology[i], nn.outputActivationType)
} else {
nn.layers[i] = NewLayer(topology[i], 0)
}
}
// initlize the weights
for i := 0; i < (nn.topologySize - 1); i++ {
nn.weightMatrices[i] = NewMatrix(topology[i], topology[i+1], true)
}
return nn
}
func (nn *NeuralNetwork) Train(input []float64, target []float64, bias float64, learningRate float64, momentum float64) {
nn.learningRate = learningRate
nn.bias = bias
nn.momentum = momentum
nn.target = target
nn.input = input
for i := 0; i < len(input); i++ {
nn.layers[0].Set(i, input[i])
}
nn.feedForward()
nn.setErrors()
nn.backPropagation()
}
func (nn *NeuralNetwork) feedForward() {
var (
a *Matrix
b *Matrix
c *Matrix
)
for i := 0; i < (nn.topologySize - 1); i++ {
if i != 0 {
a = nn.ActivatedNeuronMatrix(i)
} else {
a = nn.RawNeuronMatrix(i)
}
b = nn.WeightMatrix(i)
c = MultiplyMatrix(a, b)
for c_index := 0; c_index < c.NumCols(); c_index++ {
nn.setNeuronVal(i+1, c_index, c.Get(0, c_index)+nn.bias)
}
}
}
func (nn *NeuralNetwork) backPropagation() {
var (
newWeights []*Matrix = make([]*Matrix, 0)
deltaWeights *Matrix
gradients *Matrix
derivedValues *Matrix
zActivatedValues *Matrix
tempNewWeights *Matrix
hiddenDerived *Matrix
indexOutputLayer = nn.topologySize - 1
originalValue float64
deltaValue float64
g float64
)
// PART 1 : OUTPUT to LAST HIDDEN LAYER
gradients = NewMatrix(1, nn.topology[indexOutputLayer], false)
derivedValues = nn.layers[indexOutputLayer].MatrixifyDerivedVals()
for i := 0; i < nn.topology[indexOutputLayer]; i++ {
gradients.Set(0, i, (nn.derivedErrors[i] * derivedValues.Get(0, i)))
}
// Gt * Z
zActivatedValues = nn.layers[indexOutputLayer-1].MatrixifyActivatedVals()
deltaWeights = MultiplyMatrix(gradients.Transpose(), zActivatedValues)
// Compute for new weights (last hidden <-> output)
tempNewWeights = NewMatrix(nn.topology[indexOutputLayer-1], nn.topology[indexOutputLayer], false)
for r := 0; r < nn.topology[indexOutputLayer-1]; r++ {
for c := 0; c < nn.topology[indexOutputLayer]; c++ {
originalValue = nn.weightMatrices[indexOutputLayer-1].Get(r, c) * nn.momentum
deltaValue = deltaWeights.Get(c, r) * nn.learningRate
tempNewWeights.Set(r, c, (originalValue - deltaValue))
}
}
newWeights = append(newWeights, tempNewWeights)
// PART 2 : LAST HIDDEN LAYER to INPUT LAYER
for i := (indexOutputLayer - 1); i > 0; i-- {
gradients = MultiplyMatrix(gradients, nn.weightMatrices[i].Transpose())
hiddenDerived = nn.layers[i].MatrixifyDerivedVals()
for colCounter := 0; colCounter < hiddenDerived.NumCols(); colCounter++ {
g = gradients.Get(0, colCounter) * hiddenDerived.Get(0, colCounter)
gradients.Set(0, colCounter, g)
}
if i == 1 {
zActivatedValues = nn.layers[0].MatrixifyRawVals()
} else {
zActivatedValues = nn.layers[i-1].MatrixifyActivatedVals()
}
deltaWeights = MultiplyMatrix(zActivatedValues.Transpose(), gradients)
// update weights
tempNewWeights = NewMatrix(nn.weightMatrices[i-1].NumRows(), nn.weightMatrices[i-1].NumCols(), false)
for r := 0; r < tempNewWeights.NumRows(); r++ {
for c := 0; c < tempNewWeights.NumCols(); c++ {
originalValue = nn.weightMatrices[i-1].Get(r, c) * nn.momentum
deltaValue = deltaWeights.Get(r, c) * nn.learningRate
tempNewWeights.Set(r, c, (originalValue - deltaValue))
}
}
newWeights = append(newWeights, tempNewWeights)
}
nn.weightMatrices = make([]*Matrix, len(newWeights))
for i := 0; i < len(newWeights); i++ {
nn.weightMatrices[i] = newWeights[len(newWeights)-i-1]
}
}
func (nn *NeuralNetwork) setErrors() {
switch nn.costFunction {
case COST_MSE:
nn.setErrorMSE()
break
default:
nn.setErrorMSE()
break
}
}
func (nn *NeuralNetwork) setErrorMSE() {
var (
outputLayerIndex = len(nn.layers) - 1
outputNeurons = nn.layers[outputLayerIndex].GetNeurons()
t float64
y float64
)
nn.error = 0.00
for i := 0; i < len(nn.target); i++ {
t = nn.target[i]
y = outputNeurons[i].ActivatedVal()
nn.errors[i] = 0.5 * math.Pow(math.Abs(t-y), 2)
nn.derivedErrors[i] = (y - t)
nn.error += nn.errors[i]
}
}
func (nn *NeuralNetwork) setNeuronVal(indexLayer int, indexNeuron int, val float64) {
nn.layers[indexLayer].Set(indexNeuron, val)
}
func (nn *NeuralNetwork) ActivatedVals(index int) []float64 {
return nn.layers[index].ActivatedVals()
}
func (nn *NeuralNetwork) RawVals(index int) []float64 {
return nn.layers[index].RawVals()
}
func (nn *NeuralNetwork) DerivedVals(index int) []float64 {
return nn.layers[index].DerivedVals()
}
func (nn *NeuralNetwork) RawNeuronMatrix(index int) *Matrix {
return nn.layers[index].MatrixifyRawVals()
}
func (nn *NeuralNetwork) ActivatedNeuronMatrix(index int) *Matrix {
return nn.layers[index].MatrixifyActivatedVals()
}
func (nn *NeuralNetwork) DerivedNeuronMatrix(index int) *Matrix {
return nn.layers[index].MatrixifyDerivedVals()
}
func (nn *NeuralNetwork) WeightMatrix(index int) *Matrix {
return nn.weightMatrices[index].Copy()
}
func (nn *NeuralNetwork) TotalError() float64 {
return nn.error
}