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
controllernet.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 __CONTROLLERNET_H
25 #define __CONTROLLERNET_H
26 
27 #include <vector>
28 
29 #include "feedforwardnn.h"
30 #include "layer.h"
31 
32 /** multi layer neural network with configurable activation functions
33  and propagation and projection methods suitable for homeokinesis controller
34  */
35 class ControllerNet : public Configurable {
36 public:
37 
38  /**
39  @param layers Layer description (the input layer is not specified (always linear))
40  @param useBypass if true, then a connection from input to output layer is included
41  */
42  ControllerNet(const std::vector<Layer>& layers, bool useBypass=false);
43  virtual ~ControllerNet(){ }
44 
45  /** initialisation of the network with the given number of input and output units.
46  The dimensionality of the ouputlayer is automatically adjusted.
47  @param unit_map defines the approximate response of the network
48  after initialisation (if unit_map=1 the weights are unit matrices).
49  @param randGen pointer to random generator, if 0 an new one is used
50  */
51  virtual void init(unsigned int inputDim, unsigned int outputDim,
52  double unit_map = 0.0, double rand = 0.2, RandGen* randGen = 0);
53 
54  /** passive processing of the input.
55  This has to be done before calling reponse, and the back/forward propagation/projection functions.
56  The activations and the response matrix are stored internally.
57  */
58  virtual const matrix::Matrix process (const matrix::Matrix& input);
59 
60  /** like process just with the opportunity to overwrite the activation of
61  a specific layer
62  @param injections the input that is clamped at layer injectInLayer
63  @param injectInLayer the injection is clamped at this layer
64  */
65  virtual const matrix::Matrix processX (const matrix::Matrix& input,
66  const matrix::Matrix& injection,
67  unsigned int injectInLayer);
68 
69  /// damps the weights and the biases by multiplying (1-damping)
70  virtual void damp(double damping);
71 
72  /** response matrix of neural network (for current activation, see process)
73  \f[ J_ij = \frac{\partial y_i}{\partial x_j} \f]
74  \f[ J = G_n' W_n G_{n-1}' W_{n-1} ... G_1' W_1 \f]
75  with \f$W_n\f$ is the weight matrix of layer n and
76  \f$ G'\f$ is a diagonal matrix with \f$ G'_ii = g'_i \f$ as values on the diagonal.
77  */
78  virtual const matrix::Matrix& response() const;
79 
80  /** like response, just that only a range of layers is considered
81  The Bypass is not considered here.
82  @param from index of layer to start: -1 at input, 0 first hidden layer ...
83  @param to index of layer to stop: -1: last layer, 0 first hidden layer ...
84  */
85  virtual matrix::Matrix responsePart(int from, int to) const;
86 
87 
88  /** linear response matrix of neural network
89  \f[ R = W_n W_{n-1} ... W_1 \f]
90  with \f$W_n\f$ is the weight matrix of layer n.
91  */
92  virtual const matrix::Matrix& responseLinear() const;
93 
94  /** backpropagation of vector error through network.
95  The storage for the intermediate values (errors, zetas) do not need to be given.
96  The errors(layerwise) are at the output of the neurons
97  (index 0 is at the input level, output of layer 0 has index 1 and so on)
98  The zetas(layerwise) are the values inside the neurons that
99  arise when backpropagating the error signal. (zeta[0] is at layer 0)
100  @return errors[0] (result of backpropagation)
101  */
102  virtual const matrix::Matrix backpropagation(const matrix::Matrix& error,
103  matrix::Matrices* errors = 0,
104  matrix::Matrices* zetas = 0) const;
105 
106  /** like backpropagation but with special features: we can start from any layer
107  and the bypass-discounting can be used (see disseration Georg Martius)
108  WARNING: the errors and zetas above the `startWithLayer' are undefined
109  @param startWithLayer the error is clamped at this layer and the processing starts there
110  (-1: output layer)
111  @see backpropagation
112  */
113  virtual const matrix::Matrix backpropagationX(const matrix::Matrix& error,
114  matrix::Matrices* errors = 0,
115  matrix::Matrices* zetas = 0,
116  int startWithLayer = -1) const;
117 
118 
119  /** backprojection of vector error through network.
120  The storage for the intermediate values (errors, zetas) do not need to be given.
121  The errors(layerwise) are at the output of the neurons
122  (index 0 is at the input level, output of layer 0 has index 1 and so on)
123  The zetas(layerwise) are the values inside the neurons that
124  arise when backprojecting the error signal. (zeta[0] is at layer 0)
125  @return errors[0] (result of backprojecting)
126  */
127  virtual const matrix::Matrix backprojection(const matrix::Matrix& error,
128  matrix::Matrices* errors = 0,
129  matrix::Matrices* zetas = 0) const;
130 
131 
132  /** forwardpropagation of vector error through network.
133  The storage for the intermediate values (errors, zetas) do not need to be given.
134  The errors(layerwise) are at the output of the neurons
135  (index 0 is at the input level = error, output of layer 0 has index 1 and so on)
136  The zetas(layerwise) are the values inside the neurons that
137  arise when forwardpropagate the error signal. (zeta[0] is at layer 0)
138  @return errors[layernum] (result of forwardpropagation)
139  */
140  virtual const matrix::Matrix forwardpropagation(const matrix::Matrix& error,
141  matrix::Matrices* errors = 0,
142  matrix::Matrices* zetas = 0) const;
143 
144  /** forwardprojection of vector error through network.
145  The storage for the intermediate values (errors, zetas) do not need to be given.
146  The errors(layerwise) are at the output of the neurons
147  (index 0 is at the input level = error, output of layer 0 has index 1 and so on)
148  The zetas(layerwise) are the values inside the neurons that
149  arise when forwardprojecting the error signal. (zeta[0] is at layer 0)
150  @return errors[layernum] (result of forwardprojection)
151  */
152  virtual const matrix::Matrix forwardprojection(const matrix::Matrix& error,
153  matrix::Matrices* errors = 0,
154  matrix::Matrices* zetas = 0) const;
155 
156  /// returns the number of input neurons
157  virtual unsigned int getInputDim() const {
158  return weights[0].getN();
159  }
160  /// returns the number of output neurons
161  virtual unsigned int getOutputDim() const {
162  return (weights.rbegin())->getM();
163  }
164 
165  /** returns activation of the given layer. Layer 0 is the first hidden layer.
166  Negative values count from the end (-1 is the last layer)
167  */
168  virtual const matrix::Matrix& getLayerOutput(int layer) const {
169  if(layer<0) layer = layers.size() + layer;
170  assert(layer>=0 && layer < (int)layers.size());
171  return y[layer];
172  }
173 
174  // total number of layers (1 means no hidden units)
175  virtual unsigned int getLayerNum() const {
176  return layers.size();
177  }
178 
179  /// layers 0 is the first hidden layer
180  virtual const Layer& getLayer(unsigned int layer) const {
181  assert(layer < layers.size());
182  return layers[layer];
183  }
184 
185  /// layers 0 is the first hidden layer
186  virtual Layer& getLayer(unsigned int layer) {
187  assert(layer < layers.size());
188  return layers[layer];
189  }
190 
191  /** weight matrix 0 connects input with the first hidden layer
192  Negative values count from the end (-1 is the last layer)
193  */
194  virtual const matrix::Matrix& getWeights(int to_layer) const {
195  if(to_layer<0) to_layer = weights.size() - to_layer;
196  assert(to_layer>=0 && to_layer < (int)weights.size());
197  return weights[to_layer];
198  }
199 
200  /** weight matrix 0 connects input with the first hidden layer
201  Negative values count from the end (-1 is the last layer)
202  */
203  virtual matrix::Matrix& getWeights(int to_layer) {
204  if(to_layer<0) to_layer = weights.size() - to_layer;
205  assert(to_layer>=0 && to_layer < (int)weights.size());
206  return weights[to_layer];
207  }
208 
209  virtual const matrix::Matrix& getByPass() const {
210  assert(useBypass);
211  return bypassWeights;
212  }
213 
215  assert(useBypass);
216  return bypassWeights;
217  }
218 
219  /** Note: layers 0 is the first hidden layer
220  Negative values count from the end (-1 is the last layer)
221  */
222  virtual const matrix::Matrix& getBias(int of_layer) const {
223  if(of_layer<0) of_layer = bias.size() - of_layer;
224  assert(of_layer>=0 && of_layer < (int)bias.size());
225  return bias[of_layer];
226  }
227 
228  /** Note: layers 0 is the first hidden layer
229  Negative values count from the end (-1 is the last layer)
230  */
231  virtual matrix::Matrix& getBias(int of_layer) {
232  if(of_layer<0) of_layer = bias.size() - of_layer;
233  assert(of_layer>=0 && of_layer < (int)bias.size());
234  return bias[of_layer];
235  }
236 
237  /************** STOREABLE **********************************/
238  /// stores the layer binary into file stream
239  bool store(FILE* f) const;
240  /// restores the layer binary from file stream
241  bool restore(FILE* f);
242 
243  /// writes the layer ASCII into file stream (not in the storable interface)
244  bool write(FILE* f) const;
245 
246 protected:
247  // actually calculate the jacobian and stores it in L, see response()
248  virtual void calcResponseIntern();
249 
250 
251 protected:
252  std::vector<Layer> layers;
253  std::vector<matrix::Matrix> weights;
254  std::vector<matrix::Matrix> bias;
255  bool useBypass;
257 
258  /*** storage variables ****/
259  ///
261  matrix::Matrices y; // activations
262  matrix::Matrices z; // potentials
264 
265  matrix::Matrix L; // jacobian (or response) matrix
266  matrix::Matrix R; // linearized jacobian matrix
267 
268  double lambda; // regularisation value for pseudoinverse
270 };
271 
272 #endif
Matrix type.
Definition: matrix.h:65
virtual const matrix::Matrix backpropagation(const matrix::Matrix &error, matrix::Matrices *errors=0, matrix::Matrices *zetas=0) const
backpropagation of vector error through network.
Definition: controllernet.cpp:237
Definition: layer.h:31
virtual const matrix::Matrix backprojection(const matrix::Matrix &error, matrix::Matrices *errors=0, matrix::Matrices *zetas=0) const
backprojection of vector error through network.
Definition: controllernet.cpp:330
virtual unsigned int getInputDim() const
returns the number of input neurons
Definition: controllernet.h:157
virtual matrix::Matrix responsePart(int from, int to) const
like response, just that only a range of layers is considered The Bypass is not considered here...
Definition: controllernet.cpp:405
matrix::Matrices z
Definition: controllernet.h:262
I size() const
Definition: matrix.h:92
bool useBypass
Definition: controllernet.h:255
bool restore(FILE *f)
restores the layer binary from file stream
Definition: controllernet.cpp:473
bool initialised
Definition: controllernet.h:269
virtual void init(unsigned int inputDim, unsigned int outputDim, double unit_map=0.0, double rand=0.2, RandGen *randGen=0)
initialisation of the network with the given number of input and output units.
Definition: controllernet.cpp:42
virtual const matrix::Matrix & getLayerOutput(int layer) const
returns activation of the given layer.
Definition: controllernet.h:168
virtual const matrix::Matrix & getByPass() const
Definition: controllernet.h:209
matrix::Matrix L
Definition: controllernet.h:265
virtual unsigned int getOutputDim() const
returns the number of output neurons
Definition: controllernet.h:161
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual const matrix::Matrix forwardpropagation(const matrix::Matrix &error, matrix::Matrices *errors=0, matrix::Matrices *zetas=0) const
forwardpropagation of vector error through network.
Definition: controllernet.cpp:139
std::vector< matrix::Matrix > bias
Definition: controllernet.h:254
virtual const matrix::Matrix & response() const
response matrix of neural network (for current activation, see process) with is the weight matrix ...
Definition: controllernet.cpp:374
virtual const matrix::Matrix backpropagationX(const matrix::Matrix &error, matrix::Matrices *errors=0, matrix::Matrices *zetas=0, int startWithLayer=-1) const
like backpropagation but with special features: we can start from any layer and the bypass-discountin...
Definition: controllernet.cpp:266
bool write(FILE *f) const
writes the layer ASCII into file stream (not in the storable interface)
Definition: controllernet.cpp:454
virtual const matrix::Matrix & getBias(int of_layer) const
Note: layers 0 is the first hidden layer Negative values count from the end (-1 is the last layer) ...
Definition: controllernet.h:222
matrix::Matrix input
Definition: controllernet.h:260
virtual Layer & getLayer(unsigned int layer)
layers 0 is the first hidden layer
Definition: controllernet.h:186
virtual void calcResponseIntern()
Definition: controllernet.cpp:384
matrix::Matrices gp
Definition: controllernet.h:263
virtual unsigned int getLayerNum() const
Definition: controllernet.h:175
ControllerNet(const std::vector< Layer > &layers, bool useBypass=false)
Definition: controllernet.cpp:33
double lambda
Definition: controllernet.h:268
virtual ~ControllerNet()
Definition: controllernet.h:43
matrix::Matrix R
Definition: controllernet.h:266
virtual const matrix::Matrix & getWeights(int to_layer) const
weight matrix 0 connects input with the first hidden layer Negative values count from the end (-1 is ...
Definition: controllernet.h:194
virtual const matrix::Matrix process(const matrix::Matrix &input)
passive processing of the input.
Definition: controllernet.cpp:85
virtual matrix::Matrix & getWeights(int to_layer)
weight matrix 0 connects input with the first hidden layer Negative values count from the end (-1 is ...
Definition: controllernet.h:203
virtual matrix::Matrix & getBias(int of_layer)
Note: layers 0 is the first hidden layer Negative values count from the end (-1 is the last layer) ...
Definition: controllernet.h:231
Abstact class for configurable objects.
Definition: configurable.h:81
virtual void damp(double damping)
damps the weights and the biases by multiplying (1-damping)
Definition: controllernet.cpp:423
matrix::Matrix bypassWeights
Definition: controllernet.h:256
std::vector< Matrix > Matrices
Definition: matrix.h:40
virtual matrix::Matrix & getByPass()
Definition: controllernet.h:214
virtual const matrix::Matrix & responseLinear() const
linear response matrix of neural network with is the weight matrix of layer n.
Definition: controllernet.cpp:379
std::vector< Layer > layers
Definition: controllernet.h:252
std::vector< matrix::Matrix > weights
Definition: controllernet.h:253
bool store(FILE *f) const
stores the layer binary into file stream
Definition: controllernet.cpp:436
matrix::Matrices y
Definition: controllernet.h:261
multi layer neural network with configurable activation functions and propagation and projection meth...
Definition: controllernet.h:35
virtual const matrix::Matrix forwardprojection(const matrix::Matrix &error, matrix::Matrices *errors=0, matrix::Matrices *zetas=0) const
forwardprojection of vector error through network.
Definition: controllernet.cpp:197
virtual const matrix::Matrix processX(const matrix::Matrix &input, const matrix::Matrix &injection, unsigned int injectInLayer)
like process just with the opportunity to overwrite the activation of a specific layer ...
Definition: controllernet.cpp:108
virtual const Layer & getLayer(unsigned int layer) const
layers 0 is the first hidden layer
Definition: controllernet.h:180