-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.cpp
319 lines (262 loc) · 7.42 KB
/
Train.cpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "Model.h"
#include "DataSet.h"
#include <cassert>
#include <cmath>
#include <cfloat>
#include <iostream>
using namespace std;
class Options {
public:
Float epsilon;
int NF;
int NB;
int seed;
int shuffle;
int batch_blocks;
int readModel;
int readEpoch;
int nEpochs;
int adaptive;
int reload;
Float belief;
Float attenuation;
void write(std::ostream& os = cout) {
os << "epsilon " << epsilon << endl;
os << "NF " << NF << endl;
os << "NB " << NB << endl;
os << "seed " << seed << endl;
os << "shuffle " << shuffle << endl;
os << "batch_blocks " << batch_blocks << endl;
os << "readModel " << readModel << endl;
os << "readEpoch " << readEpoch << endl;
os << "nEpochs " << nEpochs << endl;
os << "adaptive " << adaptive << endl;
os << "reload " << reload << endl;
os << "belief " << belief << endl;
os << "attenuation " << attenuation << endl;
}
Options(std::istream& is) {
char str[128];
while(is) {
is >> str;
if(!strcmp(str, "epsilon")) is >> epsilon;
else if(!strcmp(str, "NF")) is >> NF;
else if(!strcmp(str, "NB")) is >> NB;
else if(!strcmp(str, "seed")) is >> seed;
else if(!strcmp(str, "shuffle")) is >> shuffle;
else if(!strcmp(str, "batch_blocks")) is >> batch_blocks;
else if(!strcmp(str, "readModel")) is >> readModel;
else if(!strcmp(str, "readEpoch")) is >> readEpoch;
else if(!strcmp(str, "nEpochs")) is >> nEpochs;
else if(!strcmp(str, "adaptive")) is >> adaptive;
else if(!strcmp(str, "reload")) is >> reload;
else if(!strcmp(str, "belief")) is >> belief;
else if(!strcmp(str, "attenuation")) is >> attenuation;
}
}
};
class Performance {
int NY;
int** _cf; // confusion matrix
string msg;
public:
Performance(int nclasses, const string& message = ""): NY(nclasses), msg(message) {
_cf = new int*[NY];
for(int y=0; y<NY; ++y)
_cf[y] = new int[NY];
}
~Performance() {
for(int y=0; y<NY; ++y)
delete[] _cf[y];
delete[] _cf;
}
void update(Instance* instance) {
for(int t=1; t<=instance->length; ++t) {
assert(instance->y[t] != -1 && instance->y_pred[t] != -1);
_cf[instance->y_pred[t]][instance->y[t]]++;
}
}
void reset() {
for(int y1=0; y1<NY; ++y1)
for(int y2=0; y2<NY; ++y2)
_cf[y1][y2] = 0;
}
int getNumInstancesOfClass(int c) const {
assert(c >= 0 && c < NY);
int count = 0;
for(int y=0; y<NY; ++y)
count += _cf[y][c];
return count;
}
int getNumErrorsForClass(int c) const {
assert(c >= 0 && c < NY);
int count = 0;
for(int y=0; y<NY; ++y)
if(y != c)
count += _cf[y][c];
return count;
}
int getTotalNumErrors() const {
int count = 0;
for(int y1=0; y1<NY; ++y1)
for(int y2=0; y2!=NY; ++y2)
if(y1 != y2)
count += _cf[y1][y2];
return count;
}
friend ostream& operator<<(ostream& os, const Performance& p) {
double a[128];
double all = 0;
for(int y=0; y<p.NY; ++y) {
a[y] = p.getNumInstancesOfClass(y);
all += a[y];
}
int tot_class_errors = p.getTotalNumErrors();
os << endl << endl << p.msg << "_NErrors= " << tot_class_errors << "/" << all;
os << " " << (double)tot_class_errors/(double)(all) * 100.0;
for(int y=0; y<p.NY; ++y) {
int tot_errors_per_class = p.getNumErrorsForClass(y);
os << endl << "Class" << y <<"= " << tot_errors_per_class << "/" << a[y];
os << "\t" << (double)tot_errors_per_class/(double)a[y] * 100.0;
}
}
};
int Errcomp=100000;
void save(int epoch, Model* M) {
filebuf outbuf;
char fname[1024];
sprintf(fname, "trained-%d.model", epoch);
if(outbuf.open(fname, ios::out) != 0) {
ostream os(&outbuf);
M->write(os);
} else {
FAULT("Failed to write to file " << fname);
}
outbuf.close();
}
void load(int epoch, Model* M) {
filebuf inbuf;
char fname[1024];
sprintf(fname, "trained-%d.model", epoch);
if(inbuf.open(fname, ios::in) != 0) {
istream is(&inbuf);
M->read(is);
} else {
FAULT("Failed to read file " << fname);
}
inbuf.close();
}
void evaluate(Model* M, DataSet& D, char* which) {
cout << endl << " counting_" << which << "_errors" << flush;
Performance perf(M->getClasses(), which);
perf.reset();
for(int p=0; p<D.size(); ++p) {
M->predict(D[p]);
perf.update(D[p]);
if(p%20==0) cout << "." << flush;
}
cout << perf;
int tot_errors = perf.getTotalNumErrors();
if((strncmp(which, "test", 4)==0) && tot_errors < Errcomp) {
save(-10,M);
Errcomp = tot_errors;
}
cout << endl;
}
void train(Model* M, DataSet& D, DataSet& T, Options& Opt) {
int Gui = Opt.adaptive;
//Number of steps at increasing error before
//rescaling the learning rate.
int gui=0;
/*
Float ep=Opt.epsilon/(Float)D.totSize;
Float ep0=ep;
if (Opt.batch_blocks>1) {
ep *= (Float)(Opt.batch_blocks-1);
}
cout << "Actual lrate= " << ep << "\n";
M->setEpsilon(ep);
*/
cout << "Start learning" << endl;
srand(Opt.seed);
Float previous_error = FLT_MAX;
for(int epoch=Opt.readEpoch+1; epoch<=Opt.readEpoch + Opt.nEpochs; ++epoch) {
if(Opt.shuffle)
D.shuffle();
M->resetError();
int batch_cnt = 0;
for(int pp=0; pp<D.size(); ++pp) {
M->e_step(D[pp]);
batch_cnt++;
if(batch_cnt >= D.size()/Opt.batch_blocks && D.size()-pp >= D.size()/Opt.batch_blocks) {
M->m_step(Opt.attenuation, Opt.belief);
batch_cnt = 0;
}
if(pp%20 == 0) cout << "." << flush;
}
if(batch_cnt > 0)
M->m_step(Opt.attenuation, Opt.belief);
Float current_error = M->getError();
cout << "\nEpoch " << epoch << " Error= " << current_error;
save(0,M);
if (current_error < previous_error) {
gui=0;
save(0,M);
if (Gui>0) {
// ep += ep0*0.01;
// M->setEpsilon(ep);
}
previous_error = current_error;
}
if (epoch && epoch%5==0) {
save(epoch, M);
evaluate(M, D, "train");
evaluate(M, T, "test");
D.write("train-predictions");
T.write("test-predictions");
}
cout << endl << flush;
}
}
int main(int argc, char** argv) {
if (argc<2) {
cerr << "Usage: " << argv[0] << " option-file\n";
exit(1);
}
ifstream optstream(argv[1]);
Options Opt(optstream);
Opt.write();
// TODO
// training/test sets file names should be given as arguments,
// consider default as well
cout << "Reading train dataset" << endl << flush;
ifstream dstream("train.dataset");
DataSet trainingSet(dstream);
// trainingSet.set_belief(Opt.belief);
cout << "Reading test dataset " << endl << flush;
ifstream tstream("test.dataset");
DataSet testSet(tstream);
// testSet.set_belief(Opt.belief);
int inputDim = trainingSet.getInputDim();
int outputDim = trainingSet.getOutputDim();
assert(inputDim == testSet.getInputDim());
assert(outputDim == testSet.getOutputDim());
Model* M;
if (Opt.readModel) {
char tmp[1024];
sprintf(tmp, "trained-%d.model", Opt.readEpoch);
cout << "Reading model from " << tmp << "\n";
ifstream mstream(tmp);
M = new Model(mstream);
} else {
cout << "Creating model" << endl << flush;
M = new Model(inputDim, outputDim, Opt.NF, Opt.NB);
cout << "Generating random parameters" << endl << flush;
M->randomize(Opt.seed);
save(-1, M);
Opt.readEpoch = 0;
}
train(M, trainingSet, testSet, Opt);
delete M;
return 0;
}