gradients.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. type RPropGradient struct {
  31. GradientsPrev *mat.Dense
  32. Gradients *mat.Dense
  33. Deltas *mat.Dense
  34. batchSize int
  35. }
  36. func NewRPropGradient(r, c int) (g *RPropGradient) {
  37. g = &RPropGradient{}
  38. deltas := make([]float64, r*c)
  39. for j, _ := range deltas {
  40. deltas[j] = 0.1
  41. }
  42. g.Gradients = mat.NewDense(r, c, nil)
  43. g.GradientsPrev = mat.NewDense(r, c, nil)
  44. g.Deltas = mat.NewDense(r, c, deltas)
  45. return
  46. }
  47. func (g *RPropGradient) ApplyDelta(m mat.Matrix, _ mat.Matrix) (result *mat.Dense) {
  48. //TODO: move this hardcoded parameters to separate config for gradient
  49. nuPlus := 1.2
  50. nuMinus := 0.5
  51. deltaMax := 50.0
  52. deltaMin := 0.000001
  53. result = &mat.Dense{}
  54. gradient := g.Gradients
  55. r, c := gradient.Dims()
  56. dividers := make([]float64, r*c)
  57. for i := range dividers {
  58. dividers[i] = float64(g.batchSize)
  59. }
  60. gradientDivider := mat.NewDense(r, c, dividers)
  61. gradient.DivElem(gradient, gradientDivider)
  62. result.Apply(func(i, j int, v float64) (outV float64) {
  63. gradientSign := g.GradientsPrev.At(i, j) * gradient.At(i, j)
  64. if gradientSign > 0 {
  65. g.Deltas.Set(i, j, math.Min(nuPlus*g.Deltas.At(i, j), deltaMax))
  66. outV = v - sign(gradient.At(i, j))*g.Deltas.At(i, j)
  67. g.GradientsPrev.Set(i, j, gradient.At(i, j))
  68. } else if gradientSign < 0 {
  69. outV = v + sign(g.GradientsPrev.At(i, j))*g.Deltas.At(i, j)
  70. g.Deltas.Set(i, j, math.Max(nuMinus*g.Deltas.At(i, j), deltaMin))
  71. g.GradientsPrev.Set(i, j, 0.0)
  72. } else {
  73. outV = v - sign(gradient.At(i, j))*g.Deltas.At(i, j)
  74. g.GradientsPrev.Set(i, j, gradient.At(i, j))
  75. }
  76. return
  77. }, m)
  78. g.batchSize = 0
  79. return result
  80. }
  81. func (g *RPropGradient) AccumGradients(gradient mat.Matrix) {
  82. g.Gradients.Apply(func(i, j int, v float64) float64 {
  83. v += gradient.At(i, j)
  84. return v
  85. }, g.Gradients)
  86. g.batchSize++
  87. }
  88. //Simple backpropagation with constant value η
  89. type BackPropGradient struct {
  90. alpha float64
  91. }
  92. func (g *BackPropGradient) ApplyDelta(m mat.Matrix, gradient mat.Matrix) (result *mat.Dense) {
  93. // Gradient change of actual matrix using:
  94. // m[l]′ = m[l] − η * ∂C/∂m
  95. // Where ∂E/∂m is `in` matrix
  96. scaled := &mat.Dense{}
  97. result = &mat.Dense{}
  98. // η * ∂E/∂m
  99. scaled.Scale(g.alpha, gradient)
  100. // m[l] − η * ∂E/∂m
  101. result.Sub(m, scaled)
  102. return result
  103. }
  104. func (g *BackPropGradient) AccumGradients(gradient mat.Matrix) {
  105. }