mtinvnet.cpp
Go to the documentation of this file.00001 #include "NeuralNetwork.h"
00002 #include <iostream>
00003 #include <vector>
00004 #include <string>
00005 #include <algorithm>
00006 #include <iterator>
00007 #include <cmath>
00008 #include <fstream>
00009 #include <boost/progress.hpp>
00010 #include "UniformRNG.h"
00011 #include "ApplyFilter.h"
00012 #include "C1DMTSynthData.h"
00013
00014 using namespace std;
00015 using namespace gplib;
00016
00017 const int frequenzen = 10;
00018 const float T[frequenzen] =
00019 { 1.06667, 2.15554, 4.33332, 8.66701, 16.000, 32.000, 64.0, 127.992, 256.016,
00020 512.033 };
00021
00022 int main()
00023 {
00024 UniformRNG Random;
00025 NeuralNetwork::ttypeVector typeVector;
00026 NeuralNetwork::ttypeArray typeArray;
00027 C1DMTSynthData Synthetic;
00028 trealdata frequency;
00029
00030 const int inlength = frequenzen;
00031 for (int i = 0; i < frequenzen; ++i)
00032 frequency.push_back(1. / T[i]);
00033 Synthetic.SetFrequencies(frequency);
00034
00035 const double maxinit = 0.5;
00036 const int maxit = 500;
00037 const int predit = 10;
00038 const int hlayers = 2;
00039 const int hlsize = 50;
00040 const int modellayers = 2;
00041 const int modelparms = 2;
00042 const double mu = 0.1;
00043 const double alpha = 0.1;
00044 gplib::rvec input(inlength), desired(modelparms), output(modelparms);
00045 vector<double> trainingerror;
00046 const double minres = 1.0;
00047 const double minthick = 5.0;
00048 const double maxthick = 15.0;
00049 const double noiselevel = 0.02;
00050 trealdata resistivities(modellayers), thickness(modellayers);
00051
00052 typeVector.assign(hlsize, SigmoidalNeuron::bipolar);
00053 for (int i = 0; i < hlayers; ++i)
00054 {
00055 typeArray.push_back(typeVector);
00056 }
00057 typeVector.assign(modelparms, SigmoidalNeuron::bipolar);
00058 typeArray.push_back(typeVector);
00059
00060 NeuralNetwork Network(inlength, modelparms, mu, typeArray, maxinit);
00061 Network.SetAlpha(alpha);
00062 boost::progress_display progressbar(maxit);
00063 for (int iteration = 0; iteration < maxit; ++iteration)
00064 {
00065 for (int i = 0; i < modellayers; ++i)
00066 {
00067 resistivities.at(i) = std::pow(10.0, minres + Random.GetNumber(0, 2));
00068 thickness.at(i) = minthick;
00069 }
00070 Synthetic.SetResistivities(resistivities);
00071 Synthetic.SetThicknesses(thickness);
00072 Synthetic.GetData();
00073
00074 for (int i = 0; i < inlength; ++i)
00075 {
00076 double rho = Synthetic.GetMTData().at(i).GetRhoxy();
00077
00078 input(i) = log10(rho) - 2.0;
00079
00080 }
00081
00082
00083 for (int i = 0; i < modellayers; ++i)
00084 {
00085 desired(i) = log10(resistivities.at(i)) - 2.0;
00086
00087
00088 }
00089
00090 Network.CalcOutput(input, output);
00091 Network.AdaptFilter(input, desired);
00092
00093
00094
00095
00096
00097
00098
00099 trainingerror.push_back(inner_prod(Network.GetEpsilon(),
00100 Network.GetEpsilon()));
00101 ++progressbar;
00102 }
00103 ofstream terrfile("train.err");
00104 copy(trainingerror.begin(), trainingerror.end(), ostream_iterator<double> (
00105 terrfile, "\n"));
00106 cout << endl << endl;
00107 cout << "Finished training !" << endl;
00108 for (int iteration = 0; iteration < predit; ++iteration)
00109 {
00110 for (int i = 0; i < modellayers; ++i)
00111 {
00112 resistivities.at(i) = std::pow(10.0, minres + Random.GetNumber(0, 2));
00113 thickness.at(i) = minthick;
00114 }
00115 Synthetic.SetResistivities(resistivities);
00116 Synthetic.SetThicknesses(thickness);
00117 Synthetic.GetData();
00118 for (int i = 0; i < inlength; ++i)
00119 {
00120 input(i) = log10(Synthetic.GetMTData().at(i).GetRhoxy()) - 2.0;
00121 }
00122 for (int i = 0; i < modellayers; ++i)
00123 {
00124 desired(i) = log10(resistivities.at(i)) - 2.0;
00125
00126 }
00127
00128 Network.CalcOutput(input, output);
00129 ++progressbar;
00130 cout << "Model: ";
00131 copy(desired.begin(), desired.end(), ostream_iterator<double> (cout,
00132 " "));
00133 cout << endl;
00134 cout << "Output: ";
00135 copy(output.begin(), output.end(), ostream_iterator<double> (cout, " "));
00136 cout << endl;
00137 }
00138
00139 cout << endl << flush;
00140 Synthetic.WriteAsMtt("in");
00141 for (int i = 0; i < modellayers; ++i)
00142 {
00143 resistivities.at(i) = pow(10.0, output(i) + 2.0);
00144 thickness.at(i) = minthick;
00145 }
00146
00147 Synthetic.SetResistivities(resistivities);
00148 Synthetic.SetThicknesses(thickness);
00149 Synthetic.GetData();
00150 Synthetic.WriteAsMtt("out");
00151 Network.PrintTopology("test.dot");
00152
00153 }