-
Notifications
You must be signed in to change notification settings - Fork 24
/
cpp.go
173 lines (170 loc) · 4.11 KB
/
cpp.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2019, The Emergent 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 weights
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"strconv"
"strings"
)
// NetReadCpp reads weights for entire network from old emergent C++ format
func NetReadCpp(r io.Reader) (*Network, error) {
nw := &Network{}
var (
lw *Layer
pw *Path
rw *Recv
ri int
pi int
skipnext bool
cidx int
err error
errlist []error
)
scan := bufio.NewScanner(r) // line at a time
for scan.Scan() {
if skipnext {
skipnext = false
continue
}
b := scan.Bytes()
bs := string(b)
switch {
case strings.HasPrefix(bs, "</"): // don't care about any ending tags
continue
case strings.HasPrefix(bs, "<Fmt "):
continue
case strings.HasPrefix(bs, "<Name "):
continue
case strings.HasPrefix(bs, "<Epoch "):
continue
case bs == "<Network>":
continue
case bs == "<Ug>":
continue
case bs == "<Un>":
skipnext = true // skip over bias weight
continue
case strings.HasPrefix(bs, "<Lay "):
lnm := strings.TrimSuffix(strings.TrimPrefix(bs, "<Lay "), ">")
nw.Layers = append(nw.Layers, Layer{Layer: lnm})
lw = &nw.Layers[len(nw.Layers)-1]
pw = nil
continue
case strings.HasPrefix(bs, "<UgUn "):
us := strings.TrimSuffix(strings.TrimPrefix(bs, "<UgUn "), ">")
uss := strings.Split(us, " ") // includes unit name
ri, err = strconv.Atoi(uss[0])
if err != nil {
errlist = append(errlist, err)
}
continue
case strings.HasPrefix(bs, "<Cg "):
cs := strings.TrimSuffix(strings.TrimPrefix(bs, "<Cg "), ">")
css := strings.Split(cs, " ")
pi, err = strconv.Atoi(css[0])
if err != nil {
errlist = append(errlist, err)
}
fm := strings.TrimPrefix(css[1], "From:")
if len(lw.Paths) < pi+1 {
lw.Paths = append(lw.Paths, Path{From: fm})
}
pw = &lw.Paths[pi]
continue
case strings.HasPrefix(bs, "<Cn "):
us := strings.TrimSuffix(strings.TrimPrefix(bs, "<Cn "), ">")
nc, err := strconv.Atoi(us)
if err != nil {
errlist = append(errlist, err)
}
if len(pw.Rs) < ri+1 {
pw.Rs = append(pw.Rs, Recv{Ri: ri, N: nc})
}
rw = &pw.Rs[ri]
if len(rw.Si) != nc {
rw.Si = make([]int, nc)
rw.Wt = make([]float32, nc)
rw.Wt1 = make([]float32, nc)
}
cidx = 0 // start reading on next ones
continue
case strings.HasPrefix(bs, "<"): // misc meta
kvl := strings.Split(bs, " ")
if len(kvl) != 2 {
err = fmt.Errorf("NetReadCpp: unrecognized input: %v", bs)
errlist = append(errlist, err)
log.Println(err)
continue
}
ky := strings.TrimPrefix(kvl[0], "<")
vl := strings.TrimSuffix(kvl[1], ">")
switch ky {
case "acts_m_avg":
ky = "ActMAvg"
case "acts_p_avg":
ky = "ActPAvg"
}
if lw == nil {
nw.SetMetaData(ky, vl)
} else if pw == nil {
lw.SetMetaData(ky, vl)
} else {
pw.SetMetaData(ky, vl)
}
continue
default: // weight values read into current rw
siwts := strings.Split(bs, " ")
switch len(siwts) {
case 2:
si, err := strconv.Atoi(siwts[0])
if err != nil {
errlist = append(errlist, err)
}
wt, err := strconv.ParseFloat(siwts[1], 32)
if err != nil {
errlist = append(errlist, err)
}
rw.Si[cidx] = si
rw.Wt[cidx] = float32(wt)
rw.Wt1[cidx] = float32(0)
cidx++
case 3:
si, err := strconv.Atoi(siwts[0])
if err != nil {
errlist = append(errlist, err)
}
wt, err := strconv.ParseFloat(siwts[1], 32)
if err != nil {
errlist = append(errlist, err)
}
scale, err := strconv.ParseFloat(siwts[2], 32)
if err != nil {
errlist = append(errlist, err)
}
rw.Si[cidx] = si
rw.Wt[cidx] = float32(wt)
rw.Wt1[cidx] = float32(scale)
cidx++
default:
err = fmt.Errorf("NetReadCpp: unrecognized input: %v", bs)
errlist = append(errlist, err)
log.Println(err)
continue
}
}
}
var eall error
if len(errlist) > 0 {
es := ""
for _, er := range errlist {
es = es + er.Error() + "\n"
}
eall = errors.New(es)
}
return nw, eall
}