Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multilayerffnn.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __MULTILAYERFFNN_H
25 #define __MULTILAYERFFNN_H
26 
27 #include <vector>
28 
29 #include "feedforwardnn.h"
30 #include "invertablemodel.h"
31 #include "layer.h"
32 
33 
34 /// multi layer neural network with configurable activation functions
35 class MultiLayerFFNN : public FeedForwardNN {
36 public:
37 
38  /**
39  @param eps learning rate
40  @param layers Layer description (the input layer is not specified (always linear))
41  @param useBypass if true, then a connection from input to output layer is included
42  @param someInternalParams if true then only a few parameters are send to plotting
43  */
44  MultiLayerFFNN(double eps, const std::vector<Layer>& layers, bool useBypass=false,
45  bool someInternalParams=true);
46  virtual ~MultiLayerFFNN(){ }
47 
48  /** initialisation of the network with the given number of input and output units.
49  The dimensionality of the ouputlayer is automatically adjusted.
50  @param unit_map defines the approximate response of the network
51  after initialisation (if unit_map=1 the weights are unit matrices).
52  @param randGen pointer to random generator, if 0 an new one is used
53  */
54  virtual void init(unsigned int inputDim, unsigned int outputDim,
55  double unit_map = 0.0, RandGen* randGen = 0);
56 
57  /// passive processing of the input
58  virtual const matrix::Matrix process (const matrix::Matrix& input);
59 
60  /** performs learning and returns the network output before learning
61  (process should be called before) */
62  virtual const matrix::Matrix learn (const matrix::Matrix& input,
63  const matrix::Matrix& nom_output,
64  double learnRateFactor = 1);
65 
66  /** response matrix of neural network at given input
67 
68  \f[ J_ij = \frac{\partial y_i}{\partial x_j} \f]
69  \f[ J = G_n' W_n G_{n-1}' W_{n-1} ... G_1' W_1 \f]
70  with \f$W_n\f$ is the weight matrix of layer n and
71  \f$ G'\f$ is a diagonal matrix with \f$ G'_ii = g'_i \f$ as values on the diagonal.
72  ATTENTION: input is ignored! use process before!
73  */
74  virtual const matrix::Matrix response(const matrix::Matrix& input) const;
75 
76  /** calculates the input shift v to a given output shift xsi via pseudo inversion.
77 
78  \f[o+\xi = \psi(i+v)\f]
79 
80  The result is a vector of dimension inputdim.
81  ATTENTION: input is ignored! use process before!
82  */
83  virtual const matrix::Matrix inversion(const matrix::Matrix& input, const matrix::Matrix& xsi) const;
84 
85 
86  /// returns the number of input neurons
87  virtual unsigned int getInputDim() const {
88  return weights[0].getN();
89  }
90  /// returns the number of output neurons
91  virtual unsigned int getOutputDim() const {
92  return (weights.rbegin())->getM();
93  }
94 
95  /// returns activation of the given layer. Layer 0 is the first hidden layer
96  virtual const matrix::Matrix& getLayerOutput(unsigned int layer) const {
97  assert(layer < layers.size());
98  return ys[layer];
99  }
100 
101  /// damps the weights and the biases by multiplying (1-damping)
102  virtual void damp(double damping);
103 
104  // total number of layers (1 means no hidden units)
105  virtual unsigned int getLayerNum() const {
106  return layers.size();
107  }
108 
109  /// layers 0 is the first hidden layer
110  virtual const Layer& getLayer(unsigned int layer) const {
111  assert(layer < layers.size());
112  return layers[layer];
113  }
114 
115  /// layers 0 is the first hidden layer
116  virtual Layer& getLayer(unsigned int layer) {
117  assert(layer < layers.size());
118  return layers[layer];
119  }
120 
121  /// weight matrix 0 connects input with the first hidden layer
122  virtual const matrix::Matrix& getWeights(unsigned int to_layer) const {
123  assert(to_layer < weights.size());
124  return weights[to_layer];
125  }
126 
127  /// weight matrix 0 connects input with the first hidden layer
128  virtual matrix::Matrix& getWeights(unsigned int to_layer) {
129  assert(to_layer < weights.size());
130  return weights[to_layer];
131  }
132 
133  /// Note: layers 0 is the first hidden layer
134  virtual const matrix::Matrix& getBias(unsigned int of_layer) const {
135  assert(of_layer < bias.size());
136  return bias[of_layer];
137  }
138 
139  /// Note: layers 0 is the first hidden layer
140  virtual matrix::Matrix& getBias(unsigned int of_layer) {
141  assert(of_layer < bias.size());
142  return bias[of_layer];
143  }
144 
145  /************** STOREABLE **********************************/
146  /// stores the layer binary into file stream
147  bool store(FILE* f) const;
148  /// restores the layer binary from file stream
149  bool restore(FILE* f);
150 
151 
152  /// writes the layer ASCII into file stream (not in the storable interface)
153  bool write(FILE* f) const;
154 
155  /************** Inspectable **********************************/
156  virtual iparamkeylist getInternalParamNames() const;
157  virtual iparamvallist getInternalParams() const;
158  virtual ilayerlist getStructuralLayers() const;
160 
161 
163  assert(!initialised); this->someInternalParams = someInternalParams;
164  }
165 
166 public:
167  double eps; ///< learning rate
168 
169  /**
170  * sets the activation function (and derivative and inversion too) for ALL layers!
171  * @param actfun the activation function to be used
172  * @return the activation functions which where used until now
173  */
174  virtual std::vector<ActivationFunction> setActivationFunction(ActivationFunction actfun);
175 
176 /**
177  * sets the activation functions (and derivative and inversion too) for all layers.
178  * @note: normally you call setActivationFunction() first and get a list of the used
179  * activation functions, which are set back with this function
180  * @param actfunList the list of actfuns to be used
181  */
182  virtual void setActivationFunctions(std::vector<ActivationFunction> actfunList);
183 
184 
185 protected:
186  std::vector<Layer> layers;
187  std::vector<matrix::Matrix> weights;
188  std::vector<matrix::Matrix> bias;
189  std::vector<matrix::Matrix> smallids; // small unit matrices for pseudoinversion
190  bool useBypass;
193 
195  std::vector<matrix::Matrix> ys; // activations
196  std::vector<matrix::Matrix> zs; // potentials
197 
198  double lambda; // regularisation value for pseudoinverse
200 };
201 
202 #endif
Matrix type.
Definition: matrix.h:65
double lambda
Definition: multilayerffnn.h:198
virtual void init(unsigned int inputDim, unsigned int outputDim, double unit_map=0.0, RandGen *randGen=0)
initialisation of the network with the given number of input and output units.
Definition: multilayerffnn.cpp:45
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
Definition: layer.h:31
matrix::Matrix bypassWeights
Definition: multilayerffnn.h:191
bool store(FILE *f) const
stores the layer binary into file stream
Definition: multilayerffnn.cpp:206
virtual unsigned int getInputDim() const
returns the number of input neurons
Definition: multilayerffnn.h:87
virtual const matrix::Matrix & getLayerOutput(unsigned int layer) const
returns activation of the given layer. Layer 0 is the first hidden layer
Definition: multilayerffnn.h:96
bool someInternalParams
Definition: multilayerffnn.h:192
bool write(FILE *f) const
writes the layer ASCII into file stream (not in the storable interface)
Definition: multilayerffnn.cpp:225
double eps
learning rate
Definition: multilayerffnn.h:167
bool restore(FILE *f)
restores the layer binary from file stream
Definition: multilayerffnn.cpp:245
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual const Layer & getLayer(unsigned int layer) const
layers 0 is the first hidden layer
Definition: multilayerffnn.h:110
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: multilayerffnn.cpp:284
std::vector< matrix::Matrix > weights
Definition: multilayerffnn.h:187
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: multilayerffnn.cpp:326
bool initialised
Definition: multilayerffnn.h:199
matrix::Matrix input
Definition: multilayerffnn.h:194
std::vector< matrix::Matrix > smallids
Definition: multilayerffnn.h:189
virtual const matrix::Matrix process(const matrix::Matrix &input)
passive processing of the input
Definition: multilayerffnn.cpp:84
virtual matrix::Matrix & getWeights(unsigned int to_layer)
weight matrix 0 connects input with the first hidden layer
Definition: multilayerffnn.h:128
MultiLayerFFNN(double eps, const std::vector< Layer > &layers, bool useBypass=false, bool someInternalParams=true)
Definition: multilayerffnn.cpp:32
virtual iparamvallist getInternalParams() const
Definition: multilayerffnn.cpp:305
virtual matrix::Matrix & getBias(unsigned int of_layer)
Note: layers 0 is the first hidden layer.
Definition: multilayerffnn.h:140
virtual const matrix::Matrix & getWeights(unsigned int to_layer) const
weight matrix 0 connects input with the first hidden layer
Definition: multilayerffnn.h:122
virtual void setActivationFunctions(std::vector< ActivationFunction > actfunList)
sets the activation functions (and derivative and inversion too) for all layers.
Definition: multilayerffnn.cpp:356
std::vector< matrix::Matrix > zs
Definition: multilayerffnn.h:196
virtual ~MultiLayerFFNN()
Definition: multilayerffnn.h:46
virtual const matrix::Matrix response(const matrix::Matrix &input) const
response matrix of neural network at given input
Definition: multilayerffnn.cpp:168
virtual void damp(double damping)
damps the weights and the biases by multiplying (1-damping)
Definition: multilayerffnn.cpp:191
virtual void setSomeInternalParams(bool someInternalParams)
Definition: multilayerffnn.h:162
virtual Layer & getLayer(unsigned int layer)
layers 0 is the first hidden layer
Definition: multilayerffnn.h:116
std::vector< matrix::Matrix > ys
Definition: multilayerffnn.h:195
abstract class (interface) for feed forward rate based neural networks
Definition: feedforwardnn.h:41
virtual unsigned int getOutputDim() const
returns the number of output neurons
Definition: multilayerffnn.h:91
virtual const matrix::Matrix & getBias(unsigned int of_layer) const
Note: layers 0 is the first hidden layer.
Definition: multilayerffnn.h:134
bool useBypass
Definition: multilayerffnn.h:190
multi layer neural network with configurable activation functions
Definition: multilayerffnn.h:35
virtual unsigned int getLayerNum() const
Definition: multilayerffnn.h:105
double(* ActivationFunction)(double)
activation function type: input: membrane potential
Definition: feedforwardnn.h:34
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
virtual std::vector< ActivationFunction > setActivationFunction(ActivationFunction actfun)
sets the activation function (and derivative and inversion too) for ALL layers!
Definition: multilayerffnn.cpp:347
virtual const matrix::Matrix learn(const matrix::Matrix &input, const matrix::Matrix &nom_output, double learnRateFactor=1)
performs learning and returns the network output before learning (process should be called before) ...
Definition: multilayerffnn.cpp:104
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
virtual const matrix::Matrix inversion(const matrix::Matrix &input, const matrix::Matrix &xsi) const
calculates the input shift v to a given output shift xsi via pseudo inversion.
Definition: multilayerffnn.cpp:141
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
std::vector< Layer > layers
Definition: multilayerffnn.h:186
std::vector< matrix::Matrix > bias
Definition: multilayerffnn.h:188
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: multilayerffnn.cpp:338