|
@@ -41,47 +41,56 @@ import (
|
|
|
//
|
|
|
// Matrix: A
|
|
|
// Description: A is set of calculated neuron activations after sigmoid correction
|
|
|
-// Format: 0 n N
|
|
|
+// Format: 0 l L
|
|
|
// ⎡A[0] ⎤ ... ⎡A[0] ⎤ ... ⎡A[0] ⎤
|
|
|
// ⎢A[1] ⎥ ... ⎢A[1] ⎥ ... ⎢A[1] ⎥
|
|
|
// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
// ⎢A[i] ⎥ ... ⎢A[i] ⎥ ... ⎢A[i] ⎥
|
|
|
// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
// ⎣A[s] ⎦ ... ⎣A[s] ⎦ ... ⎣A[s] ⎦
|
|
|
-// Where s = Sizes[n], N = len(Sizes)
|
|
|
+// Where s = Sizes[l] - Neural network layer size
|
|
|
+// L = len(Sizes) - Number of neural network layers
|
|
|
//
|
|
|
// Matrix: Z
|
|
|
// Description: Z is set of calculated raw neuron activations
|
|
|
-// Format: 0 n N
|
|
|
+// Format: 0 l L
|
|
|
// ⎡Z[0] ⎤ ... ⎡Z[0] ⎤ ... ⎡Z[0] ⎤
|
|
|
// ⎢Z[1] ⎥ ... ⎢Z[1] ⎥ ... ⎢Z[1] ⎥
|
|
|
// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
// ⎢Z[i] ⎥ ... ⎢Z[i] ⎥ ... ⎢Z[i] ⎥
|
|
|
// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
// ⎣Z[s] ⎦ ... ⎣Z[s] ⎦ ... ⎣Z[s] ⎦
|
|
|
-// Where s = Sizes[n], N = len(Sizes)
|
|
|
+// Where s = Sizes[l] - Neural network layer size
|
|
|
+// L = len(Sizes) - Number of neural network layers
|
|
|
//
|
|
|
// Matrix: Biases
|
|
|
-// Description: Biases is set of biases per layer except L0
|
|
|
-// Format:
|
|
|
-// ⎡b[0] ⎤
|
|
|
-// ⎢b[1] ⎥
|
|
|
-// ⎢ ... ⎥
|
|
|
-// ⎢b[i] ⎥
|
|
|
-// ⎢ ... ⎥
|
|
|
-// ⎣b[s] ⎦
|
|
|
-// Where s = Sizes[n]
|
|
|
+// Description: Biases is set of biases per layer except l0
|
|
|
+// NOTE: l0 is always empty Dense because first layer
|
|
|
+// doesn't have connections to previous layer
|
|
|
+// Format: 1 l L
|
|
|
+// ⎡b[0] ⎤ ... ⎡b[0] ⎤ ... ⎡b[0] ⎤
|
|
|
+// ⎢b[1] ⎥ ... ⎢b[1] ⎥ ... ⎢b[1] ⎥
|
|
|
+// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
+// ⎢b[i] ⎥ ... ⎢b[i] ⎥ ... ⎢b[i] ⎥
|
|
|
+// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
+// ⎣b[s] ⎦ ... ⎣b[s] ⎦ ... ⎣b[s] ⎦
|
|
|
+// Where s = Sizes[l] - Neural network layer size
|
|
|
+// L = len(Sizes) - Number of neural network layers
|
|
|
//
|
|
|
// Matrix: Weights
|
|
|
-// Description: Weights is set of weights per layer except L0
|
|
|
-// Format:
|
|
|
-// ⎡w[0,0] ... w[0,j] ... w[0,s']⎤
|
|
|
-// ⎢w[1,0] ... w[1,j] ... w[1,s']⎥
|
|
|
-// ⎢ ... ⎥
|
|
|
-// ⎢w[i,0] ... w[i,j] ... w[i,s']⎥
|
|
|
-// ⎢ ... ⎥
|
|
|
-// ⎣w[s,0] ... w[s,j] ... w[s,s']⎦
|
|
|
-// Where s = Sizes[n], s' = Sizes[n-1]
|
|
|
+// Description: Weights is set of weights per layer except l0
|
|
|
+// NOTE: l0 is always empty Dense because first layer
|
|
|
+// doesn't have connections to previous layer
|
|
|
+// Format: 1 l L
|
|
|
+// ⎡w[0,0] ... w[0,j] ... w[0,s']⎤ ... ⎡w[0,0] ... w[0,j] ... w[0,s']⎤ ... ⎡w[0,0] ... w[0,j] ... w[0,s']⎤
|
|
|
+// ⎢w[1,0] ... w[1,j] ... w[1,s']⎥ ... ⎢w[1,0] ... w[1,j] ... w[1,s']⎥ ... ⎢w[1,0] ... w[1,j] ... w[1,s']⎥
|
|
|
+// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
+// ⎢w[i,0] ... w[i,j] ... w[i,s']⎥ ... ⎢w[i,0] ... w[i,j] ... w[i,s']⎥ ... ⎢w[i,0] ... w[i,j] ... w[i,s']⎥
|
|
|
+// ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
|
|
|
+// ⎣w[s,0] ... w[s,j] ... w[s,s']⎦ ... ⎣w[s,0] ... w[s,j] ... w[s,s']⎦ ... ⎣w[s,0] ... w[s,j] ... w[s,s']⎦
|
|
|
+// Where s = Sizes[l] - Neural network layer size
|
|
|
+// s' = Sizes[l-1] - Previous neural network layer size
|
|
|
+// L = len(Sizes) - Number of neural network layers
|
|
|
|
|
|
type NeuralNetwork struct {
|
|
|
Count int
|
|
@@ -139,6 +148,24 @@ func NewNeuralNetwork(sizes []int, nu float64, trainingCycles int) (nn *NeuralNe
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (nn *NeuralNetwork) Copy() (out *NeuralNetwork) {
|
|
|
+ out = &NeuralNetwork{}
|
|
|
+ out.Sizes = nn.Sizes
|
|
|
+ out.Count = nn.Count
|
|
|
+ out.Weights = make([]*mat.Dense, nn.Count)
|
|
|
+ out.Biases = make([]*mat.Dense, nn.Count)
|
|
|
+ out.A = make([]*mat.Dense, nn.Count)
|
|
|
+ out.Z = make([]*mat.Dense, nn.Count)
|
|
|
+ out.alpha = nn.alpha
|
|
|
+ out.trainingCycles = nn.trainingCycles
|
|
|
+
|
|
|
+ for i := 1; i < out.Count; i++ {
|
|
|
+ nn.Weights[i] = mat.DenseCopyOf(out.Weights[i])
|
|
|
+ nn.Biases[i] = mat.DenseCopyOf(out.Biases[i])
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func (nn *NeuralNetwork) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
|
|
|
r, _ := aIn.Dims()
|
|
|
if r != nn.Sizes[0] {
|
|
@@ -162,8 +189,8 @@ func (nn *NeuralNetwork) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
|
|
|
|
|
|
func (nn *NeuralNetwork) Teach(teacher teach.Teacher) {
|
|
|
for i := 0; i < nn.trainingCycles; i++ {
|
|
|
- for teacher.Next() {
|
|
|
- nn.backward(teacher.GetData(), teacher.GetExpect())
|
|
|
+ for teacher.NextData() {
|
|
|
+ nn.backward(teacher.GetData())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -317,3 +344,7 @@ func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
|
|
|
func (nn *NeuralNetwork) result() *mat.Dense {
|
|
|
return nn.A[nn.Count-1]
|
|
|
}
|
|
|
+
|
|
|
+func (nn *NeuralNetwork) TeachResilient(aIn, aOut mat.Matrix, nuP, nuM float64, deltaMin, deltaMax float64) {
|
|
|
+
|
|
|
+}
|