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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
* main.cpp
*
* Created on: 09.06.2011
* Author: sven
*/
#include "MLPConfig.h"
#include "MLP.h"
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <csignal>
namespace po = boost::program_options;
const char* MLP_XOR_CONFIGFILE = "Init_MLP.txt";
const char* MLP_PCA_TRAINFILE = "train_pca";
const char* MLP_PCA_TESTFILE = "test_pca";
const char* MLP_RAW_TRAINFILE = "train_raw";
const char* MLP_RAW_TESTFILE = "test_raw";
const char* MLP_PCA_CONFIGFILE = "mlp_pca_config.txt";
const char* MLP_RAW_CONFIGFILE = "mlp_raw_config.txt";
const uint32_t VALIDATION_DATA_RATIO = 20; /* every x th value will become a validition pattern */
const double EPSILON = 0.001;
void mlp_xor(MLPConfig& config);
void mlp_train_pca(MLPConfig& config);
void mlp_train_raw(MLPConfig& config);
void mlp_test_pca(MLPConfig& config);
void mlp_test_raw(MLPConfig& config);
void parseTrainData(const char*,Traindata&,Traindata&);
void signal_handler(int signal);
static MLP* mlpPtr = NULL;
void signal_handler(int signal)
{
std::cout << "Terminating..." << std::endl;
if (mlpPtr) {
mlpPtr->stop();
}
mlpPtr = NULL;
}
double toDouble(const std::string& t)
{
double res;
std::istringstream ss(t);
ss >> res;
return res;
}
void parseTrainData(const char* filename, Traindata& td, Traindata& vd)
{
typedef boost::char_separator<char> TrainSepChar;
typedef tokenizer<TrainSepChar> TrainTokenizer;
TrainSepChar sepCol(":");
TrainSepChar sepSpace(" ");
ifstream inFile(filename);
if (!inFile.is_open()) {
std::cerr << "error opening data file " << filename << std::endl;
exit(1);
}
std::string line;
uint32_t counter = 0;
while(inFile.good())
{
getline(inFile,line);
if (line.empty()) {
continue;
}
TrainTokenizer lineTok(line,sepCol);
std::vector<std::string> lineToks;
BOOST_FOREACH(string t, lineTok)
{
lineToks.push_back(t);
}
Trainingpair tp;
TrainTokenizer targetTok(lineToks.at(0),sepSpace);
BOOST_FOREACH(string t,targetTok)
{
tp.mTarget.push_back(toDouble(t));
}
TrainTokenizer patternTok(lineToks.at(1),sepSpace);
BOOST_FOREACH(string t,patternTok)
{
if (t.find(";") != std::string::npos) {
continue;
}
tp.mPattern.push_back(toDouble(t));
}
if ( (counter % VALIDATION_DATA_RATIO) != 0 ) {
td.push_back(tp);
} else {
vd.push_back(tp);
}
counter++;
}
inFile.close();
}
void mlp_train_pca(MLPConfig& config)
{
config.setNumInputNeurons(90U);
config.setNumHiddenNeurons(90U);
config.setNumOutputNeurons(2U);
config.initWeights(true);
MLP mlp(config);
mlpPtr = &mlp;
Traindata td;
Traindata vd;
parseTrainData(MLP_PCA_TRAINFILE,td,vd);
mlp.train(td,vd,config.getNumTrainingCycles(),MLP_PCA_CONFIGFILE);
}
void mlp_test_pca(MLPConfig& config)
{
config.parseConfigfile(MLP_PCA_CONFIGFILE);
if (!config.isValid()) {
std::cerr << "error parsing config file " << MLP_PCA_CONFIGFILE << std::endl;
exit(1);
}
MLP mlp(config);
Traindata td;
Output output;
parseTrainData(MLP_PCA_TESTFILE,td,td);
uint32_t c;
uint32_t num = 1;
uint32_t errorCount = 0;
double out,target;
double res[2];
BOOST_FOREACH(Trainingpair tp, td) {
output.clear();
mlp.propagate(tp.mPattern,output);
bool errorFound = false;
for (c = 0; c < 2; c++) {
out = output.at(c);
if (out >= 0.5) {
res[c] = 1.0;
} else {
res[c] = 0.0;
}
}
for (c = 0; c < 2; c++) {
target = tp.mTarget.at(c);
if (std::abs(res[c] - target) > EPSILON) {
cout << num << " PCA Testerror: " << res[c] << " != " << target << endl;
errorFound = true;
} else {
cout << num << " PCA OK: " << res[c] << " == " << target << endl;
}
}
if (errorFound) {
errorCount++;
}
num++;
}
cout << "PCA: " << errorCount << " errors in " << td.size() << " testpatterns" << endl;
}
void mlp_train_raw(MLPConfig& config)
{
config.setNumInputNeurons(6U*151U);
config.setNumHiddenNeurons(6U*151U);
config.setNumOutputNeurons(2U);
config.initWeights(true);
MLP mlp(config);
mlpPtr = &mlp;
Traindata td;
Traindata vd;
parseTrainData(MLP_RAW_TRAINFILE,td,vd);
mlp.train(td,vd,config.getNumTrainingCycles(),MLP_RAW_CONFIGFILE);
}
void mlp_test_raw(MLPConfig& config)
{
config.parseConfigfile(MLP_RAW_CONFIGFILE);
if (!config.isValid()) {
std::cerr << "error parsing config file " << MLP_RAW_CONFIGFILE << std::endl;
exit(1);
}
MLP mlp(config);
Traindata td;
Output output;
parseTrainData(MLP_RAW_TESTFILE,td,td);
uint32_t c;
uint32_t num = 1;
uint32_t errorCount = 0;
double out,target;
double res[2];
BOOST_FOREACH(Trainingpair tp, td) {
output.clear();
mlp.propagate(tp.mPattern,output);
bool errorFound = false;
for (c = 0; c < 2; c++) {
out = output.at(c);
if (out >= 0.5) {
res[c] = 1.0;
} else {
res[c] = 0.0;
}
}
for (c = 0; c < 2; c++) {
target = tp.mTarget.at(c);
if (std::abs(res[c] - target) > EPSILON) {
cout << num << " RAW Testerror: " << res[c] << " != " << target << endl;
errorFound = true;
} else {
cout << num << " RAW OK: " << res[c] << " == " << target << endl;
}
}
if (errorFound) {
errorCount++;
}
num++;
}
cout << "RAW: " << errorCount << " errors in " << td.size() << " testpatterns" << endl;
}
void mlp_xor(MLPConfig& config) {
MLP mlp(config);
uint8_t numPatterns = 4;
Traindata td;
const double patterns[][2] = {
{0.0 , 0.0},
{0.0 , 1.0},
{1.0 , 0.0},
{1.0 , 1.0}
};
const double targets[][1] = {
{0},
{1},
{1},
{0}
};
for (uint8_t i = 0; i < numPatterns ; i++) {
Trainingpair tp;
tp.mPattern.assign(patterns[i],patterns[i]+2);
tp.mTarget.assign(targets[i],targets[i]+1);
td.push_back(tp);
}
mlp.train(td,td,config.getNumTrainingCycles(),"mlp_xor_weights.txt");
// test with other data (Aufgabe 3)
int32_t i,j;
int32_t minval = -40;
int32_t maxval = 50;
double x,y;
Output result;
Pattern test_pat;
ofstream csvFile;
const char* sep = " ";
csvFile.open("mlp_xor_out.dat",ios::out);
for (i = minval ; i < maxval ; i++)
{
x = ((double) i) / 10.0;
for (j = minval ; j < maxval ; j++) {
y = ((double) j) / 10.0;
test_pat.clear();
test_pat.push_back(x);
test_pat.push_back(y);
result.clear();
mlp.propagate(test_pat,result);
csvFile << x << sep << y;
BOOST_FOREACH(double z,result)
{
csvFile << sep << z;
}
csvFile << endl;
}
csvFile << endl;
}
csvFile.close();
system("gnuplot -persist mlp_xor.plt");
}
int main(int argc, char* argv[])
{
signal(SIGINT,&signal_handler);
MLPConfig config;
enum APP {
APP_XOR,APP_TRAIN_RAW,APP_TRAIN_PCA,APP_TEST_RAW,APP_TEST_PCA
};
// default settings
config.setLernrate(0.8);
config.setUpdateMode(MLPConfig::UPDATE_MODE_BATCH);
config.setMomentum(0.9);
config.setErrorThreshold(0.01);
config.setConfigAcceptanceErrorThreshold(0.1);
config.setNumTrainingCycles(20000U);
APP app = APP_XOR;
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("trainingcycles", po::value<uint32_t>(), "set number of training cycles. default 20000")
("lernrate", po::value<double>(), "set lernrate. default 0.8")
("momentum", po::value<double>(), "set momentum. default 0.9")
("maxerror", po::value<double>(), "set maximum error that stops training. default 0.01")
("acceptanceerror", po::value<double>(), "set error after which the configuration is accepted if error increases. default 0.1")
("updatemode", po::value<std::string>(), "set update mode <\"batch\"|\"single\">. default batch")
("app", po::value<std::string>(), "application <\"xor\"|\"trainraw\"|\"trainpca\"|\"testraw\"|\"testpca\">. default xor")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
if (vm.count("trainingcycles")) {
uint32_t trainingcycles = vm["trainingcycles"].as<uint32_t>();
config.setNumTrainingCycles(trainingcycles);
}
if (vm.count("lernrate")) {
double lernrate = vm["lernrate"].as<double>();
config.setLernrate(lernrate);
}
if (vm.count("momentum")) {
double momentum = vm["momentum"].as<double>();
config.setMomentum(momentum);
}
if (vm.count("maxerror")) {
double maxerror = vm["maxerror"].as<double>();
config.setErrorThreshold(maxerror);
}
if (vm.count("momentum")) {
double acceptanceerror = vm["acceptanceerror"].as<double>();
config.setConfigAcceptanceErrorThreshold(acceptanceerror);
}
if (vm.count("updatemode")) {
std::string updatemode = vm["updatemode"].as<std::string>();
if (updatemode.find("batch") != std::string::npos) {
config.setUpdateMode(MLPConfig::UPDATE_MODE_BATCH);
} else if(updatemode.find("single") != std::string::npos) {
config.setUpdateMode(MLPConfig::UPDATE_MODE_SINGLE);
}
}
if (vm.count("app")) {
std::string appOpt = vm["app"].as<std::string>();
if (appOpt.find("xor") != std::string::npos) {
app = APP_XOR;
} else if (appOpt.find("trainraw") != std::string::npos) {
app = APP_TRAIN_RAW;
} else if (appOpt.find("trainpca") != std::string::npos) {
app = APP_TRAIN_PCA;
} else if (appOpt.find("testraw") != std::string::npos) {
app = APP_TEST_RAW;
} else if (appOpt.find("testpca") != std::string::npos) {
app = APP_TEST_PCA;
}
}
}
catch(std::exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
catch(...) {
cerr << "Exception of unknown type!\n";
}
switch(app) {
case APP_XOR:
config.parseConfigfile(MLP_XOR_CONFIGFILE);
if (!config.isValid()) {
std::cerr << "error parsing config file " << MLP_XOR_CONFIGFILE << std::endl;
exit(1);
}
mlp_xor(config);
break;
case APP_TRAIN_PCA:
mlp_train_pca(config);
break;
case APP_TRAIN_RAW:
mlp_train_raw(config);
break;
case APP_TEST_PCA:
mlp_test_pca(config);
break;
case APP_TEST_RAW:
mlp_test_raw(config);
break;
default:
break;
}
return 0;
}
|