-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist.go
67 lines (58 loc) · 1.39 KB
/
persist.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
66
67
package deep
import (
"encoding/json"
)
// Dump is a neural network dump
type Dump struct {
Config *Config
Weights [][][]float64
}
// ApplyWeights sets the weights from a three-dimensional slice
func (n *Neural) ApplyWeights(weights [][][]float64) {
for i, l := range n.Layers {
for j := range l.Neurons {
for k := range l.Neurons[j].In {
n.Layers[i].Neurons[j].In[k].Weight = weights[i][j][k]
}
}
}
}
// Weights returns all weights in sequence
func (n Neural) Weights() [][][]float64 {
weights := make([][][]float64, len(n.Layers))
for i, l := range n.Layers {
weights[i] = make([][]float64, len(l.Neurons))
for j, n := range l.Neurons {
weights[i][j] = make([]float64, len(n.In))
for k, in := range n.In {
weights[i][j][k] = in.Weight
}
}
}
return weights
}
// Dump generates a network dump
func (n Neural) Dump() *Dump {
return &Dump{
Config: n.Config,
Weights: n.Weights(),
}
}
// FromDump restores a Neural from a dump
func FromDump(dump *Dump) *Neural {
n := NewNeural(dump.Config)
n.ApplyWeights(dump.Weights)
return n
}
// Marshal marshals to JSON from network
func (n Neural) Marshal() ([]byte, error) {
return json.Marshal(n.Dump())
}
// Unmarshal restores network from a JSON blob
func Unmarshal(bytes []byte) (*Neural, error) {
var dump Dump
if err := json.Unmarshal(bytes, &dump); err != nil {
return nil, err
}
return FromDump(&dump), nil
}