iocommon.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Tatyana Borisova <tanusshhka@mail.ru>
  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 neuralnetwork
  26. import (
  27. "encoding/binary"
  28. "fmt"
  29. "io"
  30. mat "gonum.org/v1/gonum/mat"
  31. )
  32. func saveDense(writer io.Writer, matrix *mat.Dense) {
  33. buffer, _ := matrix.MarshalBinary()
  34. // Save 4-bytes little-endian size of Dense buffer
  35. bufferSize := make([]byte, 4)
  36. binary.LittleEndian.PutUint32(bufferSize, uint32(len(buffer)))
  37. writer.Write(bufferSize)
  38. _, err := writer.Write(buffer)
  39. check(err)
  40. // fmt.Printf("wrote array size %d count of bytes %d \n", len(buffer), bufferCount)
  41. printMatDense(matrix)
  42. }
  43. func printMatDense(matrix *mat.Dense) {
  44. // Print the result using the formatter.
  45. // fc := mat.Formatted(matrix, mat.Prefix(" "), mat.Squeeze())
  46. // fmt.Printf("c = %v \n\n", fc)
  47. }
  48. func readDense(reader io.Reader, matrix *mat.Dense) *mat.Dense {
  49. count := readInt(reader)
  50. // fmt.Printf("%d \n\n", count)
  51. matrix = &mat.Dense{}
  52. matrix.UnmarshalBinary(readByteArray(reader, count))
  53. printMatDense(matrix)
  54. return matrix
  55. }
  56. func readByteArray(reader io.Reader, size int) []byte {
  57. // Read an input array
  58. sizeBuffer := make([]byte, size)
  59. n1, err := reader.Read(sizeBuffer)
  60. check(err)
  61. fmt.Printf("readByteArray: size = %d \n", n1)
  62. return sizeBuffer
  63. }
  64. func readInt(reader io.Reader) int {
  65. // Reade int
  66. count := make([]byte, 4)
  67. _, err := reader.Read(count)
  68. check(err)
  69. return int(binary.LittleEndian.Uint32(count))
  70. }
  71. func check(e error) {
  72. if e != nil {
  73. panic(e)
  74. }
  75. }