remotecontrol.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 remotecontrol
  26. import (
  27. context "context"
  28. fmt "fmt"
  29. "log"
  30. "net"
  31. "os"
  32. "sync"
  33. "time"
  34. "google.golang.org/grpc/codes"
  35. status "google.golang.org/grpc/status"
  36. neuralnetwork "../neuralnetwork"
  37. "gonum.org/v1/gonum/mat"
  38. grpc "google.golang.org/grpc"
  39. training "../training"
  40. )
  41. type RemoteControl struct {
  42. nn *neuralnetwork.NeuralNetwork
  43. activationsQueue chan *LayerMatrix
  44. biasesQueue chan *LayerMatrix
  45. weightsQueue chan *LayerMatrix
  46. stateQueue chan int
  47. mutex sync.Mutex
  48. }
  49. func (rw *RemoteControl) Init(nn *neuralnetwork.NeuralNetwork) {
  50. rw.nn = nn
  51. rw.activationsQueue = make(chan *LayerMatrix, 5)
  52. rw.biasesQueue = make(chan *LayerMatrix, 5)
  53. rw.weightsQueue = make(chan *LayerMatrix, 5)
  54. rw.stateQueue = make(chan int, 2)
  55. }
  56. func (rw *RemoteControl) UpdateActivations(l int, a *mat.Dense) {
  57. matrix := NewLayerMatrix(l, a, LayerMatrix_Activations)
  58. select {
  59. case rw.activationsQueue <- matrix:
  60. default:
  61. }
  62. }
  63. func (rw *RemoteControl) UpdateBiases(l int, biases *mat.Dense) {
  64. matrix := NewLayerMatrix(l, biases, LayerMatrix_Biases)
  65. select {
  66. case rw.biasesQueue <- matrix:
  67. default:
  68. }
  69. }
  70. func (rw *RemoteControl) UpdateWeights(l int, weights *mat.Dense) {
  71. matrix := NewLayerMatrix(l, weights, LayerMatrix_Weights)
  72. select {
  73. case rw.weightsQueue <- matrix:
  74. default:
  75. }
  76. }
  77. func (rw *RemoteControl) UpdateState(state int) {
  78. select {
  79. case rw.stateQueue <- state:
  80. default:
  81. }
  82. }
  83. func NewLayerMatrix(l int, dense *mat.Dense, contentType LayerMatrix_ContentType) (matrix *LayerMatrix) {
  84. buffer, err := dense.MarshalBinary()
  85. if err != nil {
  86. log.Fatalln("Invalid dense is provided for remote control")
  87. }
  88. matrix = &LayerMatrix{
  89. Matrix: &Matrix{
  90. Matrix: buffer,
  91. },
  92. Layer: int32(l),
  93. ContentType: contentType,
  94. }
  95. return
  96. }
  97. func (rw *RemoteControl) GetConfiguration(context.Context, *None) (*Configuration, error) {
  98. config := &Configuration{}
  99. for _, size := range rw.nn.Sizes {
  100. config.Sizes = append(config.Sizes, int32(size))
  101. }
  102. return config, nil
  103. }
  104. func (rw *RemoteControl) Activations(_ *None, srv RemoteControl_ActivationsServer) error {
  105. ctx := srv.Context()
  106. for {
  107. select {
  108. case <-ctx.Done():
  109. return ctx.Err()
  110. default:
  111. }
  112. msg := <-rw.activationsQueue
  113. srv.Send(msg)
  114. }
  115. }
  116. func (rw *RemoteControl) Biases(_ *None, srv RemoteControl_BiasesServer) error {
  117. ctx := srv.Context()
  118. for {
  119. select {
  120. case <-ctx.Done():
  121. return ctx.Err()
  122. default:
  123. }
  124. msg := <-rw.biasesQueue
  125. srv.Send(msg)
  126. }
  127. }
  128. func (rw *RemoteControl) Weights(_ *None, srv RemoteControl_WeightsServer) error {
  129. ctx := srv.Context()
  130. for {
  131. select {
  132. case <-ctx.Done():
  133. return ctx.Err()
  134. default:
  135. }
  136. msg := <-rw.weightsQueue
  137. srv.Send(msg)
  138. }
  139. }
  140. func (rw *RemoteControl) State(_ *None, srv RemoteControl_StateServer) error {
  141. ctx := srv.Context()
  142. for {
  143. select {
  144. case <-ctx.Done():
  145. return ctx.Err()
  146. default:
  147. }
  148. state := <-rw.stateQueue
  149. msg := &NetworkState{
  150. State: NetworkState_State(state),
  151. }
  152. fmt.Printf("Send state %v %v\n", msg, state)
  153. srv.Send(msg)
  154. }
  155. }
  156. func (rw *RemoteControl) Predict(context.Context, *Matrix) (*Matrix, error) {
  157. return nil, status.Error(codes.Unimplemented, "Not implemented")
  158. }
  159. func (rw *RemoteControl) DummyStart(context.Context, *None) (*None, error) {
  160. go func() {
  161. rw.mutex.Lock()
  162. defer rw.mutex.Unlock()
  163. // trainer := training.NewMNISTReader("./minst.data", "./mnist.labels")
  164. trainer := training.NewTextDataReader("wine.data", 5)
  165. rw.nn.Train(trainer, 500)
  166. // for i := 0; i < nn.Count; i++ {
  167. // if i > 0 {
  168. // fmt.Printf("Weights after:\n%v\n\n", mat.Formatted(nn.Weights[i], mat.Prefix(""), mat.Excerpt(0)))
  169. // fmt.Printf("Biases after:\n%v\n\n", mat.Formatted(nn.Biases[i], mat.Prefix(""), mat.Excerpt(0)))
  170. // fmt.Printf("Z after:\n%v\n\n", mat.Formatted(nn.Z[i], mat.Prefix(""), mat.Excerpt(0)))
  171. // }
  172. // fmt.Printf("A after:\n%v\n\n", mat.Formatted(nn.A[i], mat.Prefix(""), mat.Excerpt(0)))
  173. // }
  174. outFile, err := os.OpenFile("./data", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  175. if err != nil {
  176. log.Fatal(err)
  177. }
  178. defer outFile.Close()
  179. rw.nn.SaveState(outFile)
  180. outFile.Close()
  181. rw.UpdateState(neuralnetwork.StateLearning)
  182. defer rw.UpdateState(neuralnetwork.StateIdle)
  183. failCount := 0
  184. trainer.Reset()
  185. for trainer.NextValidator() {
  186. dataSet, expect := trainer.GetValidator()
  187. index, _ := rw.nn.Predict(dataSet)
  188. //TODO: remove this is not used for visualization
  189. time.Sleep(400 * time.Millisecond)
  190. if expect.At(index, 0) != 1.0 {
  191. failCount++
  192. // fmt.Printf("Fail: %v, %v\n\n", trainer.ValidationIndex(), expect.At(index, 0))
  193. }
  194. if !trainer.NextValidator() {
  195. fmt.Printf("Fail count: %v\n\n", failCount)
  196. failCount = 0
  197. trainer.Reset()
  198. }
  199. }
  200. fmt.Printf("Fail count: %v\n\n", failCount)
  201. failCount = 0
  202. trainer.Reset()
  203. rw.UpdateState(neuralnetwork.StateIdle)
  204. }()
  205. return &None{}, nil
  206. }
  207. func (rw *RemoteControl) Run() {
  208. grpcServer := grpc.NewServer()
  209. RegisterRemoteControlServer(grpcServer, rw)
  210. lis, err := net.Listen("tcp", "localhost:65001")
  211. if err != nil {
  212. fmt.Printf("Failed to listen: %v\n", err)
  213. }
  214. fmt.Printf("Listen localhost:65001\n")
  215. if err := grpcServer.Serve(lis); err != nil {
  216. fmt.Printf("Failed to serve: %v\n", err)
  217. }
  218. }