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
esn.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * FIAS winter school, playful machine group *
3  * Supervisors: Ralf Der, Georg Martius *
4 
5  * Members: Fabien Benureau, Chrisantha Fernando, Quan Wang, Jimmy Baraglia *
6  * Echo State Network Header File *
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 __ESN_H
25 #define __ESN_H
26 
27 
28 #include <stdio.h>
29 #include <cmath>
30 #include <selforg/invertablemodel.h>
31 #include <selforg/matrix.h>
32 
33 
34 struct ESNConf {
35  int numNeurons; ///< number of neurons in the reservoir
36  double inputStrength; ///< strength of input to reservoir connections
37  double inputRatio; ///< ratio of input connections w.r.t full connectivity
38  double connectionRatio; ///< ratio of internal connections w.r.t full connectivity
39  double spectralRadius; ///< largest eigenvalue of internal weights
40  double learningrate;
41  /// switch on to get the internal weights and activations inspectabe
43 };
44 
45 
46 /**
47  * class for robot control with sine, sawtooth and impuls
48  *
49  * period is the length of the period in steps and
50  * phaseshift is the phase difference between channels given in Pi/2
51  */
52 class ESN : public InvertableModel {
53 public:
54 
55  /**
56  @param controlmask bitmask to select channels to control (default all)
57  @param function controller function to use
58  */
59  ESN(const ESNConf& conf = getDefaultConf());
60 
62  ESNConf c;
63  c.numNeurons = 100;
64  c.inputStrength = 0.1;
65  c.inputRatio = 1;
66  c.connectionRatio = 0.1;
67  c.spectralRadius = 0.9;
68  c.inspectInternals = false;
69  c.learningrate=0.01;
70  return c;
71  }
72 
73  /** initialisation of the network with the given number of input and output units
74  @param inputDim length of input vector
75  @param outputDim length of output vector
76  @param unit_map if 0 the parametes are choosen randomly.
77  Otherwise the model is initialised to represent a unit_map
78  with the given response strength.
79  @param randGen pointer to random generator, if 0 an new one is used
80  */
81  virtual void init(unsigned int inputDim, unsigned int outputDim,
82  double unit_map = 0.0, RandGen* randGen = 0);
83 
84  /** passive processing of the input
85  (this function is not constant since a recurrent network
86  for example might change internal states
87  */
88  virtual const matrix::Matrix process (const matrix::Matrix& input);
89 
90  /* performs learning and returns the network output before learning.
91  Neural networks process the input before. (no need to call process before)
92  \param learnRateFactor can be given to modify eps for this learning step.
93  */
94  virtual const matrix::Matrix learn (const matrix::Matrix& input,
95  const matrix::Matrix& nom_output,
96  double learnRateFactor = 1);
97 
98  /* calculates the partial derivative of the of the output with repect to the input (Jacobi matrix).
99 
100  \f[J_{ij} = \frac{\partial output_i}{\partial input_j}\f]
101 
102  The input is ignored, the network must be processed or learned before!
103  */
104  virtual const matrix::Matrix response(const matrix::Matrix& _input) const;
105 
106  /* calculates the input shift v to given output shift xsi via pseudo inversion.
107 
108  \f[o+\xi = \pi(i+v)\f]
109 
110  The input is ignored, the network must be processed or learned before!
111  */
112  virtual const matrix::Matrix inversion(const matrix::Matrix& input, const matrix::Matrix& xsi) const;
113 
114  /// damps the weights and the biases by multiplying (1-damping)
115  virtual void damp(double damping);
116 
117  /// returns the number of input neurons
118  virtual unsigned int getInputDim() const;
119  /// returns the number of output neurons
120  virtual unsigned int getOutputDim() const;
121 
122  virtual bool store(FILE*) const;
123 
124  virtual bool restore(FILE*);
125 
126 
127  static double tanh_prime(double z)
128  {
129  double k=tanh(z);
130  return 1.0 - k*k;
131  };
132 
133 protected:
134 
135  ESNConf conf;
136 
137  int nbInputs;
145  double error;
147 
148  //
149 };
150 
151 #endif
152 
virtual const matrix::Matrix response(const matrix::Matrix &_input) const
calculates the partial derivative of the of the output with repect to the input (Jacobi matrix)...
Definition: esn.cpp:191
Matrix type.
Definition: matrix.h:65
abstract class (interface) for invertable models.
Definition: invertablemodel.h:33
matrix::Matrix ESNWeights
Definition: esn.h:144
static ESNConf getDefaultConf()
Definition: esn.h:61
virtual bool restore(FILE *)
loads the object from the given file stream (ASCII preferred).
Definition: esn.cpp:230
virtual const matrix::Matrix learn(const matrix::Matrix &input, const matrix::Matrix &nom_output, double learnRateFactor=1)
Definition: esn.cpp:120
double spectralRadius
largest eigenvalue of internal weights
Definition: esn.h:39
double error
Definition: esn.h:145
int nbInputs
Definition: esn.h:137
double learningrate
Definition: esn.h:40
virtual unsigned int getOutputDim() const
returns the number of output neurons
Definition: esn.cpp:211
virtual void damp(double damping)
damps the weights and the biases by multiplying (1-damping)
Definition: esn.cpp:202
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: esn.cpp:52
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
matrix::Matrix ESNState
Definition: esn.h:142
double connectionRatio
ratio of internal connections w.r.t full connectivity
Definition: esn.h:38
double inputRatio
ratio of input connections w.r.t full connectivity
Definition: esn.h:37
virtual const matrix::Matrix inversion(const matrix::Matrix &input, const matrix::Matrix &xsi) const
calculates the input shift v to given output shift xsi via pseudo inversion.
Definition: esn.cpp:196
matrix::Matrix outputDirectWeights
Definition: esn.h:141
ESNConf conf
Definition: esn.h:131
matrix::Matrix inputWeights
Definition: esn.h:139
int numNeurons
number of neurons in the reservoir
Definition: esn.h:35
virtual bool store(FILE *) const
stores the object to the given file stream (ASCII preferred).
Definition: esn.cpp:217
bool initialized
Definition: esn.h:146
virtual unsigned int getInputDim() const
returns the number of input neurons
Definition: esn.cpp:206
ESN(const ESNConf &conf=getDefaultConf())
ESN class constructor.
Definition: esn.cpp:35
matrix::Matrix outputWeights
Definition: esn.h:140
class for robot control with sine, sawtooth and impuls
Definition: esn.h:52
Definition: esn.h:34
static double tanh_prime(double z)
Definition: esn.h:127
matrix::Matrix ESNActivations
Definition: esn.h:143
double inputStrength
strength of input to reservoir connections
Definition: esn.h:36
int c
Definition: hexapod.cpp:56
int nbOutputs
Definition: esn.h:138
bool inspectInternals
switch on to get the internal weights and activations inspectabe
Definition: esn.h:42
virtual const matrix::Matrix process(const matrix::Matrix &input)
passive processing of the input (this function is not constant since a recurrent network for example ...
Definition: esn.cpp:105