-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
payload.go
65 lines (53 loc) · 1.5 KB
/
payload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2019 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nn
import (
"bytes"
"encoding/binary"
mat "github.com/nlpodyssey/spago/pkg/mat32"
)
// Payload contains the support data used for example by the optimization methods
type Payload struct {
Label int
Data []mat.Matrix
}
// NewPayload returns an empty support structure, not connected to any optimization method.
func NewPayload() *Payload {
return &Payload{
Label: 0, // important set the label to zero
Data: make([]mat.Matrix, 0),
}
}
// MarshalBinary encodes the Payload into binary form.
func (p Payload) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
binLabel := make([]byte, 8)
binary.LittleEndian.PutUint64(binLabel, uint64(p.Label))
buf.Write(binLabel)
binLen := make([]byte, 4)
binary.LittleEndian.PutUint32(binLen, uint32(len(p.Data)))
buf.Write(binLen)
for _, m := range p.Data {
err := mat.MarshalBinaryMatrix(m, buf)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// UnmarshalBinary decodes a Payload from binary form.
func (p *Payload) UnmarshalBinary(data []byte) error {
p.Label = int(binary.LittleEndian.Uint64(data))
dataLen := int(binary.LittleEndian.Uint32(data[8:]))
var err error
r := bytes.NewReader(data[12:])
p.Data = make([]mat.Matrix, dataLen)
for i := range p.Data {
p.Data[i], err = mat.UnmarshalBinaryMatrix(r)
if err != nil {
return err
}
}
return nil
}