Neuron.h

00001 /*
00002  *====================================================================
00003  * HISTORY:
00004  * -------
00005  * $Log: Neuron.h,v $
00006  * Revision 1.1  2008/08/23 14:28:48  samit_ahlawat
00007  * Feed forward neural network library
00008  *
00009  * Revision 1.1  2007/11/06 10:06:30  samit_ahlawat
00010  * Checking in first version of basic neural net infrastructure
00011  *
00012  * 
00013  * ID:
00014  * --
00015  * static const char *CvsId = "@(#)$Id: Neuron.h,v 1.1 2008/08/23 14:28:48 samit_ahlawat Exp $";
00016  *====================================================================
00017  */
00018 
00019 #ifndef _AIMAGIK_NEURON_H_
00020 #define _AIMAGIK_NEURON_H_
00021 
00022 #include <vector>
00023 #include "ActivationFunc.h"
00024 #include <stdexcept>
00025 #include <cstdlib>
00026 #include <iostream>
00027 
00028 typedef double (*ACTIVATION_FUNC) (double);
00029 typedef double (*DERIV_ACTIVATION_FUNC) (double);
00030 
00047 class Neuron
00048 {
00049         private:
00051                 double* pWeights;
00053                 int iNumFrom;
00054 
00056                 int iIndex;
00057 
00058     public:
00064 #ifdef SAME_ACTIVATION
00065         static
00066 #endif
00067                 ACTIVATION_FUNC pActivationFunc;
00069 #ifdef SAME_ACTIVATION
00070         static
00071 #endif        
00072                 DERIV_ACTIVATION_FUNC pDerivActivationFunc;
00073 
00074         public:
00075                 Neuron();
00081                 Neuron(int numInputs) throw (std::bad_alloc);
00082                 Neuron(const Neuron& neuron) throw (std::bad_alloc);
00083         Neuron& operator= (const Neuron& neuron) throw (std::bad_alloc);
00084 
00086                 void SetNumInputs(int num);
00087                 int GetNumInputs() const { return iNumFrom; }
00088                 void SetActivationFunc(ACTIVATION_FUNC activ_func) { pActivationFunc = activ_func; }
00089         void SetDerivActivationFunc(DERIV_ACTIVATION_FUNC deriv) { pDerivActivationFunc = deriv; }
00092                 void SetWeight(int indx, double weight);
00093                 void SetWeights(const std::vector<double>& weights);
00094                 double GetWeight(int indx) const;
00095                 void GetWeights(std::vector<double>& weights) const;
00098         double* GetWeights() { return pWeights; }
00099         /* Calculate the output from neuron for supplied input.
00100          * If inputs to neuron have not changed, it is more efficient
00101          * to call this function once to get the output and store result
00102          * since this function recomputes the output from input.
00103          * @param
00104          * inputs: The vector containing inputs (all of them need not
00105          * be feeding into this neuron)
00106          * indices: indices from inputs (vector) that feed into this 
00107          * neuron. indices must be a pointer to an array having elements
00108          * = iNumFrom
00109          */
00110                 double GetOutput(const std::vector<double>& inputs, const int* indices);
00111 
00112         /* Overloaded version when total input for neuron is needed too.
00113          * total input to the neuron (which provides the 
00114          * activation) is returned in the last argument, net_input
00115          * Note neuron activation = g(net_input) where g is the activation
00116          * function.
00117          * Calculate the output from neuron for supplied input.
00118          * If inputs to neuron have not changed, it is more efficient
00119          * to call this function once to get the output and store result
00120          * since this function recomputes the output from input.
00121          * @param
00122          * inputs: The array containing inputs (all of them need not
00123          * be feeding into this neuron)
00124          * indices: indices from inputs (vector) that feed into this 
00125          * neuron. indices must be a pointer to an array having elements
00126          * = iNumFrom
00127          */        
00128         double GetOutput(const std::vector<double>& inputs, const int* indices, double& net_input);        
00129 
00130         /* Overloaded version when input is provided as array
00131          * Calculate the output from neuron for supplied input.
00132          * If inputs to neuron have not changed, it is more efficient
00133          * to call this function once to get the output and store result
00134          * since this function recomputes the output from input.
00135          * @param
00136          * inputs: The array containing inputs (all of them need not
00137          * be feeding into this neuron)
00138          * indices: indices from inputs (vector) that feed into this 
00139          * neuron. indices must be a pointer to an array having elements
00140          * = iNumFrom
00141          */
00142                 double GetOutput(const double* inputs, const int* indices);
00143 
00144         /* Overloaded version when input is provided as array
00145          * and total input to the neuron (which provides the 
00146          * activation is returned in the last argument, net_input)
00147          * Note neuron activation = g(net_input) where g is the activation
00148          * function.
00149          * Calculate the output from neuron for supplied input.
00150          * If inputs to neuron have not changed, it is more efficient
00151          * to call this function once to get the output and store result
00152          * since this function recomputes the output from input.
00153          * @param
00154          * inputs: The array containing inputs (all of them need not
00155          * be feeding into this neuron)
00156          * indices: indices from inputs (vector) that feed into this 
00157          * neuron. indices must be a pointer to an array having elements
00158          * = iNumFrom
00159          */        
00160         double GetOutput(const double* inputs, const int* indices, double& net_input);
00161 
00162                 void SetIndex(int i) { iIndex = i; }
00163                 int GetIndex() const { return iIndex; }
00164         
00169                 void Clear();
00170 
00171                 ~Neuron();
00172 };
00173 
00174 std::ostream& operator<< (std::ostream& os, const Neuron& nr);
00175 std::ostream& operator<< (std::ostream& os, const Neuron* nr);
00176 
00177 #endif // _AIMAGIK_NEURON_H_
00178 

Generated on Sun Aug 24 17:33:41 2008 by  doxygen 1.4.6