summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp
blob: 067a0d882a9b8b3382dda5973501e4be052b3191 (plain)
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
/*
 * MLPConfig.cpp
 *
 *  Created on: 09.06.2011
 *      Author: sven
 */

#include "MLPConfig.h"

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <time.h>

typedef tokenizer<boost::char_separator<char> > MLP_Tokenizer;
const uint32_t DEFAULT_NUMCYCLES = 20000U;
const double DEFAULT_ERROR_THRESHOLD = 0.01;
const double DEFAULT_CONFIGACCEPTANCE_THRESHOLD = 0.3;

MLPConfig::MLPConfig()
:mNumInputNeurons(0),mNumHiddenNeurons(0),mNumOutputNeurons(0),mValid(false)
,mErrorThreshold(DEFAULT_ERROR_THRESHOLD),mNumTrainingCycles(DEFAULT_NUMCYCLES)
,mConfigAcceptanceErrorThreshold(DEFAULT_CONFIGACCEPTANCE_THRESHOLD)
{

}
MLPConfig::MLPConfig(const char* configFileName)
:mNumInputNeurons(0),mNumHiddenNeurons(0),mNumOutputNeurons(0),mValid(false)
,mErrorThreshold(DEFAULT_ERROR_THRESHOLD),mNumTrainingCycles(DEFAULT_NUMCYCLES)
{
	parseConfigfile(configFileName);
}

MLPConfig::~MLPConfig() {
	// TODO Auto-generated destructor stub
}
bool MLPConfig::parseConfigfile(ifstream& configFile)
{
	string line;
	while( configFile.good() )
	{
		getline(configFile,line);
		if( !parseLine(line) )
		{
			return false;
		}
	}
	return true;
}

bool MLPConfig::parseLine(string& line)
{
	vector<string> tokens;
	char_separator<char> sep(":");
	MLP_Tokenizer tok(line,sep);
	MLP_Tokenizer::iterator it;
	BOOST_FOREACH(string t, tok)
	{
		tokens.push_back(t);
	}
	if (tokens.empty()) {
		return true;
	}
	if (tokens.at(0).find("Bias") != string::npos) {
		return true;
	}
	if (tokens.at(0).find("Threshold") != string::npos) {
		return true;
	}
	if (tokens.at(0).find("Input -> Hidden") != string::npos) {
		return true;
	}
	if (tokens.at(0).find("Hidden -> Output") != string::npos) {
		return true;
	}
	if (tokens.at(0).find("Input") != string::npos) {
		MLP_Tokenizer inpTok(tokens.at(1));
		for (it = inpTok.begin() ;it != inpTok.end() ; ++it) {
			mNumInputNeurons++;
		}
		return true;
	}
	if (tokens.at(0).find("Hidden") != string::npos) {
		MLP_Tokenizer hiddenTok(tokens.at(1));
		for (it = hiddenTok.begin() ;it != hiddenTok.end() ; ++it) {
			mNumHiddenNeurons++;
		}
		return true;
	}
	if (tokens.at(0).find("Output") != string::npos) {
		MLP_Tokenizer outputTok(tokens.at(1));
		for (it = outputTok.begin() ;it != outputTok.end() ; ++it) {
			mNumOutputNeurons++;
		}
		return true;
	}
	if (mWeights.empty()) {
		initWeights(false);
	}
	char_separator<char> weightSep(" ");
	MLP_Tokenizer weightTok(tokens.at(0),weightSep);
	it = weightTok.begin();
	uint32_t from = toUint32(*it);
	it++;
	uint32_t to = toUint32(*it);
	it++;
	double w = toDouble(*it);
	mWeights.at(from).at(to) = w;
	return true;
}

void MLPConfig::initWeights(bool randomWeights)
{
	uint32_t i,j;
	uint32_t numNeurons = 1 /*Bias*/ + mNumInputNeurons + mNumHiddenNeurons + mNumOutputNeurons;
	for (i = 0; i< numNeurons; i++) {
		mWeights.push_back(vector<double>(numNeurons));
	}
	double randWeight = 0.0;
	if (randomWeights) {
		srand(time(NULL));
		uint32_t startHidden = 1 + mNumInputNeurons;
		uint32_t startOutput = startHidden + mNumHiddenNeurons;
		uint32_t noNeuron = startOutput + mNumOutputNeurons;
		// Bias -> Hidden
		for (i = startHidden; i < startOutput; i++) {
			randWeight = getRandomWeight();
			if ( (randWeight < -0.5) || (randWeight > 0.5) ) {
				cout << randWeight << endl;
			}
			mWeights.at(0).at(i) = randWeight;
		}
		// Bias -> Output
		for (i = startOutput; i < noNeuron; i++) {
			randWeight = getRandomWeight();
			if ( (randWeight < -0.5) || (randWeight > 0.5) ) {
				cout << randWeight << endl;
			}
			mWeights.at(0).at(i) = randWeight;
		}
		// Input -> Hidden
		for (i=1; i < startHidden; i++) {
			for (j=startHidden ; j<startOutput; j++) {
				randWeight = getRandomWeight();
				if ( (randWeight < -0.5) || (randWeight > 0.5) ) {
					cout << randWeight << endl;
				}
				mWeights.at(i).at(j) = randWeight;
			}
		}
		// Hidden -> Output
		for (i=startHidden; i < startOutput; i++) {
			for (j=startOutput ; j<noNeuron; j++) {
				randWeight = getRandomWeight();
				if ( (randWeight < -0.5) || (randWeight > 0.5) ) {
					cout << randWeight << endl;
				}
				mWeights.at(i).at(j) = randWeight;
			}
		}
	}
}

uint32_t MLPConfig::toUint32(const string& t)
{
	uint32_t res;
	istringstream ss(t);
	ss >> res;
	return res;
}

double MLPConfig::toDouble(const string& t)
{
	double res;
	istringstream ss(t);
	ss >> res;
	return res;
}

void MLPConfig::dump()
{
	int width = 12;
	int prec = 8;
	cout << "============ MLP config==========" << endl;
    cout << "Lernrate: " << mLernrate << endl;
    cout << "Momentum: " << mMomentum << endl;
    cout << "Updatemode: " << (mUpdateMode==UPDATE_MODE_BATCH?"batch":"single") << endl;
	cout << "Input neurons: " << mNumInputNeurons << endl;
	cout << "Hidden neurons: " << mNumHiddenNeurons << endl;
	cout << "Output neurons: " << mNumOutputNeurons << endl;

	cout << "Weights:" << endl;
	BOOST_FOREACH(vector<double> i, mWeights)
	{
		BOOST_FOREACH(double w, i)
		{
			cout << setw(width) << setprecision(prec)  << w;
		}
		cout << endl;
	}
	cout << "=================================" << endl;
}
bool MLPConfig::parseConfigfile(const char* configfilename)
{
	ifstream inFile(configfilename);
	if (!inFile.is_open()) {
		mValid = false;
		return mValid;
	}
	mValid = parseConfigfile(inFile);
	inFile.close();
	return mValid;
}
double MLPConfig::getRandomWeight()
{
	int prec = 10000;
	int x = rand() % prec;
	x -= prec / 2;
	return ((double) x) / ((double) prec);
}