-
Notifications
You must be signed in to change notification settings - Fork 10
/
goslide.go
239 lines (190 loc) · 5.55 KB
/
goslide.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2020, The GoSLIDE 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 main
import (
"log"
"os"
"runtime/pprof"
"time"
"github.com/nlpodyssey/goslide/configuration"
"github.com/nlpodyssey/goslide/dataset"
"github.com/nlpodyssey/goslide/dataset/xcrepo"
"github.com/nlpodyssey/goslide/network"
"github.com/nlpodyssey/goslide/node"
)
var logger = log.New(os.Stderr, "", 0)
var globalTime time.Duration
func main() {
const cowId = 0
validateArguments()
loadGlobalConfiguration()
config := configuration.Global
if config.CpuProfile {
f, err := os.Create("cpu.prof")
if err != nil {
logger.Fatal(err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
logger.Fatal(err)
}
defer pprof.StopCPUProfile()
}
// Initialize Network
numBatches := config.TotRecords / config.BatchSize
numBatchesTest := config.TotRecordsTest / config.BatchSize
if config.LoadWeight {
/*
TODO: load weight...
cnpy::npz_t arr;
arr = cnpy::npz_load(Weights);
*/
}
startTime := time.Now()
myNet := network.New(
cowId,
config.NumLayer,
config.SizesOfLayers,
makeLayersTypes(config.NumLayer),
config.BatchSize,
config.LearningRate,
config.InputDim,
config.K,
config.L,
config.RangePow,
config.Sparsity,
)
endTime := time.Now()
logger.Println("Network Initialization takes", endTime.Sub(startTime))
// Start Training
for e := 0; e < config.Epoch; e++ {
logger.Println("Epoch", e)
trainSvmEpoch(cowId, numBatches, myNet, e)
// test
if e == config.Epoch-1 {
evaluateSvm(cowId, numBatchesTest, myNet, (e+1)*numBatches)
} else {
evaluateSvm(cowId, 50, myNet, (e+1)*numBatches)
}
}
if config.MemProfile {
f, err := os.Create("mem.prof")
if err != nil {
logger.Fatal(err)
}
defer f.Close()
// Do not call `runtime.GC()` so that we can inspect
// the complete current memory status
if err := pprof.WriteHeapProfile(f); err != nil {
logger.Fatal(err)
}
}
}
func makeLayersTypes(numLayers int) []node.NodeType {
layersTypes := make([]node.NodeType, numLayers)
for i := 0; i < numLayers-1; i++ {
layersTypes[i] = node.ReLU
}
layersTypes[numLayers-1] = node.Softmax
return layersTypes
}
func trainSvmEpoch(cowId, numBatches int, myNet *network.Network, epoch int) {
config := configuration.Global
file, err := os.Open(config.TrainData)
if err != nil {
logger.Fatal(err)
}
defer file.Close()
scanner := xcrepo.NewScanner(file)
if err := scanner.Err(); err != nil {
logger.Fatal(err)
}
examples := make([]dataset.Example, 0, config.BatchSize)
for i := 0; i < numBatches; i++ {
if i > 0 && (i+epoch*numBatches)%config.Stepsize == 0 {
evaluateSvm(cowId, 20, myNet, epoch*numBatches+i)
}
examples = examples[:0]
for count := 0; count < config.BatchSize && scanner.Scan(); count++ {
examples = append(examples, scanner.Example())
}
if err := scanner.Err(); err != nil {
logger.Fatalf("Error at line %d. %v", scanner.LineNumber(), err)
}
rehash := false
rebuild := false
if config.LayerMode == configuration.LayerMode1 || config.LayerMode == configuration.LayerMode4 {
if (epoch*numBatches+i)%(config.Rehash/config.BatchSize) == (config.Rehash/config.BatchSize - 1) {
rehash = true
}
// TODO: probably there was an error in the original code (using rehash at right)
if (epoch*numBatches+i)%(config.Rebuild/config.BatchSize) == (config.Rebuild/config.BatchSize - 1) {
rebuild = true
}
}
startTime := time.Now()
// logloss
_, myNet = myNet.ProcessInput(
cowId, examples, epoch*numBatches+i, rehash, rebuild)
endTime := time.Now()
globalTime += endTime.Sub(startTime)
}
}
func evaluateSvm(cowId, numBatchesTest int, myNet *network.Network, iter int) {
config := configuration.Global
totCorrect := 0
file, err := os.Open(config.TestData)
if err != nil {
logger.Fatal(err)
}
defer file.Close()
scanner := xcrepo.NewScanner(file)
if err := scanner.Err(); err != nil {
logger.Fatal(err)
}
examples := make([]dataset.Example, 0, config.BatchSize)
for i := 0; i < numBatchesTest; i++ {
numFeatures := 0
numLabels := 0
examples = examples[:0]
for count := 0; count < config.BatchSize && scanner.Scan(); count++ {
example := scanner.Example()
examples = append(examples, example)
numFeatures += len(example.Features)
numLabels += len(example.Labels)
}
if err := scanner.Err(); err != nil {
logger.Fatalf("Error at line %d. %v", scanner.LineNumber(), err)
}
logger.Println(config.BatchSize, "records, with", numFeatures,
"features and", numLabels, "labels")
var correctPredict int
// FIXME: reassignment of myNet for CoW problematic
correctPredict, myNet = myNet.PredictClass(cowId, examples)
totCorrect += correctPredict
logger.Println("Iter", i, "-",
float64(totCorrect)/(float64(config.BatchSize)*(float64(i)+1)),
"correct")
}
logger.Println("Over all:",
float64(totCorrect)/(float64(numBatchesTest)*float64(config.BatchSize)),
"correct")
logger.Println(iter, globalTime,
float64(totCorrect)/(float64(numBatchesTest)*float64(config.BatchSize)))
}
func validateArguments() {
if len(os.Args) != 2 {
logger.Println("Invalid or malformed arguments.")
logger.Fatal("\nUsage:\n goslide <json_configuration_file>\n")
}
}
func loadGlobalConfiguration() {
configFilename := os.Args[1]
config, err := configuration.FromJsonFile(configFilename)
if err != nil {
logger.Println("An error occurred reading the configuration file.")
logger.Fatal(err)
}
configuration.Global = config
}