neuralnetwork.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package neuralnetworkbase
  2. import (
  3. teach "../teach"
  4. mat "gonum.org/v1/gonum/mat"
  5. )
  6. // NeuralNetwork is simple neural network implementation
  7. //
  8. // Matrix: A
  9. // Description: A is set of calculated neuron activations after sigmoid correction
  10. // Format: 0 n N
  11. // ⎡A[0] ⎤ ... ⎡A[0] ⎤ ... ⎡A[0] ⎤
  12. // ⎢A[1] ⎥ ... ⎢A[1] ⎥ ... ⎢A[1] ⎥
  13. // ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
  14. // ⎢A[i] ⎥ ... ⎢A[i] ⎥ ... ⎢A[i] ⎥
  15. // ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
  16. // ⎣A[s] ⎦ ... ⎣A[s] ⎦ ... ⎣A[s] ⎦
  17. // Where s = Sizes[n], N = len(Sizes)
  18. //
  19. // Matrix: Z
  20. // Description: Z is set of calculated raw neuron activations
  21. // Format: 0 n N
  22. // ⎡Z[0] ⎤ ... ⎡Z[0] ⎤ ... ⎡Z[0] ⎤
  23. // ⎢Z[1] ⎥ ... ⎢Z[1] ⎥ ... ⎢Z[1] ⎥
  24. // ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
  25. // ⎢Z[i] ⎥ ... ⎢Z[i] ⎥ ... ⎢Z[i] ⎥
  26. // ⎢ ... ⎥ ... ⎢ ... ⎥ ... ⎢ ... ⎥
  27. // ⎣Z[s] ⎦ ... ⎣Z[s] ⎦ ... ⎣Z[s] ⎦
  28. // Where s = Sizes[n], N = len(Sizes)
  29. //
  30. // Matrix: Biases
  31. // Description: Biases is set of biases per layer except L0
  32. // Format:
  33. // ⎡b[0] ⎤
  34. // ⎢b[1] ⎥
  35. // ⎢ ... ⎥
  36. // ⎢b[i] ⎥
  37. // ⎢ ... ⎥
  38. // ⎣b[s] ⎦
  39. // Where s = Sizes[n]
  40. //
  41. // Matrix: Weights
  42. // Description: Weights is set of weights per layer except L0
  43. // Format:
  44. // ⎡w[0,0] ... w[0,j] ... w[0,s']⎤
  45. // ⎢w[1,0] ... w[1,j] ... w[1,s']⎥
  46. // ⎢ ... ⎥
  47. // ⎢w[i,0] ... w[i,j] ... w[i,s']⎥
  48. // ⎢ ... ⎥
  49. // ⎣w[s,0] ... w[s,j] ... w[s,s']⎦
  50. // Where s = Sizes[n], s' = Sizes[n-1]
  51. type NeuralNetwork struct {
  52. Count int
  53. Sizes []int
  54. Biases []*mat.Dense
  55. Weights []*mat.Dense
  56. A []*mat.Dense
  57. Z []*mat.Dense
  58. alpha float64
  59. trainingCycles int
  60. }
  61. func NewNeuralNetwork(Sizes []int, nu float64, trainingCycles int) (nn *NeuralNetwork) {
  62. nn = &NeuralNetwork{}
  63. nn.Sizes = Sizes
  64. nn.Count = len(Sizes)
  65. nn.Weights = make([]*mat.Dense, nn.Count)
  66. nn.Biases = make([]*mat.Dense, nn.Count)
  67. nn.A = make([]*mat.Dense, nn.Count)
  68. nn.Z = make([]*mat.Dense, nn.Count)
  69. nn.alpha = nu / float64(nn.Sizes[0])
  70. nn.trainingCycles = trainingCycles
  71. for i := 1; i < nn.Count; i++ {
  72. nn.Weights[i] = generateRandomDense(nn.Sizes[i], nn.Sizes[i-1])
  73. nn.Biases[i] = generateRandomDense(nn.Sizes[i], 1)
  74. }
  75. return
  76. }
  77. func (nn *NeuralNetwork) Predict(aIn mat.Matrix) (maxIndex int, max float64) {
  78. nn.forward(aIn)
  79. result := nn.result()
  80. r, _ := result.Dims()
  81. max = 0.0
  82. maxIndex = 0
  83. for i := 0; i < r; i++ {
  84. if result.At(i, 0) > max {
  85. max = result.At(i, 0)
  86. maxIndex = i
  87. }
  88. }
  89. return
  90. }
  91. func (nn *NeuralNetwork) Teach(teacher teach.Teacher) {
  92. for i := 0; i < nn.trainingCycles; i++ {
  93. for teacher.Next() {
  94. nn.backward(teacher.GetData(), teacher.GetExpect())
  95. }
  96. }
  97. }
  98. func (nn *NeuralNetwork) SaveState(filename string) {
  99. }
  100. func (nn *NeuralNetwork) LoadState(filename string) {
  101. }
  102. func (nn *NeuralNetwork) forward(aIn mat.Matrix) {
  103. nn.A[0] = mat.DenseCopyOf(aIn)
  104. for i := 1; i < nn.Count; i++ {
  105. nn.A[i] = mat.NewDense(nn.Sizes[i], 1, nil)
  106. aSrc := nn.A[i-1]
  107. aDst := nn.A[i]
  108. //Each iteration implements formula bellow for neuron activation values
  109. //A[l]=σ(W[l]*A[l−1]+B[l])
  110. //W[l]*A[l−1]
  111. aDst.Mul(nn.Weights[i], aSrc)
  112. //W[l]*A[l−1]+B[l]
  113. aDst.Add(aDst, nn.Biases[i])
  114. //Save raw activation value for back propagation
  115. nn.Z[i] = mat.DenseCopyOf(aDst)
  116. //σ(W[l]*A[l−1]+B[l])
  117. aDst.Apply(applySigmoid, aDst)
  118. }
  119. }
  120. func (nn *NeuralNetwork) backward(aIn, aOut mat.Matrix) {
  121. nn.forward(aIn)
  122. lastLayerNum := nn.Count - 1
  123. //To calculate new values of weights and biases
  124. //following formulas are used:
  125. //W[l] = A[l−1]*δ[l]
  126. //B[l] = δ[l]
  127. //For last layer δ value is calculated by following:
  128. //δ = (A[L]−y)⊙σ'(Z[L])
  129. //Calculate initial error for last layer L
  130. //error = A[L]-y
  131. //Where y is expected activations set
  132. err := &mat.Dense{}
  133. err.Sub(nn.result(), aOut)
  134. //Calculate sigmoids prime σ'(Z[L]) for last layer L
  135. sigmoidsPrime := &mat.Dense{}
  136. sigmoidsPrime.Apply(applySigmoidPrime, nn.Z[lastLayerNum])
  137. //(A[L]−y)⊙σ'(Z[L])
  138. delta := &mat.Dense{}
  139. delta.MulElem(err, sigmoidsPrime)
  140. //B[L] = δ[L]
  141. biases := mat.DenseCopyOf(delta)
  142. //W[L] = A[L−1]*δ[L]
  143. weights := &mat.Dense{}
  144. weights.Mul(delta, nn.A[lastLayerNum-1].T())
  145. //Initialize new weights and biases values with last layer values
  146. newBiases := []*mat.Dense{makeBackGradien(biases, nn.Biases[lastLayerNum], nn.alpha)}
  147. newWeights := []*mat.Dense{makeBackGradien(weights, nn.Weights[lastLayerNum], nn.alpha)}
  148. //Save calculated delta value temporary error variable
  149. err = delta
  150. //Next layer Weights and Biases are calculated using same formulas:
  151. //W[l] = A[l−1]*δ[l]
  152. //B[l] = δ[l]
  153. //But δ[l] is calculated using different formula:
  154. //δ[l] = ((Wt[l+1])*δ[l+1])⊙σ'(Z[l])
  155. //Where Wt[l+1] is transponded matrix of actual Weights from
  156. //forward step
  157. for l := nn.Count - 2; l > 0; l-- {
  158. //Calculate sigmoids prime σ'(Z[l]) for last layer l
  159. sigmoidsPrime := &mat.Dense{}
  160. sigmoidsPrime.Apply(applySigmoidPrime, nn.Z[l])
  161. //(Wt[l+1])*δ[l+1]
  162. //err bellow is delta from previous step(l+1)
  163. delta := &mat.Dense{}
  164. wdelta := &mat.Dense{}
  165. wdelta.Mul(nn.Weights[l+1].T(), err)
  166. //Calculate new delta and store it to temporary variable err
  167. //δ[l] = ((Wt[l+1])*δ[l+1])⊙σ'(Z[l])
  168. delta.MulElem(wdelta, sigmoidsPrime)
  169. err = delta
  170. //B[l] = δ[l]
  171. biases := mat.DenseCopyOf(delta)
  172. //W[l] = A[l−1]*δ[l]
  173. //At this point it's required to give explanation for inaccuracy
  174. //in the formula
  175. //Multiplying of activations matrix for layer l-1 and δ[l] is imposible
  176. //because view of matrices are following:
  177. // A[l-1] δ[l]
  178. // ⎡A[0] ⎤ ⎡δ[0] ⎤
  179. // ⎢A[1] ⎥ ⎢δ[1] ⎥
  180. // ⎢ ... ⎥ ⎢ ... ⎥
  181. // ⎢A[i] ⎥ X ⎢δ[i] ⎥
  182. // ⎢ ... ⎥ ⎢ ... ⎥
  183. // ⎣A[s'] ⎦ ⎣δ[s] ⎦
  184. //So we need to modify these matrices to apply mutiplications and got Weights matrix
  185. //of following view:
  186. // ⎡w[0,0] ... w[0,j] ... w[0,s']⎤
  187. // ⎢w[1,0] ... w[1,j] ... w[1,s']⎥
  188. // ⎢ ... ⎥
  189. // ⎢w[i,0] ... w[i,j] ... w[i,s']⎥
  190. // ⎢ ... ⎥
  191. // ⎣w[s,0] ... w[s,j] ... w[s,s']⎦
  192. //So we substitude matrices and transposes A[l-1] to get valid multiplication
  193. //if following view:
  194. // δ[l] A[l-1]
  195. // ⎡δ[0] ⎤ x [A[0] A[1] ... A[i] ... A[s']]
  196. // ⎢δ[1] ⎥
  197. // ⎢ ... ⎥
  198. // ⎢δ[i] ⎥
  199. // ⎢ ... ⎥
  200. // ⎣δ[s] ⎦
  201. weights := &mat.Dense{}
  202. weights.Mul(delta, nn.A[l-1].T())
  203. //!Prepend! new Biases and Weights
  204. // Scale down
  205. newBiases = append([]*mat.Dense{makeBackGradien(biases, nn.Biases[l], nn.alpha)}, newBiases...)
  206. newWeights = append([]*mat.Dense{makeBackGradien(weights, nn.Weights[l], nn.alpha)}, newWeights...)
  207. }
  208. newBiases = append([]*mat.Dense{&mat.Dense{}}, newBiases...)
  209. newWeights = append([]*mat.Dense{&mat.Dense{}}, newWeights...)
  210. nn.Biases = newBiases
  211. nn.Weights = newWeights
  212. }
  213. func (nn *NeuralNetwork) result() *mat.Dense {
  214. return nn.A[nn.Count-1]
  215. }