/////////////////////////////////////////////////////////////// // neuron.cpp - neuron family used in neural net // // ver 1.0 // // // // Language: Visual C++, ver 6.0 // // Platform: Dell XPS B1000r, Windows 2000 // // Application: Prototype for Pr#1, ELE 591/791 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////// #include "neuron.h" #include using namespace std; //----< generic function to display container contents >------- template void showCont(ostream &out, const char *msg, const T &cont) { out << msg; T::const_iterator it; for(it = cont.begin(); it != cont.end(); it++) out << " " << *it; } //----< set input value >-------------------------------------- void neuron::input (int select, double value) { if(_inputs.size() > select) _inputs[select] = value; else throw "index out of bounds in neuron::input()"; } //----< set weight value >------------------------------------- void neuron::weight(int select, double value) { if(_weights.size() > select) _weights[select] = value; else throw "index out of bounds in neuron::weight()"; } //----< show inputs >------------------------------------------ void neuron::showInputs(ostream &out, const char *msg) { showCont(out,msg, _inputs); } //----< show weights >----------------------------------------- void neuron::showWeights(ostream &out, const char *msg) { showCont(out,msg, _weights); } // //----< constructor >------------------------------------------ linearNeuron::linearNeuron(int numInputs) { int i; for(i=0; i------------------------------------------------ neuron* linearNeuron::clone( ) { return new linearNeuron(_inputs.size()); } //----< compute output >--------------------------------------- double linearNeuron::output( ) { int i; double s = 0; for(i=0; i<_inputs.size(); i++) s += _weights[i]*_inputs[i]; return s; } //----< compute slope >---------------------------------------- double linearNeuron::slope( ) { return 1; } //----< test stub >-------------------------------------------- #ifdef TEST_NEURON void main() { linearNeuron ln(3); ln.input(1,2.0); ln.input(2,3.0); ln.showInputs(cout,"\n neuron inputs: "); ln.weight(1,2.0); ln.weight(2,3.0); ln.showWeights(cout,"\n neuron weights: "); cout << "\n linear neuron output is: " << ln.output() << endl; cout << "\n\n"; } #endif