/////////////////////////////////////////////////////////////// // // // layer.cpp - neural net layer // // // // 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 // // // /////////////////////////////////////////////////////////////// #include #include #include "bneuron.h" #include "layer.h" //----< void constructor >------------------------------------- layer::layer() : numNeurons(0) { } //----< size constructor >------------------------------------- layer::layer(const neuron &nModel, int number, const string &name) : numNeurons(number), nPtrArray(number), _name(name) { int i; for(i=0; isize(nModel.size()); // set num inputs } } //----< copy constructor >------------------------------------- layer::layer(const layer &l) : numNeurons(l.numNeurons), nPtrArray(l.numNeurons), _name(l._name) { int i; try { for(i=0; i clone(); } catch(...) { throw ("\nmemory alloc failure in layer(const layer &l)\n"); } } // //----< destructor >------------------------------------------- layer::~layer() { int i; for(i=0; i------------------------------------------- layer& layer::operator=(const layer &l) { if(this == &l) return *this; int i; for(i=0; i clone(); } catch(...) { throw ("\nmemory alloc failure in layer assignment\n"); } numNeurons = l.numNeurons; return *this; } //----< indexing >--------------------------------------------- neuron& layer::operator[](int index) { static neuron safe; if(index<0 || numNeurons<=index) { cout << " index " << index << " out of domain in layer[index]\n"; return safe; } return (*nPtrArray[index]); } //----< get weights >------------------------------------------ void layer::weights(const vec< vec > &wts) { int i; for(i=0; iweights() = wts[i]; } // //----< give weights random values >--------------------------- void layer::randWts(double min, double max, bool newSeed) { int i; for(i=0; i randWts(min,max,newSeed); } //----< are vector dimensions valid? >------------------------- bool layer::checkSizes(int vinSize, int voutSize) { int layerDim = nPtrArray[0]->inputs().size(); if(vinSize != layerDim) { cout << " layer expected input vector dim = " << vinSize << '\n' << " but input supplied has dim = " << layerDim << '\n'; return false; } if(voutSize != numNeurons) { cout << " layer expected output vector dim = " << numNeurons << '\n' << " but output supplied has dim = " << voutSize << '\n'; return false; } return true; } //----< compute outputs from input vec >----------------------- void layer::outputs(const vec &vin, vec &vout) { int i; for(i=0; iinputs() = vin; disconnect(); // this hook does nothing here for(i=0; ioutput(); } //----< display >---------------------------------------------- void layer::display(ostream &out, char *msg) { out << msg << "\n layer size = " << nPtrArray.size() << '\n'; int i; for(i=0; i display(out,"internal neuron:"); } // #ifdef TEST_LAYER void main() { cout << "\n Testing Neural Net Layer Model" << "\n ================================\n"; // build layer with 5 nodes each having 2 inputs lneuron ln(2); layer nlayer(ln,5); // define weights vec< vec > wts(5); int i; for(i=0; i<5; i++) { wts[i].size(2); wts[i][0] = i; wts[i][1] = i+0.5; } nlayer.weights(wts); // define inputs vec input(2); input[0] = 1; // bias input[1] = 2; // from previous layer // create vector to hold output vec output(5); // evaluate layer output nlayer.outputs(input,output); nlayer.display(cout,"state from linear neuron nlayer:"); cout << "\n Output vector is:" << output << endl; cout << "\f\n Testing void, copy construction and assignment" << "\n ================================================\n\n"; layer Copied = nlayer; Copied.display(cout,"Copy constructed from nlayer:"); layer Void; cout << "\f\n"; Void = nlayer; Void.display(cout,"Assigned from nlayer:"); cout << "\n Testing random initialization of layer weights:" << "\n =================================================\n\n"; layer randomized = nlayer; randomized.randWts(0.2, 0.4, true); randomized.display(cout,"layer with randomized weights:"); } #endif