From da797691ff24de4c5bcde1acdf7088383e99be75 Mon Sep 17 00:00:00 2001 From: Muhammad Asjad Date: Mon, 7 Mar 2016 12:16:20 +1100 Subject: [PATCH] Added files via upload --- MLP.cpp | 1143 +++++++++++++++ data1.txt | 406 ++++++ data2.txt | 4193 +++++++++++++++++++++++++++++++++++++++++++++++++++++ data3.txt | 367 +++++ data4.txt | 1934 ++++++++++++++++++++++++ data5.txt | 363 +++++ data6.txt | 373 +++++ 7 files changed, 8779 insertions(+) create mode 100644 MLP.cpp create mode 100644 data1.txt create mode 100644 data2.txt create mode 100644 data3.txt create mode 100644 data4.txt create mode 100644 data5.txt create mode 100644 data6.txt diff --git a/MLP.cpp b/MLP.cpp new file mode 100644 index 0000000..3173290 --- /dev/null +++ b/MLP.cpp @@ -0,0 +1,1143 @@ +*************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 50; // Max neurons in any layer +const int MAXPATS = 5000; // Max training patterns + +// mlp paramaters +long NumIts ; // Max training iterations +int NumHN ; // Number of hidden layers +int NumHN1 ; // Number of neurons in hidden layer 1 +int NumHN2 ; // Number of neurons in hidden layer 2 +int NumHN3 ; // Number of neurons in hidden layer 3 +int NumHN4 ; // Number of neurons in hidden layer 4 +float LrnRate; // Learning rate +float Mtm1 ; // Momentum(t-1) +float Mtm2 ; // Momentum(t-2) +float ObjErr ; // Objective error + +// mlp weights +float **w1,**w11,**w111;// 1st layer wts +float **w2,**w22,**w222;// 2nd layer wts +float **w3,**w33,**w333;// 3rd layer wts +float **w4,**w44,**w444;// 4rth layer wts +float **w5,**w55,**w555;// 5th layer wts + + +void TrainNet2(float **x,float **d,int NumIPs,int NumOPs,int NumPats, int Ordering); +void TrainNet3(float **x,float **d,int NumIPs,int NumOPs,int NumPats); +void TrainNet4(float **x,float **d,int NumIPs,int NumOPs,int NumPats); +void TrainNet5(float **x,float **d,int NumIPs,int NumOPs,int NumPats); + +void TestNet2(float **x,float **d,int NumIPs,int NumOPs,int NumPats); +void TestNet3(float **x,float **d,int NumIPs,int NumOPs,int NumPats); + +float **Aloc2DAry(int m,int n); +void Free2DAry(float **Ary2D,int n); +long random_gen(long max); + +int main(){ + ifstream fin; + int i,j,NumIPs,NumOPs,NumTrnPats,NumTstPats,Ordering; + + char Line[500],Tmp[20],FName[20]; + + cout<<"Enter data filename: "; + cin>>FName; cin.ignore(); + fin.open(FName); + if(!fin.good()){cout<<"File not found!\n";exit(1);} + + + //read data specs... + do{fin.getline(Line,500);}while(Line[0]==';'); //eat comments + sscanf(Line,"%s%d",Tmp,&NumIPs); + fin>>Tmp>>NumOPs; //Number of outputs + fin>>Tmp>>NumTrnPats; //Total number of training patterns + fin>>Tmp>>NumTstPats; //Total testing patterns + fin>>Tmp>>NumIts; //training iterations + fin>>Tmp>>NumHN; //no. of hidden layers + + i=NumHN; + + //if more than one layer network + if(i-- > 0)fin>>Tmp>>NumHN1; + if(i-- > 0)fin>>Tmp>>NumHN2; + if(i-- > 0)fin>>Tmp>>NumHN3; + if(i-- > 0)fin>>Tmp>>NumHN4; + + ///read learn_rate,momentum, objective ... + fin>>Tmp>>LrnRate; + fin>>Tmp>>Mtm1; + fin>>Tmp>>Mtm2; + fin>>Tmp>>ObjErr; + fin>>Tmp>>Ordering; + + //general error checking.. + + if( NumIPs<1||NumIPs>MAXN||NumOPs<1||NumOPs>MAXN|| + NumTrnPats<1||NumTrnPats>MAXPATS||NumTrnPats<1||NumTrnPats>MAXPATS|| + NumIts<1||NumIts>20e6||NumHN1<0||NumHN1>900|| + LrnRate<0||LrnRate>1||Mtm1<0||Mtm1>10||Mtm2<0||Mtm2>10||ObjErr<0||ObjErr>10 + ){ cout<<"Invalid specs in data file!\n"; exit(1); } + + float **IPTrnData = Aloc2DAry(NumTrnPats,NumIPs); + float **OPTrnData = Aloc2DAry(NumTrnPats,NumOPs); + float **IPTstData = Aloc2DAry(NumTstPats,NumIPs); + float **OPTstData = Aloc2DAry(NumTstPats,NumOPs); + + //simultaneously read data into the two 2d arrays IPTrnData,OPTrnData of length 'NumTrnPats x NumIPs' and 'NumTrnPats x NumOPs' respectively + + for(i=0;i>IPTrnData[i][j]; + for(j=0;j>OPTrnData[i][j]; + } + + //simultaneously read data into the two 2d arrays IPTrnData,OPTrnData of length 'NumTrnPats x NumIPs' and 'NumTrnPats x NumOPs' respectively + for(i=0;i>IPTstData[i][j]; + for(j=0;j>OPTstData[i][j]; + } + fin.close(); + + //---------------------------file reading ends here --------------------------- + + //call the appropriate TestNet function + switch(NumHN){ + case 1: + TrainNet2(IPTrnData,OPTrnData,NumIPs,NumOPs,NumTrnPats,Ordering); + TestNet2(IPTstData,OPTstData,NumIPs,NumOPs,NumTstPats); + break; + case 2: + cout<<"Running TrainNet3: IP:"<input data, d[][]=>desired output data + int input_index; + float *h1 = new float[NumHN1]; // O/Ps of hidden layer + + float *y = new float[NumOPs]; // O/P of Net + + float *ad1= new float[NumHN1]; // HN1 back prop errors + float *ad2= new float[NumOPs]; // O/P back prop errors + + float PatErr,MinErr,AveErr,MaxErr; // Pattern errors + int p,i,j; // for loops indexes + long ItCnt=0; // Iteration counter + long NumErr=0; // Error counter (added for spiral problem) + + ofstream myfile; + myfile.open ("experiment1.csv"); + myfile << "Train results.\n"; + myfile << "IternationCount,MinErr,AverageErr,MaxError,PercentageError,PatternError,\n"; + + + cout<<"TrainNet2: IP:"<