remotecontrol.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "google.golang.org/grpc/codes"
  32. status "google.golang.org/grpc/status"
  33. neuralnetworkbase "../neuralnetworkbase"
  34. "gonum.org/v1/gonum/mat"
  35. grpc "google.golang.org/grpc"
  36. )
  37. type RemoteControl struct {
  38. nn *neuralnetworkbase.NeuralNetwork
  39. }
  40. func (rw *RemoteControl) Init(nn *neuralnetworkbase.NeuralNetwork) {
  41. rw.nn = nn
  42. }
  43. func (rw *RemoteControl) UpdateActivations(l int, a *mat.Dense) {
  44. // matrix := NewLayerMatrix(l, a, LayerMatrix_Activations)
  45. }
  46. func (rw *RemoteControl) UpdateBiases(l int, biases *mat.Dense) {
  47. // matrix := NewLayerMatrix(l, biases, LayerMatrix_Biases)
  48. }
  49. func (rw *RemoteControl) UpdateWeights(l int, weights *mat.Dense) {
  50. // matrix := NewLayerMatrix(l, weights, LayerMatrix_Weights)
  51. }
  52. func NewLayerMatrix(l int, dense *mat.Dense, contentType LayerMatrix_ContentType) (matrix *LayerMatrix) {
  53. buffer, err := dense.MarshalBinary()
  54. if err != nil {
  55. log.Fatalln("Invalid dense is provided for remote control")
  56. }
  57. matrix = &LayerMatrix{
  58. Matrix: &Matrix{
  59. Matrix: buffer,
  60. },
  61. Layer: int32(l),
  62. ContentType: contentType,
  63. }
  64. return
  65. }
  66. func (rw *RemoteControl) GetConfiguration(context.Context, *None) (*Configuration, error) {
  67. return nil, status.Error(codes.Unimplemented, "Not implemented")
  68. }
  69. func (rw *RemoteControl) Activations(*None, RemoteControl_ActivationsServer) error {
  70. return status.Error(codes.Unimplemented, "Not implemented")
  71. }
  72. func (rw *RemoteControl) Biases(*None, RemoteControl_BiasesServer) error {
  73. return status.Error(codes.Unimplemented, "Not implemented")
  74. }
  75. func (rw *RemoteControl) Weights(*None, RemoteControl_WeightsServer) error {
  76. return status.Error(codes.Unimplemented, "Not implemented")
  77. }
  78. func (rw *RemoteControl) Predict(context.Context, *Matrix) (*Matrix, error) {
  79. return nil, status.Error(codes.Unimplemented, "Not implemented")
  80. }
  81. func (rw *RemoteControl) Run() {
  82. grpcServer := grpc.NewServer()
  83. RegisterRemoteControlServer(grpcServer, rw)
  84. lis, err := net.Listen("tcp", "localhost:65001")
  85. if err != nil {
  86. fmt.Printf("Failed to listen: %v\n", err)
  87. }
  88. fmt.Printf("Listen localhost:65001\n")
  89. if err := grpcServer.Serve(lis); err != nil {
  90. fmt.Printf("Failed to serve: %v\n", err)
  91. }
  92. }