/////////////////////////////////////////////////////////////// // // // bneuron.cpp - model basic neuron // // // // version: 1.3 // // 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 "bneuron.h" using namespace std; //----< static factory function >------------------------------ neuron* neuron::makeNeuron(const neuron &n) { return n.clone(); } //----< cloning neurons >-------------------------------------- neuron* neuron::clone() const { neuron *temp; try { temp = new neuron(*this); // copy of self } catch(...) { throw "memory allocation failure\n"; return 0; } return temp; } //----< sizing constructor >----------------------------------- neuron::neuron(int inputs) : wts(inputs), ins(inputs) { int i; for(i=0; i--------------------------------- void neuron::size(int n) { if(n<=0) { cout << " invalid size parameter " << n << endl; return; } ins.size(n); // vector resize wts.size(n); // " " } //----< copy constructor >------------------------------------- neuron::neuron(const neuron &n) : wts(n.wts), ins(n.ins) { } //----< assignment operator >---------------------------------- neuron& neuron::operator=(const neuron &n) { if(this == &n) return *this; wts = n.wts; ins = n.ins; return *this; } //----< check input index >------------------------------------ int neuron::chkIndex(int i) { if(i<0 || ins.size()<=i) { cout << " domain error in neuron::chkIndex(" << i << ")\n"; exit(1); } return i; } //----< access value of ith weight >--------------------------- double& neuron::weight(int i) { return wts[chkIndex(i)]; } //----< access value of ith input >---------------------------- double& neuron::input(int i) { return ins[chkIndex(i)]; } //----< access input vector >---------------------------------- vec& neuron::inputs() { return ins; } // //----< access weights >--------------------------------------- vec& neuron::weights() { return wts; } //----< initialize weights with random values >---------------- void neuron::randWts(double min, double max, bool newSeed) { static int count = 654321; static bool beenHere = false; double scaleFactor = (max-min)/RAND_MAX; if(newSeed || !beenHere) { count += 123456; srand((unsigned)time(NULL) + count); beenHere = true; } int i; for(i=0; i----------------------------- double neuron::sum() { double _sum = wts[0]; // bias int i; for(i=1; i---------------------------------- double neuron::outFunc(double sum) { if(sum > 0) return 1; if(sum < 0) return -1; return 0; } //----< output nonlinearity slope >---------------------------- double neuron::slope(double sum) { return 0; } //----< fetch neuron output >---------------------------------- double neuron::output() { return outFunc(sum()); } //----< derivative of output >--------------------------------- double neuron::outDeriv() { return slope(sum()); } //----< display neuron elements >------------------------------ void neuron::display(ostream &out, char *msg) { if(msg) out << msg << '\n'; out << " neuron size = " << ins.size() << '\n'; out << " weights:\n"; wts.display(out); out << " inputs:\n"; ins.display(out); out << " sum = " << sum() << '\n'; } // //----< sigmoid neuron output function >----------------------- double sneuron::outFunc(double sum) { return (1.0/(1.0+exp(-sum))); } //----< slope of sigmoid function >---------------------------- double sneuron::slope(double sum) { return (outFunc(sum)*(1.0-outFunc(sum))); } //----< cloning sigmoid neurons >------------------------------ neuron* sneuron::clone() const { neuron* temp; try { temp = new sneuron(*this); // copy of self } catch(...) { throw "memory allocation failure\n"; return 0; } return temp; } //----< linear neuron output function >------------------------ double lneuron::outFunc(double sum) { return sum; } //----< slope of linear function >----------------------------- double lneuron::slope(double sum) { return 1.0; } //----< cloning linear neurons >------------------------------- neuron* lneuron::clone() const { neuron* temp; try { temp = new lneuron(*this); // copy of self } catch(...) { throw "memory allocation failure\n"; return 0; } return temp; } // //----< test stub >-------------------------------------------- #ifdef TEST_BNEURON void main() { cout << " Simulation of a Basic Neuron \n" << " ============================== \n\n"; const int numInputs = 2; double min=-1, max=3, inc=3; neuron Einstein1(numInputs); int i; for(i=0; i