/////////////////////////////////////////////////////////////// // // // pr1.cpp - model neural network // // // // version: 1.0 // // Language: Visual C++, ver 5.0 // // Platform: Micron Dual Pentium Pro 200 // // Windows NT 4.0, service pack 3 // // Application: ELE591/791 Example // // Author: Jim Fawcett // // CST 2-187, (315) 443-3948 // // fawcett@neptune.syr.edu // // // /////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include "bneuron.h" #include "layer.h" #include "nnet.h" using namespace std; //----< swap two double values >------------------------------- void swap(double &d1, double &d2) { double temp = d1; d1 = d2; d2 = temp; } //----< generate uniformly distributed random numbers >-------- int uniform(int min, int max, bool newSeed) { static bool beenHere = false; static int count = 654321; double scaleFactor = double(max-min)/double(RAND_MAX); if(newSeed) { count += 123456; srand((unsigned)time(NULL) + count); } else if(!beenHere) { srand((unsigned)time(NULL) + count); beenHere = true; } return static_cast(rand()*scaleFactor + min); } // //----< randomize data vector >-------------------------------- void randomize(vec &v, int numSwaps=0) { if(numSwaps==0) numSwaps = 10*v.size(); int i,j,k; for(i=0; i-------------------------------------------- void main(int argc, char *argv[]) { cout << " Testing Neural Network (1:5:1): \n" << " ================================ \n\n"; if(argc < 3) { cout << "-- please enter name of training and test data files\n"; return; } // read training data ifstream inTrain(argv[argc-2]); if(!inTrain.good()) { cout << "-- can't open file " << argv[argc-2] << '\n'; return; } vec trainingData; inTrain >> trainingData; randomize(trainingData); // read testing data ifstream inTest(argv[argc-1]); if(!inTest.good()) { cout << "-- can't open file " << argv[argc-1] << '\n'; return; } vec testingData; inTest >> testingData; // build hidden layer with 5 nodes each having 1 bias and 1 data input sneuron sn(2); layer hlayer(sn,5); hlayer.randWts(-0.8,0.8,true); // build output layer with one neuron with 1 bias and 5 data inputs lneuron ln(6); layer olayer(ln,1); olayer.randWts(-2,2,true); // build (1:5S:1L) network if(!hlayer.checkSizes(2,5) || !olayer.checkSizes(6,1)) { cout << "-- size mismatch between layers in network\n"; exit(1); } nnet test(hlayer,olayer); // // define network input, output, and error vec input(2); input[0] = 1; // bias input[1] = trainingData[0]; vec output(1); vec error(1); // open output log file bool Logging = true; ofstream out("pr1.log"); if(!out.good()) { cout << "-- can't open log file\n"; Logging = false; } // display initial error and weights cout << " first testing iteration with randomized input data\n\n"; test.outputs(input,output); error[0] = output[0] - input[1]; cout << " output input error\n" << " ---------- ---------- ----------\n"; cout << setw(12) << output[0] << ", "; cout << setw(12) << input[1] << ", "; cout << setw(12) << error[0] << endl; if(Logging) test.logWts(out,out); // start training int i, count=0; double sumPrev=1000, sum=0, fractionalChange; do { for(i=0; i