|
@@ -15,10 +15,6 @@ type NeuralNetwork struct {
|
|
|
trainingCycles int
|
|
|
}
|
|
|
|
|
|
-func (nn *NeuralNetwork) Result() *mat.Dense {
|
|
|
- return nn.A[nn.Count-1]
|
|
|
-}
|
|
|
-
|
|
|
func NewNeuralNetwork(Sizes []int, nu float64, trainingCycles int) (nn *NeuralNetwork) {
|
|
|
nn = &NeuralNetwork{}
|
|
|
nn.Sizes = Sizes
|
|
@@ -38,8 +34,8 @@ func NewNeuralNetwork(Sizes []int, nu float64, trainingCycles int) (nn *NeuralNe
|
|
|
}
|
|
|
|
|
|
func (nn *NeuralNetwork) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
|
|
|
- nn.Forward(aIn)
|
|
|
- result := nn.Result()
|
|
|
+ nn.forward(aIn)
|
|
|
+ result := nn.result()
|
|
|
r, _ := result.Dims()
|
|
|
max = 0.0
|
|
|
maxIndex = 0
|
|
@@ -59,7 +55,7 @@ func (nn *NeuralNetwork) Train(dataSet, expect []*mat.Dense) {
|
|
|
if j < 0 {
|
|
|
j = 0
|
|
|
}
|
|
|
- nn.Backward(dataSet[j], expect[j])
|
|
|
+ nn.backward(dataSet[j], expect[j])
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -72,7 +68,7 @@ func (nn *NeuralNetwork) LoadState(filename string) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-func (nn *NeuralNetwork) Forward(aIn mat.Matrix) {
|
|
|
+func (nn *NeuralNetwork) forward(aIn mat.Matrix) {
|
|
|
nn.A[0] = mat.DenseCopyOf(aIn)
|
|
|
|
|
|
for i := 1; i < nn.Count; i++ {
|
|
@@ -93,14 +89,14 @@ func (nn *NeuralNetwork) Forward(aIn mat.Matrix) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (nn *NeuralNetwork) Backward(aIn, aOut mat.Matrix) {
|
|
|
- nn.Forward(aIn)
|
|
|
+func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
|
|
|
+ nn.forward(aIn)
|
|
|
|
|
|
lastLayerNum := nn.Count - 1
|
|
|
|
|
|
|
|
|
err := &mat.Dense{}
|
|
|
- err.Sub(nn.Result(), aOut)
|
|
|
+ err.Sub(nn.result(), aOut)
|
|
|
|
|
|
sigmoidsPrime := &mat.Dense{}
|
|
|
sigmoidsPrime.Apply(applySigmoidPrime, nn.Z[lastLayerNum])
|
|
@@ -145,11 +141,6 @@ func (nn *NeuralNetwork) Backward(aIn, aOut mat.Matrix) {
|
|
|
nn.Weights = newWeights
|
|
|
}
|
|
|
|
|
|
-func makeBackGradien(in mat.Matrix, actual mat.Matrix, alpha float64) *mat.Dense {
|
|
|
- scaled := &mat.Dense{}
|
|
|
- result := &mat.Dense{}
|
|
|
-
|
|
|
- scaled.Scale(alpha, in)
|
|
|
- result.Sub(actual, scaled)
|
|
|
- return result
|
|
|
+func (nn *NeuralNetwork) result() *mat.Dense {
|
|
|
+ return nn.A[nn.Count-1]
|
|
|
}
|