-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathstorage.js
76 lines (49 loc) · 1.36 KB
/
storage.js
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
var binary = require('./util/file.js');
var network = require('./network.js');
class NetOnDisk {
static write(config) {
var contents = [];
contents.push(config.model.representation)
contents.push(config.pullWeights())
return binary.Writer.write(contents)
}
static read(buffer) {
var contents = binary.Reader.read(buffer)
var model = new network.Model(contents[0])
var config = model.newConfiguration()
config.putWeights(contents[1])
return config
}
static writeMultiPart(list) {
var contents = [];
contents.push(Object.keys(list))
for (var name in list) {
var config = list[name]
if (!config instanceof network.Configuration) {
throw 'config in list must be of type Network.Configuration'
}
contents.push(name)
contents.push(config.model.representation)
contents.push(config.pullWeights())
}
return binary.Writer.write(contents)
}
static readMultiPart(buffer) {
var contents = binary.Reader.read(buffer)
var ptr = -1
var names = contents[++ptr]
var list = {}
for (var i = 0; i < names.length; i++) {
var name = names[i]
if (contents[++ptr] !== name) {
throw 'name does not match up'
}
var model = new network.Model(contents[++ptr])
var config = model.newConfiguration()
config.putWeights(contents[++ptr])
list[name] = config
}
return list
}
}
module.exports = NetOnDisk