-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnnTrain.cpp
283 lines (230 loc) · 8.69 KB
/
rnnTrain.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
/*
* Recursive Neural Networks: neural networks for data structures
*
* Copyright (C) 2018 Alessandro Vullo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "General.h"
#include "require.h"
#include "Options.h"
#include "DataSet.h"
#include "Model.h"
//#include "RecursiveNN.h"
#include "Performance.h"
#include <cstdlib>
#include <cfloat>
#include <ctime>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
double adjustLearningRate(double curr_error, bool& restore_weights_flag, double& alpha) {
/*** Vogl adaptive acceleration learning method (additive) ***/
static double prev_eta = atof((Options::instance()->get_parameter("eta")).c_str());
static double prev_error = FLT_MAX;
double alpha_0 = 1e-1;
double phi = 1.05, beta = .5, epsilon = 1e-2;
double phi_additive = prev_eta/100;
double curr_eta;
if(curr_error < prev_error) {
curr_eta = phi_additive + prev_eta;
if(curr_eta > 1)
curr_eta = 1;
alpha = alpha_0;
prev_eta = curr_eta;
prev_error = curr_error;
} else if(curr_error < (1 + epsilon) * prev_error) {
curr_eta = beta * prev_eta;
alpha = 0;
prev_eta = curr_eta;
prev_error = curr_error;
} else {
restore_weights_flag = true;
curr_eta = beta * prev_eta;
alpha = 0;
}
return curr_eta;
}
void train(const string& netname, DataSet* trainingSet, DataSet* validationSet, ostream& os = cout) {
// Get important training parameters
bool onlinelearning = (atoi((Options::instance()->get_parameter("onlinelearning")).c_str()))?true:false;
// bool random_net = (atoi((Options::instance()->get_parameter("random_net")).c_str()))?true:false;
int epochs = atoi((Options::instance()->get_parameter("epochs")).c_str());
int savedelta = atoi((Options::instance()->get_parameter("savedelta")).c_str());
Model* model;
if(trainingSet->size()) {
os << "Creating new network...";
try {
model = Model::factory();
} catch(Model::BadModelCreation e) {
cerr << e.what() << endl;
exit(EXIT_FAILURE);
}
os << " Done." << endl;
} else {
os << "Need some data to train network, please specify value for the --training-set argument\n";
return;
}
os << endl << endl;
bool restore_weights_flag = false;
double curr_eta = atof((Options::instance()->get_parameter("eta")).c_str());
double alpha = .9;
double prev_error = FLT_MAX, min_error = FLT_MAX;
int min_error_epoch = -1;
double threshold_error = atof((Options::instance()->get_parameter("threshold_error")).c_str());
for(int epoch = 1; epoch<=epochs; epoch++) {
os << "Epoch " << epoch << '\t';
for(DataSet::iterator it=trainingSet->begin(); it!=trainingSet->end(); ++it) {
// (*it)->print(os);
model->propagateStructuredInput(*it);
model->backPropagateError(*it);
/* stochastic (i.e. online) gradient descent */
if(onlinelearning) {
if(restore_weights_flag)
model->restorePrevWeights();
model->adjustWeights(curr_eta, alpha);
//curr_eta = adjustLearningRate(curr_train_error, restore_weights_flag, alpha);
}
}
/* batch weight update */
if(!onlinelearning) {
if(restore_weights_flag)
model->restorePrevWeights();
model->adjustWeights(curr_eta, alpha);
//curr_eta = adjustLearningRate(curr_train_error, restore_weights_flag, alpha);
}
double error;
double error_training_set = model->computeError(trainingSet);
os << "E_training = " << error_training_set << '\t';
if(validationSet) {
double error_validation_set = model->computeError(validationSet);
error = error_validation_set;
os << "E_validation = " << error_validation_set;
} else
error = error_training_set;
os << endl;
if(min_error > error) {
min_error = error;
min_error_epoch = epoch;
model->saveParameters(netname.c_str());
}
// stopping criterion based on error threshold
if(fabs(prev_error - error) < threshold_error) {
os << endl << endl << "Network error decay below given threshold. Stopping training..." << endl;
break;
}
prev_error = error;
// save network every 'savedelta' epochs
if(!(epoch % savedelta)) {
ostringstream oss;
oss << netname << '.' << epoch;
model->saveParameters((oss.str()).c_str());
}
}
os << endl << flush;
// deallocate Recursive Neural Network instace
delete model; model = 0;
}
void predict(DataSet* dataset, const string& netname, const char* filename) {
Model* model;
try {
model = Model::factory(netname);
} catch(Model::BadModelCreation e) {
cerr << e.what() << endl;
exit(EXIT_FAILURE);
}
Performance* p = Performance::factory(Options::instance()->problem());
for(DataSet::iterator it=dataset->begin(); it!=dataset->end(); ++it) {
model->predict(*it);
p->update(*it);
}
ofstream os(filename);
assure(os, filename);
os << p;
// cout << endl << "***" << endl << rnn->computeError(dataset) << endl << "***" << endl;
delete p; p = 0;
delete model; model = 0;
}
int main(int argc, char* argv[]) {
setenv("RNNOPTIONTYPE", "train", 1);
DataSet *trainingSet = NULL, *testSet = NULL, *validationSet = NULL;
string netname;
try {
Options::instance()->parse_args(argc,argv);
netname = Options::instance()->get_parameter("netname");
if(!netname.length()) {
cerr << "Must specify a network file" << endl << endl;
throw Options::BadOptionSetting(Options::instance()->usage());
}
string training_set_fname = Options::instance()->get_parameter("training_set");
if(training_set_fname.length()) {
cout << "Creating training set. " << flush;
trainingSet = new DataSet(training_set_fname.c_str());
cout << "Done." << flush << endl;
} else
cout << "Training set not specified. Skipping training..." << endl;
string test_set_fname = Options::instance()->get_parameter("test_set");
if(test_set_fname.length()) {
cout << "Creating test set. " << flush;
testSet = new DataSet(test_set_fname.c_str());
cout << "Done." << flush << endl;
} else
cout << "Test set not specified. Skipping testing..." << endl;
string validation_set_fname = Options::instance()->get_parameter("validation_set");
if(validation_set_fname.length()) {
cout << "Creating validation set. " << flush;
validationSet = new DataSet(validation_set_fname.c_str());
cout << "Done." << flush << endl;
}
} catch(Options::BadOptionSetting e) {
cerr << e.what() << endl;
exit(EXIT_FAILURE);
}
/*** Train the network and save results ***/
if(trainingSet) {
cout << "Training set has " << (trainingSet->size()) << " instances." << endl;
if(validationSet) {
cout << "Training network with validation set." << endl;
cout << "Validation set has " << validationSet->size() << " instances." << endl;
}
else
cout << "Training without validation set." << endl;
train(netname, trainingSet, validationSet);
cout << "RNN model saved to file " << netname << endl;
predict(trainingSet, netname, "training.pred");
delete trainingSet;
if(validationSet) {
predict(validationSet, netname, "validation.pred");
delete validationSet;
}
} else if(validationSet) {
cerr << "Cannot use validation set without training set. Skipping training..." << endl;
delete validationSet;
}
if(testSet) {
cout << "Test set has " << testSet->size() << " instances." << endl
<< "Evaluating test set performance using network defined in file " << netname << endl;
predict(testSet, netname, "test.pred");
delete testSet;
}
return EXIT_SUCCESS;
}