|
@@ -148,19 +148,19 @@ func (nn *NeuralNetwork) forward(aIn mat.Matrix) {
|
|
|
aSrc := nn.A[i-1]
|
|
|
aDst := nn.A[i]
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
aDst.Mul(nn.Weights[i], aSrc)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
aDst.Add(aDst, nn.Biases[i])
|
|
|
|
|
|
-
|
|
|
+
|
|
|
nn.Z[i] = mat.DenseCopyOf(aDst)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
aDst.Apply(applySigmoid, aDst)
|
|
|
}
|
|
|
}
|
|
@@ -170,75 +170,75 @@ func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
|
|
|
|
|
|
lastLayerNum := nn.Count - 1
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
err := &mat.Dense{}
|
|
|
err.Sub(nn.result(), aOut)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
sigmoidsPrime := &mat.Dense{}
|
|
|
sigmoidsPrime.Apply(applySigmoidPrime, nn.Z[lastLayerNum])
|
|
|
|
|
|
-
|
|
|
+
|
|
|
delta := &mat.Dense{}
|
|
|
delta.MulElem(err, sigmoidsPrime)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
biases := mat.DenseCopyOf(delta)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
weights := &mat.Dense{}
|
|
|
weights.Mul(delta, nn.A[lastLayerNum-1].T())
|
|
|
|
|
|
-
|
|
|
- newBiases := []*mat.Dense{makeBackGradien(biases, nn.Biases[lastLayerNum], nn.alpha)}
|
|
|
- newWeights := []*mat.Dense{makeBackGradien(weights, nn.Weights[lastLayerNum], nn.alpha)}
|
|
|
+
|
|
|
+ newBiases := []*mat.Dense{makeBackGradient(biases, nn.Biases[lastLayerNum], nn.alpha)}
|
|
|
+ newWeights := []*mat.Dense{makeBackGradient(weights, nn.Weights[lastLayerNum], nn.alpha)}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
err = delta
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
for l := nn.Count - 2; l > 0; l-- {
|
|
|
-
|
|
|
+
|
|
|
sigmoidsPrime := &mat.Dense{}
|
|
|
sigmoidsPrime.Apply(applySigmoidPrime, nn.Z[l])
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
delta := &mat.Dense{}
|
|
|
wdelta := &mat.Dense{}
|
|
|
wdelta.Mul(nn.Weights[l+1].T(), err)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
delta.MulElem(wdelta, sigmoidsPrime)
|
|
|
err = delta
|
|
|
|
|
|
-
|
|
|
+
|
|
|
biases := mat.DenseCopyOf(delta)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -246,16 +246,16 @@ func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -266,10 +266,9 @@ func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
|
|
|
weights := &mat.Dense{}
|
|
|
weights.Mul(delta, nn.A[l-1].T())
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- newBiases = append([]*mat.Dense{makeBackGradien(biases, nn.Biases[l], nn.alpha)}, newBiases...)
|
|
|
- newWeights = append([]*mat.Dense{makeBackGradien(weights, nn.Weights[l], nn.alpha)}, newWeights...)
|
|
|
+
|
|
|
+ newBiases = append([]*mat.Dense{makeBackGradient(biases, nn.Biases[l], nn.alpha)}, newBiases...)
|
|
|
+ newWeights = append([]*mat.Dense{makeBackGradient(weights, nn.Weights[l], nn.alpha)}, newWeights...)
|
|
|
}
|
|
|
|
|
|
newBiases = append([]*mat.Dense{&mat.Dense{}}, newBiases...)
|