/////////////////////////////////////////////////////////////// // // // nnet.cpp - simple neural net for experimentation // // - 3 layers, input, hidden, and output // // - hidden and output layers have one neuron // // - input layer is simply an input buffer // // - hidden layer function is sigmoid // // - output layer function is linear // // // // version: 1.1 // // 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 // // // /////////////////////////////////////////////////////////////// // Build Process // /////////////////////////////////////////////////////////////// // Files Required: // // nnet.cpp, bneuron.h, bneuron.cpp // // // // Build Command: // // cl -GX nnet.cpp bneuron.cpp // /////////////////////////////////////////////////////////////// // Maintenance History: // // ==================== // // ver 1.1 - 15 June 1998 // // - cosmetic changes to these comments // // ver 1.0 - 12 June 1998 // // - first release // // // /////////////////////////////////////////////////////////////// #include #include #include using namespace std; // //----< node output function >--------------------------------- double hidFunc(double sum) { return (1.0/(1.0+exp(-sum))); } double hidSlope(double sum) { return (hidFunc(sum)*(1.0 - hidFunc(sum))); } double outFunc(double sum) { return sum; } double outSlope(double sum) { return 1.0; } //----< neural network model >--------------------------------- double nnet(double input, double desired, double alpha[], double beta[]) { const double eta = .8; double hidSum = alpha[0] + alpha[1]*input; double hidOut = hidFunc(hidSum); double outSum = beta[0] + beta[1]*hidOut; double output = outFunc(outSum); double outFactor = -0.5*eta*(output-desired); // double dBeta0 = outFactor/outSlope(outSum); // double dBeta1 = dBeta0/hidOut; // double dAlph0 = dBeta0/(beta[1]*hidSlope(hidSum)); // double dAlph1 = dAlph0/input; double dBeta0 = outFactor*outSlope(outSum); double dBeta1 = dBeta0*hidOut; double dAlph0 = dBeta0*beta[1]*hidSlope(hidSum); double dAlph1 = dAlph0*input; beta[0] += dBeta0; beta[1] += dBeta1; alpha[0] += dAlph0; alpha[1] += dAlph1; return (output); } // //----< neural net administration >---------------------------- void main(int argc, char *argv[]) { if(argc == 1) { cout << " Please enter name of an input data file\n"; return; } ifstream in(argv[1]); if(!in.good()) { cout << " can't open data file " << argv[1] << endl; return; } // create input data array in memory int numIn; in >> numIn; double *input = new double[numIn]; if(!input) { cout << " can't create in-memory input array\n"; return; } // scale input array int i=0; do { in >> input[i++]; } while(in.good() && i < numIn); numIn = i-1; double max = 0; for(i=0; i