Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

FFLayer.h

00001 // Copyright (C) 2001 Jean-Marc Valin
00002 #ifndef FFLAYER_H
00003 #define FFLAYER_H
00004 
00005 #include "Object.h"
00006 #include <math.h>
00007 #include <iostream>
00008 #include <stdlib.h>
00009 #include "functions.h"
00010 
00011 namespace FD {
00012 
00016 class FFLayer : public Object {
00017   public:
00019    void (*func) (float *, float *, int);
00020 
00022    void (*deriv_func) (float *, float *, int);
00023 
00024   protected:
00026    int nbNeurons;
00027 
00029    int nbInputs;
00030 
00032    float *weights;
00033 
00034    /*type of activation function (tansig, sigmoid, lin)*/
00035    std::string funcType;
00036 
00038    int weightOffset;
00039 
00042    int neuronOffset;
00043 
00045    float derivOffset;
00046 
00047   public:
00049    FFLayer() 
00050       : derivOffset(0) 
00051    {}
00052 
00053    //FFLayer(float *_weights) : weights(_weights) {};
00054 
00056    FFLayer(int _nbNeurons, int _nbInputs, float *_weights, int _weightOffset, int _neuronOffset, std::string type = "tansig");
00057 
00059    FFLayer(const FFLayer &layer) {std::cerr << "I wouldn't do that if I were you\n";}
00060    
00062    void setupAfterRead(float *_weights, int _weightOffset, int _neuronOffset);
00063 
00064    ~FFLayer() 
00065    {
00066    }
00067 
00069    void update(const float *previous, float *value, float *deriv=NULL)
00070    {
00071       for (int i=0;i<nbNeurons;i++)
00072       {
00073          float *w=weights + i*(nbInputs+1);              
00074          value[i] = vec_inner_prod(w, previous, nbInputs) + w[nbInputs];
00075 
00076       }
00077 
00078       if (func == tansig)
00079       {
00080          tansig(value, value, nbNeurons);
00081          if (deriv)
00082             deriv_tansig(value, deriv, nbNeurons);
00083       } else if (func == lin)
00084       {
00085          //lin(value, value, nbNeurons);
00086          if (deriv)
00087             deriv_lin(value, deriv, nbNeurons);
00088       } else if (func == sigmoid)
00089       {
00090          sigmoid(value, value, nbNeurons);
00091          if (deriv)
00092             deriv_sigmoid(value, deriv, nbNeurons);
00093       } else {
00094          std::cerr << "unknown\n";
00095          func(value, value, nbNeurons);
00096          if (deriv)
00097             deriv_func(value, deriv, nbNeurons);
00098       }
00099       if (deriv)
00100          vec_add_scal(derivOffset, deriv, deriv, nbNeurons);
00101    }
00102 
00104    int size() {return nbNeurons;}
00105 
00107    int getNbWeights() {return nbNeurons*(nbInputs+1);}
00108 
00111    int getNeuronWeightOffset(int i) {return weightOffset+i*(nbInputs+1);}
00112 
00113    int getWeightOffset() {return weightOffset;}
00114 
00115    int getNeuronOffset() {return neuronOffset;}
00116 
00117    void init(float minmax);
00118 
00121    void init(double *mean, double *std);
00122 
00124    void setBias(double *minmax);
00125 
00126    float *getWeights(int i) {return weights + i*(nbInputs+1);}
00127 
00129    void printOn(std::ostream &out) const;
00130 
00132    void readFrom (std::istream &in);
00133 
00134    void setDerivOffset(float d) {derivOffset=d;}
00135 };
00136 
00138 std::istream &operator >> (std::istream &in, FFLayer &layer);
00139 
00140 }//namespace FD
00141 
00142 
00143 #endif

Generated on Wed Oct 5 14:28:55 2005 for FlowDesigner by  doxygen 1.4.4