|
@@ -1,24 +1,28 @@
|
|
package neuralnetworkbase
|
|
package neuralnetworkbase
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ rand "math/rand"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
mat "gonum.org/v1/gonum/mat"
|
|
mat "gonum.org/v1/gonum/mat"
|
|
)
|
|
)
|
|
|
|
|
|
type NeuralNetwork struct {
|
|
type NeuralNetwork struct {
|
|
- Count int
|
|
|
|
- Sizes []int
|
|
|
|
- Biases []*mat.Dense
|
|
|
|
- Weights []*mat.Dense
|
|
|
|
- A []*mat.Dense
|
|
|
|
- Z []*mat.Dense
|
|
|
|
- alpha float64
|
|
|
|
|
|
+ Count int
|
|
|
|
+ Sizes []int
|
|
|
|
+ Biases []*mat.Dense
|
|
|
|
+ Weights []*mat.Dense
|
|
|
|
+ A []*mat.Dense
|
|
|
|
+ Z []*mat.Dense
|
|
|
|
+ alpha float64
|
|
|
|
+ trainingCycles int
|
|
}
|
|
}
|
|
|
|
|
|
func (nn *NeuralNetwork) Result() *mat.Dense {
|
|
func (nn *NeuralNetwork) Result() *mat.Dense {
|
|
return nn.A[nn.Count-1]
|
|
return nn.A[nn.Count-1]
|
|
}
|
|
}
|
|
|
|
|
|
-func NewNeuralNetwork(Sizes []int) (nn *NeuralNetwork) {
|
|
|
|
|
|
+func NewNeuralNetwork(Sizes []int, nu float64, trainingCycles int) (nn *NeuralNetwork) {
|
|
nn = &NeuralNetwork{}
|
|
nn = &NeuralNetwork{}
|
|
nn.Sizes = Sizes
|
|
nn.Sizes = Sizes
|
|
nn.Count = len(Sizes)
|
|
nn.Count = len(Sizes)
|
|
@@ -26,7 +30,8 @@ func NewNeuralNetwork(Sizes []int) (nn *NeuralNetwork) {
|
|
nn.Biases = make([]*mat.Dense, nn.Count)
|
|
nn.Biases = make([]*mat.Dense, nn.Count)
|
|
nn.A = make([]*mat.Dense, nn.Count)
|
|
nn.A = make([]*mat.Dense, nn.Count)
|
|
nn.Z = make([]*mat.Dense, nn.Count)
|
|
nn.Z = make([]*mat.Dense, nn.Count)
|
|
- nn.alpha = 0.1 / float64(nn.Sizes[0])
|
|
|
|
|
|
+ nn.alpha = nu / float64(nn.Sizes[0])
|
|
|
|
+ nn.trainingCycles = trainingCycles
|
|
|
|
|
|
for i := 1; i < nn.Count; i++ {
|
|
for i := 1; i < nn.Count; i++ {
|
|
nn.Weights[i] = generateRandomDense(nn.Sizes[i], nn.Sizes[i-1])
|
|
nn.Weights[i] = generateRandomDense(nn.Sizes[i], nn.Sizes[i-1])
|
|
@@ -50,6 +55,33 @@ func (nn *NeuralNetwork) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (nn *NeuralNetwork) Train(dataSet, expect []*mat.Dense) {
|
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
|
+ dataSetSize := len(dataSet)
|
|
|
|
+ // randomIndex := rand.Int() % dataSetSize
|
|
|
|
+ // fmt.Printf("Train: %v\n", randomIndex)
|
|
|
|
+ for i := 0; i < nn.trainingCycles; i++ {
|
|
|
|
+ for j := dataSetSize - 1; j >= 0; j -= 3 {
|
|
|
|
+ if j < 0 {
|
|
|
|
+ j = 0
|
|
|
|
+ }
|
|
|
|
+ nn.Backward(dataSet[j], expect[j])
|
|
|
|
+ }
|
|
|
|
+ // _, max := nn.Predict(dataSet[randomIndex])
|
|
|
|
+ // if 1.0-max < 0.2 {
|
|
|
|
+ // break
|
|
|
|
+ // }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (nn *NeuralNetwork) SaveState(filename string) {
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+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)
|
|
nn.A[0] = mat.DenseCopyOf(aIn)
|
|
|
|
|