123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- /*
- * MIT License
- *
- * Copyright (c) 2019 Tatyana Borisova <tanusshhka@mail.ru>
- *
- * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- package neuralnetwork
- import (
- "encoding/binary"
- "fmt"
- "io"
- mat "gonum.org/v1/gonum/mat"
- )
- func saveDense(writer io.Writer, matrix *mat.Dense) {
- buffer, _ := matrix.MarshalBinary()
- // Save 4-bytes little-endian size of Dense buffer
- bufferSize := make([]byte, 4)
- binary.LittleEndian.PutUint32(bufferSize, uint32(len(buffer)))
- writer.Write(bufferSize)
- _, err := writer.Write(buffer)
- check(err)
- // fmt.Printf("wrote array size %d count of bytes %d \n", len(buffer), bufferCount)
- printMatDense(matrix)
- }
- func printMatDense(matrix *mat.Dense) {
- // Print the result using the formatter.
- // fc := mat.Formatted(matrix, mat.Prefix(" "), mat.Squeeze())
- // fmt.Printf("c = %v \n\n", fc)
- }
- func readDense(reader io.Reader, matrix *mat.Dense) *mat.Dense {
- count := readInt(reader)
- // fmt.Printf("%d \n\n", count)
- matrix = &mat.Dense{}
- matrix.UnmarshalBinary(readByteArray(reader, count))
- printMatDense(matrix)
- return matrix
- }
- func readByteArray(reader io.Reader, size int) []byte {
- // Read an input array
- sizeBuffer := make([]byte, size)
- n1, err := reader.Read(sizeBuffer)
- check(err)
- fmt.Printf("readByteArray: size = %d \n", n1)
- return sizeBuffer
- }
- func readInt(reader io.Reader) int {
- // Reade int
- count := make([]byte, 4)
- _, err := reader.Read(count)
- check(err)
- return int(binary.LittleEndian.Uint32(count))
- }
- func check(e error) {
- if e != nil {
- panic(e)
- }
- }
|