-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnmodel.cpp
380 lines (322 loc) · 13 KB
/
nnmodel.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "nnmodel.h"
//#define DEBUG
NNModel::NNModel()
{
nAllData = 4;
nCalibrationSet = 4;
firstHiddenLayerLB = 0;
firstHiddenLayerTB = 0;
hiddenLayersLB = 0;
hiddenLayersTB = 0;
totalHiddenLayerLB = 0;
totalHiddenLayerTB = 0;
in_cnt = 0;
preBM = false;
}
bool NNModel::setFirstHiddenLayerRange(unsigned int lb, unsigned int tb)
{
if( tb <= lb )
return false;
firstHiddenLayerLB = lb;
firstHiddenLayerTB = tb;
return true;
}
bool NNModel::setOtherHiddenLayerRange(unsigned int lb, unsigned int tb)
{
if( tb <= lb )
return false;
totalHiddenLayerLB = lb;
totalHiddenLayerTB = tb;
return true;
}
bool NNModel::setHiddenLayersRange(unsigned int lb, unsigned int tb)
{
if( tb <= lb )
return false;
hiddenLayersLB = lb;
hiddenLayersTB = tb;
return true;
}
bool NNModel::checkParams()
{
if(firstHiddenLayerLB > 0 && firstHiddenLayerTB > 0 &&
hiddenLayersLB > 0 && hiddenLayersTB > 0 &&
totalHiddenLayerLB > 0 && totalHiddenLayerTB > 0 &&
in_cnt > 0){
return true;
}
return false;
}
bool NNModel::loadData(std::string dataFileName, std::string labelsFileName)
{
shark::Data<shark::RealVector> data;
shark::Data<shark::RealVector> labels;
try{
shark::importCSV(labels, labelsFileName, ' ', '#');
shark::importCSV(data, dataFileName, ' ', '#');
} catch (shark::Exception e){
std::cerr << "Exception!\n\t" << e.file() << "\t" << e.line() << "\t" << e.what() << std::endl;
std::cerr << "Unable to open file " << dataFileName << " and/or " << labelsFileName << ". Check paths!" << std::endl;
return false;
}
dataset = shark::LabeledData<shark::RealVector, shark::RealVector> (data, labels);
in_cnt = shark::dataDimension(dataset.inputs());
return true;
}
std::vector<std::vector<size_t> > NNModel::genMidLayers(unsigned beg, unsigned end, unsigned count)
{
vector<vector<size_t> > lst;
if(count == 1){
for(int i = beg; i <= end; i++){
vector<size_t> l;
l.push_back(i);
lst.push_back(l);
}
}
else{
vector<vector<size_t> > lst2 = genMidLayers(beg, end, count - 1);
for(int i = beg; i <= end; i++){
for(int j = 0; j < lst2.size(); j++){
vector<size_t> l = lst2[j];
l.push_back(i);
lst.push_back(l);
}
}
}
return lst;
}
int NNModel::push_back_s(std::vector<size_t> *changeable, const std::vector<size_t> &items)
{
std::vector<size_t>::const_iterator item;
for(item = items.begin(); item != items.end(); item++){
changeable->push_back(*item);
}
}
//#ifdef DEBUG
int NNModel::echoArch(vector<size_t> arr, std::string pre)
{
cerr << pre;
for(int ul = 0; ul < arr.size(); ul++) {
if(ul == arr.size() - 1)
cerr << arr[ul];
else
cerr << arr[ul] << "->";
}
cerr << std::endl;
}
//#endif
void NNModel::setPreBM(bool nw)
{
preBM = nw;
}
shark::BinaryRBM NNModel::trainRBM(
shark::UnlabeledData<RealVector> const& data,//the data to train with
std::size_t numHidden,//number of features in the AutoencoderModel
std::size_t iterations, //number of iterations to optimize
double regularisation,//strength of the regularisation
double learningRate // learning rate of steepest descent
)
{
//create rbm with simple binary units using the global random number generator
std::size_t inputs = dataDimension(data);
shark::BinaryRBM rbm(shark::Rng::globalRng);
rbm.setStructure(inputs,numHidden);
initRandomUniform(rbm,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));//initialize weights uniformly
//create derivative to optimize the rbm
//we want a simple vanilla CD-1.
shark::BinaryCD estimator(&rbm);
shark::TwoNormRegularizer regularizer;
//0.0 is the regularization strength. 0.0 means no regularization. choose as >= 0.0
estimator.setRegularizer(regularisation,®ularizer);
estimator.setK(1);//number of sampling steps
estimator.setData(data);//the data used for optimization
//create and configure optimizer
shark::SteepestDescent optimizer;
optimizer.setLearningRate(learningRate);//learning rate of the algorithm
//now we train the rbm and evaluate the mean negative log-likelihood at the end
unsigned int numIterations = iterations;//iterations for training
optimizer.init(estimator);
for(unsigned int iteration = 0; iteration != numIterations; ++iteration) {
optimizer.step(estimator);
}
rbm.setParameterVector(optimizer.solution().point);
return rbm;
}
// I'll have modify it for any arctitecture!
Network NNModel::unsupervisedPreTraining(
shark::UnlabeledData<RealVector> const& data,
vector<size_t> lrs,
double regularisation, std::size_t iterations, double learningRate
)
{
Network nw;
nw.setStructure(lrs, shark::FFNetStructures::Normal, true);
initRandomNormal(nw,0.1);
shark::UnlabeledData<RealVector> intermediateData = data;
vector<shark::BinaryRBM> pLayers;
for(int lr = 1; lr < lrs.size() - 1; lr++) {
//cerr << "pre-training " << lr << " layer of " << lrs.size() - 2 << " hidden" << std::endl;
// loop layers array with condition for first layer pre-training
shark::BinaryRBM l = trainRBM(
intermediateData, lrs[lr],
regularisation,iterations, learningRate
);
if(lr == 1){
//compute the mapping onto features of the first hidden layer
l.evaluationType(true,true);//we compute the direction visible->hidden and want the features and no samples
}
intermediateData=l(intermediateData);
pLayers.push_back(l);
}
for(int i = 0; i < pLayers.size() - 1; i++)
nw.setLayer(i, pLayers[i].weightMatrix(),pLayers[i].hiddenNeurons().bias());
//network = nw;
return nw;
}
int NNModel::evaluate()
{
if(!checkParams()){
return CHECKPARAMS;
}
#ifdef DEBUG
cerr << "DS: " << dataset.numberOfElements() << std::endl;
#endif
for(int fhl = firstHiddenLayerLB; fhl < firstHiddenLayerTB; fhl++){
for(int hlCount = hiddenLayersLB; hlCount < hiddenLayersTB; hlCount++){
if(hlCount > 0){
vector<vector<size_t> > hiddenMidLayers = genMidLayers(totalHiddenLayerLB, totalHiddenLayerTB, hlCount);
std::vector<std::vector<size_t> >::iterator l;
for(l = hiddenMidLayers.begin(); l != hiddenMidLayers.end(); l++){
std::vector<size_t> layers;
// Setting up architecture of NN
layers.push_back(in_cnt);
layers.push_back(fhl);
push_back_s(&layers, *l);
layers.push_back(1);
if(preBM)
network = unsupervisedPreTraining(dataset.inputs(), layers, 0.001, 1000, 0.1);
else
network.setStructure(layers, shark::FFNetStructures::Normal, true);
// All prepared
#ifdef DEBUG
echoArch(layers);
#endif
dataset.shuffle();
LabeledData<RealVector, RealVector> testSet, calibrationSet;
LabeledData<RealVector, RealVector> validationSet, trainingSet;
//RealVector MSE;
RealVector opt = network.parameterVector();
bool was = false;
double bestError = DBL_MAX;
for(int k = 1; k < nCalibrationSet; k++){
#ifdef DEBUG
cerr << "==============================" << k << "==============================" << std::endl;
#endif
calibrationSet = dataset;
testSet = shark::splitAtElement(calibrationSet, static_cast<size_t>((1.0 - 1.0/nAllData) * calibrationSet.numberOfElements()));
validationSet = shark::splitAtElement(calibrationSet, static_cast<size_t>((1.0 - 1.0/nCalibrationSet) * calibrationSet.numberOfElements()));
trainingSet = calibrationSet;
shark::initRandomUniform(network, -0.3, 0.3);
shark::IRpropPlus optimizer;
shark::ErrorFunction<RealVector, RealVector> error(trainingSet, &network, &loss);
optimizer.init(error);
int validationOffset = 0;
shark::Data<RealVector> prediction;
int bestIteration = 0;
const double MaxIterationsFromMinimum = 90;
for(int i = 0; i < trainingSet.numberOfElements(); i++){
optimizer.step(error);
prediction = network(validationSet.inputs());
double e = loss.eval(validationSet.labels(), prediction);
if(bestError >= e) {
bestIteration = 0;
bestError = e;
was = true;
opt = network.parameterVector();
}
if(bestIteration > MaxIterationsFromMinimum) {
#ifdef DEBUG
cerr << "Break max iteration after minimum.\n";
#endif
break;
}
bestIteration++;
#ifdef DEBUG
cerr << "Iteration " << i << " error " << e << " " << bestError << std::endl;
#endif
}
}
if(was)
network.setParameterVector(opt);
// Section of calculate model parametres. Wrap to other function.
// X is observed value
// Y is calculated value
//
// 1
// mse = n * SUM( X - Y )^2
double mse = 0; // Mean squared error
// SUM( X - Y )^2
// q^2 = 1.0 - SUM( X_aver - X)^2
double qsq = 0; // Coefficient of determination
double mvX = 0, mvY = 0; // Mid value of X and Y
vector<double> Ycal; // Observed array
// delta = SUM( Y - X )^2
double deltaSum = 0; // Sum of squared differ between calculated and observed. There is upper half of q^2
#ifdef DEBUG
cerr << "Y\tX\tdelta^2\n";
#endif
for(int i = 0; i < testSet.numberOfElements(); i++){
RealVector Y;
// getting i-ths observed value
double x = testSet.labels().element(i)[0];
// evaluate i-ths set to Y
network.eval(testSet.inputs().element(i), Y);
deltaSum += (Y(0) - x) * (Y(0) - x);
#ifdef DEBUG
cerr << Y(0) << '\t' << x << '\t' << (Y(0) - x)*(Y(0) - x) << std::endl;
#endif
mse += (Y(0) - x)*(Y(0) - x);
mvX += x;
mvY += Y(0);
Ycal.push_back(Y(0));
}
mse = mse / testSet.numberOfElements();
mvX /= testSet.numberOfElements(); // X average divides by N (there is for averaging. Lol)
mvY /= testSet.numberOfElements(); // Y average divides by N (there is for averaging. Lol)
// SUM( X - Xaver) * (Y - Yaver )
// corr = sqrt(SUM( Xaver - X )^2 * SUM( Yaver - Y )^2 )
double corr = 0;
// covXY = SUM( X - Xaver ) * (Y - Yaver )
double covXY = 0;
// SUM( Xaver - X )^2
double sX2 = 0;
// SUM( Yaver - Y )^2
double sY2 = 0;
for(int i = 0; i < testSet.numberOfElements(); i++){
double x = testSet.labels().element(i)[0];
covXY += (Ycal[i] - mvY)*(x - mvX);
sX2 += (x - mvX)*(x - mvX);
sY2 += (Ycal[i] - mvY)*(Ycal[i] - mvY);
}
corr = covXY / sqrt(sX2 * sY2);
qsq = 1.0 - deltaSum / sX2;
echoArch(layers);
#ifndef DEBUG
if(corr*corr > 0.5){
#endif
cerr << "MSE: " << mse << std::endl;
cerr << "Corr^2: " << corr*corr << std::endl;
cerr << "q^2: " << qsq << std::endl;
#ifndef DEBUG
}
#endif
calibrationSet.shuffle();
}
}
}
}
cerr << "Press Enter key ^_^\n";
std::cin.get();
return EVALNORM;
//*/
}