123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- package neuralnetworkbase
- import (
- "errors"
- "fmt"
- "io"
- teach "../teach"
- mat "gonum.org/v1/gonum/mat"
- )
- type RProp struct {
- Count int
- Sizes []int
- Biases []*mat.Dense
- Weights []*mat.Dense
- A []*mat.Dense
- Z []*mat.Dense
- alpha float64
- trainingCycles int
- }
- func NewRProp(sizes []int, nu float64, trainingCycles int) (nn *RProp, err error) {
- err = nil
- if len(sizes) < 3 {
- fmt.Printf("Invalid network configuration: %v\n", sizes)
- return nil, errors.New("Invalid network configuration: %v\n")
- }
- for i := 0; i < len(sizes); i++ {
- if sizes[i] < 2 {
- fmt.Printf("Invalid network configuration: %v\n", sizes)
- return nil, errors.New("Invalid network configuration: %v\n")
- }
- }
- if nu <= 0.0 || nu > 1.0 {
- fmt.Printf("Invalid η value: %v\n", nu)
- return nil, errors.New("Invalid η value: %v\n")
- }
- if trainingCycles <= 0 {
- fmt.Printf("Invalid training cycles number: %v\n", trainingCycles)
- return nil, errors.New("Invalid training cycles number: %v\n")
- }
- if trainingCycles < 100 {
- fmt.Println("Training cycles number probably is too small")
- }
- nn = &RProp{}
- nn.Sizes = sizes
- nn.Count = len(sizes)
- nn.Weights = make([]*mat.Dense, nn.Count)
- nn.Biases = make([]*mat.Dense, nn.Count)
- nn.A = make([]*mat.Dense, nn.Count)
- nn.Z = make([]*mat.Dense, nn.Count)
- nn.alpha = nu / float64(nn.Sizes[0])
- nn.trainingCycles = trainingCycles
- for i := 1; i < nn.Count; i++ {
- nn.Weights[i] = generateRandomDense(nn.Sizes[i], nn.Sizes[i-1])
- nn.Biases[i] = generateRandomDense(nn.Sizes[i], 1)
- }
- return
- }
- func (nn *RProp) Copy() (out *RProp) {
- out = &RProp{}
- 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 *RProp) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
- r, _ := aIn.Dims()
- if r != nn.Sizes[0] {
- fmt.Printf("Invalid rows number of input matrix size: %v\n", r)
- return -1, 0.0
- }
- nn.forward(aIn)
- result := nn.result()
- r, _ = result.Dims()
- max = 0.0
- maxIndex = 0
- for i := 0; i < r; i++ {
- if result.At(i, 0) > max {
- max = result.At(i, 0)
- maxIndex = i
- }
- }
- return
- }
- func (nn *RProp) Teach(teacher teach.Teacher) {
- for i := 0; i < nn.trainingCycles; i++ {
- for teacher.NextData() {
- nn.backward(teacher.GetData())
- }
- }
- }
- func (nn *RProp) SaveState(writer io.Writer) {
- }
- func (nn *RProp) LoadState(reader io.Reader) {
- }
- func (nn *RProp) forward(aIn mat.Matrix) {
- nn.A[0] = mat.DenseCopyOf(aIn)
- for i := 1; i < nn.Count; i++ {
- nn.A[i] = mat.NewDense(nn.Sizes[i], 1, nil)
- 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)
- }
- }
- func (nn *RProp) backward(aIn, aOut mat.Matrix) {
- nn.forward(aIn)
- 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{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)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- weights := &mat.Dense{}
- weights.Mul(delta, nn.A[l-1].T())
-
- 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...)
- newWeights = append([]*mat.Dense{&mat.Dense{}}, newWeights...)
- nn.Biases = newBiases
- nn.Weights = newWeights
- }
- func (nn *RProp) result() *mat.Dense {
- return nn.A[nn.Count-1]
- }
|