C/C++ MLP Backprop Code
//////////////////////////////////////////////////////////////////////////// //MLP neural network in C++ //Original source code by Dr Phil Brierley //www.philbrierley.com //Translated to C++ - dspink Sep 2005 //This code may be freely used and modified at will //C++ Compiled using Bloodshed Dev-C++ free compiler http://www.bloodshed.net/ //C Compiled using Pelles C free windows compiler http://smorgasbordet.com/ //////////////////////////////////////////////////////////////////////////// //#include
#include
#include
#include
#include
//// Data dependent settings //// #define numInputs 3 #define numPatterns 4 //// User defineable settings //// #define numHidden 4 const int numEpochs = 500; const double LR_IH = 0.7; const double LR_HO = 0.07; //// functions //// void initWeights(); void initData(); void calcNet(); void WeightChangesHO(); void WeightChangesIH(); void calcOverallError(); void displayResults(); double getRand(); //// variables //// int patNum = 0; double errThisPat = 0.0; double outPred = 0.0; double RMSerror = 0.0; // the outputs of the hidden neurons double hiddenVal[numHidden]; // the weights double weightsIH[numInputs][numHidden]; double weightsHO[numHidden]; // the data int trainInputs[numPatterns][numInputs]; int trainOutput[numPatterns]; //============================================================== //************** function definitions ************************** //============================================================== //*********************************** // calculates the network output void calcNet(void) { //calculate the outputs of the hidden neurons //the hidden neurons are tanh int i = 0; for(i = 0;i
5) { weightsHO[k] = 5; } } } //************************************ // adjust the weights input-hidden void WeightChangesIH(void) { for(int i = 0;i