rpropgradient.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package neuralnetworkbase
  26. import (
  27. "math"
  28. mat "gonum.org/v1/gonum/mat"
  29. )
  30. // Resilient backpropagation
  31. type rPropGradient struct {
  32. GradientsPrev *mat.Dense
  33. Gradients *mat.Dense
  34. Deltas *mat.Dense
  35. batchSize int
  36. config RPropConfig
  37. }
  38. type RPropConfig struct {
  39. NuPlus float64
  40. NuMinus float64
  41. DeltaMax float64
  42. DeltaMin float64
  43. }
  44. func NewRPropInitializer(config RPropConfig) GradientDescentInitializer {
  45. return func(nn *NeuralNetwork, layer, gradientType int) interface{} {
  46. if gradientType == BiasGradient {
  47. return newRPropGradient(nn.Sizes[layer], 1, config)
  48. }
  49. return newRPropGradient(nn.Sizes[layer], nn.Sizes[layer-1], config)
  50. }
  51. }
  52. func newRPropGradient(r, c int, config RPropConfig) (g *rPropGradient) {
  53. g = &rPropGradient{}
  54. deltas := make([]float64, r*c)
  55. for j, _ := range deltas {
  56. deltas[j] = 0.1
  57. }
  58. g.Gradients = mat.NewDense(r, c, nil)
  59. g.GradientsPrev = mat.NewDense(r, c, nil)
  60. g.Deltas = mat.NewDense(r, c, deltas)
  61. g.config = config
  62. return
  63. }
  64. func (g *rPropGradient) ApplyDelta(m mat.Matrix) (result *mat.Dense) {
  65. nuPlus := g.config.NuPlus
  66. nuMinus := g.config.NuMinus
  67. deltaMax := g.config.DeltaMax
  68. deltaMin := g.config.DeltaMin
  69. result = &mat.Dense{}
  70. gradient := g.Gradients
  71. r, c := gradient.Dims()
  72. dividers := make([]float64, r*c)
  73. for i := range dividers {
  74. dividers[i] = float64(g.batchSize)
  75. }
  76. gradientDivider := mat.NewDense(r, c, dividers)
  77. gradient.DivElem(gradient, gradientDivider)
  78. result.Apply(func(i, j int, v float64) (outV float64) {
  79. gradientSign := g.GradientsPrev.At(i, j) * gradient.At(i, j)
  80. if gradientSign > 0 {
  81. g.Deltas.Set(i, j, math.Min(nuPlus*g.Deltas.At(i, j), deltaMax))
  82. outV = v - sign(gradient.At(i, j))*g.Deltas.At(i, j)
  83. g.GradientsPrev.Set(i, j, gradient.At(i, j))
  84. } else if gradientSign < 0 {
  85. outV = v
  86. g.Deltas.Set(i, j, math.Max(nuMinus*g.Deltas.At(i, j), deltaMin))
  87. g.GradientsPrev.Set(i, j, 0.0)
  88. } else {
  89. outV = v - sign(gradient.At(i, j))*g.Deltas.At(i, j)
  90. g.GradientsPrev.Set(i, j, gradient.At(i, j))
  91. }
  92. return
  93. }, m)
  94. g.batchSize = 0
  95. return result
  96. }
  97. func (g *rPropGradient) AccumGradients(gradient mat.Matrix) {
  98. g.Gradients.Apply(func(i, j int, v float64) float64 {
  99. v += gradient.At(i, j)
  100. return v
  101. }, g.Gradients)
  102. g.batchSize++
  103. }