multilayerffnn.h

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Robot Group Leipzig * 00003 * martius@informatik.uni-leipzig.de * 00004 * fhesse@informatik.uni-leipzig.de * 00005 * der@informatik.uni-leipzig.de * 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 *************************************************************************** 00022 * * 00023 * DESCRIPTION * 00024 * * 00025 * $Log: multilayerffnn.h,v $ 00026 * Revision 1.8 2006/12/21 11:44:17 martius 00027 * commenting style for doxygen //< -> ///< 00028 * FOREACH and FOREACHC are macros for collection iteration 00029 * 00030 * Revision 1.7 2006/11/23 08:43:46 martius 00031 * bugfix in store and restore 00032 * 00033 * Revision 1.6 2006/07/27 15:22:18 martius 00034 * activations stored (processing must preceed learning and reponse calculation) 00035 * 00036 * 00037 * * 00038 ***************************************************************************/ 00039 #ifndef __MULTILAYERFFNN_H 00040 #define __MULTILAYERFFNN_H 00041 00042 #include <vector> 00043 00044 #include "feedforwardnn.h" 00045 #include "invertablemodel.h" 00046 00047 class Layer : public Storeable { 00048 public: 00049 Layer(int size, double factor_bias=0.1, 00050 ActivationFunction actfun = FeedForwardNN::linear, 00051 ActivationFunction dactfun = FeedForwardNN::dlinear) 00052 : size(size), factor_bias(factor_bias), actfun(actfun), dactfun(dactfun) {} 00053 00054 /***STOREABLE ******/ 00055 /// stores the layer binary into file stream 00056 bool store(FILE* f) const; 00057 /// restores the layer binary from file stream 00058 bool restore(FILE* f); 00059 00060 int size; 00061 double factor_bias; 00062 ActivationFunction actfun; ///< callback activation function 00063 ActivationFunction dactfun; ///< first derivative of the activation function 00064 00065 // prints the Layer data-structure 00066 friend std::ostream& operator<<(std::ostream& , const Layer&); 00067 }; 00068 00069 /// multi layer neural network with configurable activation functions 00070 class MultiLayerFFNN : public FeedForwardNN, public InvertableModel { 00071 public: 00072 /** 00073 @param eps learning rate 00074 @param layers Layer description (the input layer is not specified (always linear)) 00075 */ 00076 MultiLayerFFNN(double eps, const std::vector<Layer>& layers) 00077 : eps(eps), layers(layers) { 00078 00079 initialised = false; 00080 } 00081 virtual ~MultiLayerFFNN(){ } 00082 00083 /// initialisation of the network with the given number of input and output units 00084 virtual void init(unsigned int inputDim, unsigned int outputDim, double unit_map = 0.0); 00085 00086 /// passive processing of the input 00087 virtual const matrix::Matrix process (const matrix::Matrix& input); 00088 00089 /** performs learning and returns the network output before learning 00090 (process should be called before) */ 00091 virtual const matrix::Matrix learn (const matrix::Matrix& input, 00092 const matrix::Matrix& nom_output, 00093 double learnRateFactor = 1); 00094 00095 /** \see InvertableModel::response 00096 (process should be called before) 00097 */ 00098 virtual const matrix::Matrix response(const matrix::Matrix& input) const; 00099 00100 /// returns the number of input neurons 00101 virtual unsigned int getInputDim() const { 00102 return weights[0].getN(); 00103 } 00104 /// returns the number of output neurons 00105 virtual unsigned int getOutputDim() const { 00106 return (weights.rbegin())->getM(); 00107 } 00108 00109 /// damps the weights and the biases by multiplying (1-damping) 00110 virtual void damp(double damping); 00111 00112 00113 /************** STOREABLE **********************************/ 00114 /// stores the layer binary into file stream 00115 bool store(FILE* f) const; 00116 /// restores the layer binary from file stream 00117 bool restore(FILE* f); 00118 00119 00120 /************** CONFIGURABLE INTERFACE ************************/ 00121 virtual paramkey getName() const { 00122 return std::string("multilayerffnn"); 00123 } 00124 00125 virtual paramval getParam(const paramkey& key) const { 00126 if(key == "epsM") return eps; 00127 else return 0.0; 00128 } 00129 00130 virtual bool setParam(const paramkey& key, paramval val){ 00131 if( key == "epsM") { 00132 eps = val; 00133 return true; 00134 } else return false; 00135 } 00136 00137 virtual paramlist getParamList() const{ 00138 paramlist l; 00139 l.push_back(std::pair<paramkey, paramval> ("epsM", eps)); 00140 return l; 00141 } 00142 00143 virtual const Layer& getLayer(unsigned int layer) const { 00144 assert(layer < layers.size()); 00145 return layers[layer]; 00146 } 00147 00148 virtual const matrix::Matrix& getWeights(unsigned int to_layer) const { 00149 assert(to_layer < weights.size()); 00150 return weights[to_layer]; 00151 } 00152 00153 virtual const matrix::Matrix& getBias(unsigned int of_layer) const { 00154 assert(of_layer < bias.size()); 00155 return bias[of_layer]; 00156 } 00157 00158 protected: 00159 double eps; 00160 std::vector<Layer> layers; 00161 std::vector<matrix::Matrix> weights; 00162 std::vector<matrix::Matrix> bias; 00163 00164 std::vector<matrix::Matrix> ys; // activations 00165 std::vector<matrix::Matrix> zs; // potentials 00166 00167 bool initialised; 00168 }; 00169 00170 #endif

Generated on Tue Jan 16 02:14:36 2007 for Robotsystem of the Robot Group Leipzig by doxygen 1.3.8