#ifndef BNEURON_H #define BNEURON_H /////////////////////////////////////////////////////////////// // bneuron.h - 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 // /////////////////////////////////////////////////////////////// /* bneuron operations ================== This class models the operation of a single neuron using a sgn() function as the output nonlinearity. It is designed to allow use as a base for public derivation. It provides protected virtual member functions: double outFunc(double), double slope(double) which can be overridden in a derived class to use some other nonlin- earity, e.g., sigmoid. Two derived classes are provided, that have sigmoid and linear output functions. Neuron Public Interface: ======================== neuron n; void construct n neuron n2 = n1; construct n2 as copy of n1 neuron myNeuron(numInputs); construct a basic neuron instance sneuron nlNeuron(numInputs); construct neuron with sigmoid output lneuron lnNeuron(numInputs); construct neuron with linear output neuron = neuron::makeNeuron(n); create an clone of the neuron n can use basic or any derived neuron n2 = n1; assign n1's state to n2 weight(0) = 3.5; set 0th weight double d = myNeuron.weight(2); get 2nd weight wts = myNeuron.weights(); read myNeuron weight vector myNeuron.weights() = doubleVec; write myNeuron weight vector myNeuron.randWts(min,max,newseed);initialize myNeuron weights with random number between min, max using new seed if newseed is true myNeuron.input(1) = 2.0; set 1st input value (count from 0) double in = myNeuron.input(3); get 3rd input value ( " " ") ins = myNeuron.inputs(); read myNeuron inputs myNeuron.inputs() = ins; write myNeuron inputs double s = myNeuron.sum(); returns current input to function double out = nlNeuron.output(); generate neuron output double slope = lnNeuron.slope(); generate output slope int s = myNeuron.size(); get number of inputs --< used for weight updates >-- myNeuron.inFactor(v); v = input*outDeriv() myNeuron.wtFactor(v); v = weight*outDeriv() ------------------------------- myNeuron.display(cout,"..."); display neuron state with msg */ // /////////////////////////////////////////////////////////////// // // // Build Process // // ------------- // // Files required: // // bneuron.h, bneuron.cpp // // // // build command for Neptune cluster with VC++ 5.0: // // I:\fawcett\ecs500\setenv.bat (neptune only) // // cl -GX -DTEST_BNEURON bneuron.cpp // // // /////////////////////////////////////////////////////////////// /* Maintenance History ------------------- ver 1.3 : 11 July 98 - added void, copy constructor and assignment operator - factored sum function out of outFunc() and slope() - added display and randWts functions ver 1.2 : 30 June 98 - added virtual clone member and static clone factory - added resizing function ver 1.1 : 15 June 98 - removed bias member, use input[0] as bias term - made outFunc virtual member and added slope as a virtual member function - derived sneuron with sigmiod nonlinearity and derived lneuron with linear output function. ver 1.0 : 28 May 98 - first release */ // #include #include "vec.h" using namespace std; //----< declare class for basic neuron simulator >------------- class neuron { public: neuron(int inputs=1); neuron(const neuron &n); virtual ~neuron() { } static neuron* makeNeuron(const neuron &n); virtual neuron* clone() const; neuron& operator=(const neuron &n); double& input (int i); double& weight(int i); void randWts(double min, double max, bool newSeed); vec& inputs(); vec& weights(); double sum(); double output(); double outDeriv(); int size() const; void size(int n); void display(ostream &out, char *msg=0); protected: virtual double outFunc(double sum); virtual double slope(double sum); int chkIndex(int i); private: vec wts; // vector of weights vec ins; // vector of input values }; inline neuron::size() const { return ins.size(); } // //----< neuron with sigmoid output function >------------------ class sneuron : public neuron { public: sneuron(int inputs=1) : neuron(inputs) { } sneuron(const sneuron &sn) : neuron(sn) { } virtual neuron* clone() const; neuron& operator=(const sneuron &sn); protected: virtual double outFunc(double sum); // sigmoid virtual double slope(double sum); }; inline neuron& sneuron::operator=(const sneuron &sn) { return (neuron::operator=(sn)); } //----< neuron with linear output function >------------------- class lneuron : public neuron { public: lneuron(int inputs=1) : neuron(inputs) { } lneuron(const lneuron &ln) : neuron(ln) { } virtual neuron* clone() const; neuron& operator=(const lneuron &ln); protected: virtual double outFunc(double sum); // linear virtual double slope(double sum); }; inline neuron& lneuron::operator=(const lneuron &ln) { return (neuron::operator=(ln)); } #endif