diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp')
| -rw-r--r-- | Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp new file mode 100644 index 0000000..067a0d8 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt3/MLP/src/MLPConfig.cpp @@ -0,0 +1,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); +} |
