plusrpropgradient.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 gradients
  26. import (
  27. "math"
  28. neuralnetwork "git.semlanik.org/semlanik/NeuralNetwork/neuralnetwork"
  29. mat "gonum.org/v1/gonum/mat"
  30. )
  31. // Plus Resilient backpropagation
  32. type plusRPropGradient struct {
  33. gradientsPrev *mat.Dense
  34. gradients *mat.Dense
  35. deltas *mat.Dense
  36. batchSize int
  37. config RPropConfig
  38. }
  39. func NewPlusRPropInitializer(config RPropConfig) neuralnetwork.GradientDescentInitializer {
  40. return func(nn *neuralnetwork.NeuralNetwork, layer, gradientType int) interface{} {
  41. if gradientType == neuralnetwork.BiasGradient {
  42. return newRPropGradient(nn.Sizes[layer], 1, config)
  43. }
  44. return newRPropGradient(nn.Sizes[layer], nn.Sizes[layer-1], config)
  45. }
  46. }
  47. func newPlusRPropGradient(r, c int, config RPropConfig) (g *plusRPropGradient) {
  48. g = &plusRPropGradient{}
  49. deltas := make([]float64, r*c)
  50. for j, _ := range deltas {
  51. deltas[j] = 0.1
  52. }
  53. g.gradients = mat.NewDense(r, c, nil)
  54. g.gradientsPrev = mat.NewDense(r, c, nil)
  55. g.deltas = mat.NewDense(r, c, deltas)
  56. g.config = config
  57. return
  58. }
  59. func (g *plusRPropGradient) ApplyDelta(m mat.Matrix) (result *mat.Dense) {
  60. nuPlus := g.config.NuPlus
  61. nuMinus := g.config.NuMinus
  62. deltaMax := g.config.DeltaMax
  63. deltaMin := g.config.DeltaMin
  64. result = &mat.Dense{}
  65. gradient := g.gradients
  66. r, c := gradient.Dims()
  67. dividers := make([]float64, r*c)
  68. for i := range dividers {
  69. dividers[i] = float64(g.batchSize)
  70. }
  71. gradientDivider := mat.NewDense(r, c, dividers)
  72. gradient.DivElem(gradient, gradientDivider)
  73. result.Apply(func(i, j int, v float64) (outV float64) {
  74. gradientSign := g.gradientsPrev.At(i, j) * gradient.At(i, j)
  75. if gradientSign > 0 {
  76. g.deltas.Set(i, j, math.Min(nuPlus*g.deltas.At(i, j), deltaMax))
  77. outV = v - sign(gradient.At(i, j))*g.deltas.At(i, j)
  78. g.gradientsPrev.Set(i, j, gradient.At(i, j))
  79. } else if gradientSign < 0 {
  80. outV = v + sign(g.gradientsPrev.At(i, j))*g.deltas.At(i, j)
  81. g.deltas.Set(i, j, math.Max(nuMinus*g.deltas.At(i, j), deltaMin))
  82. g.gradientsPrev.Set(i, j, 0.0)
  83. } else {
  84. outV = v - sign(gradient.At(i, j))*g.deltas.At(i, j)
  85. g.gradientsPrev.Set(i, j, gradient.At(i, j))
  86. }
  87. return
  88. }, m)
  89. g.batchSize = 0
  90. g.gradients = mat.NewDense(r, c, nil)
  91. return result
  92. }
  93. func (g *plusRPropGradient) AccumGradients(gradient mat.Matrix) {
  94. g.gradients.Apply(func(i, j int, v float64) float64 {
  95. v += gradient.At(i, j)
  96. return v
  97. }, g.gradients)
  98. g.batchSize++
  99. }
  100. func (g plusRPropGradient) Gradients() *mat.Dense {
  101. return g.gradients
  102. }