iCub-main
neuralNetworks.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms
7  * of the BSD-3-Clause license. See the accompanying LICENSE file for
8  * details.
9 */
10 
22 #ifndef __NEURALNETWORKS_H__
23 #define __NEURALNETWORKS_H__
24 
25 #include <iostream>
26 #include <string>
27 #include <deque>
28 
29 #include <yarp/os/Property.h>
30 #include <yarp/sig/Vector.h>
31 #include <iCub/ctrl/math.h>
32 
33 
34 namespace iCub
35 {
36 
37 namespace ctrl
38 {
39 
47 class ff2LayNN
48 {
49 protected:
50  struct minmax
51  {
52  double min;
53  double max;
54  };
55 
56  // 1st layer data
57  std::deque<yarp::sig::Vector> IW;
58  yarp::sig::Vector b1;
59 
60  // 2nd layer data
61  std::deque<yarp::sig::Vector> LW;
62  yarp::sig::Vector b2;
63 
64  // process input
65  std::deque<minmax> inMinMaxX;
66  std::deque<minmax> inMinMaxY;
67 
68  // process output
69  std::deque<minmax> outMinMaxX;
70  std::deque<minmax> outMinMaxY;
71 
72  yarp::sig::Vector inMinX;
73  yarp::sig::Vector inMinY;
74  yarp::sig::Vector inRatio;
75 
76  yarp::sig::Vector outMinX;
77  yarp::sig::Vector outMinY;
78  yarp::sig::Vector outRatio;
79 
80  bool configured;
81 
82  void prepare();
83  void setItem(yarp::os::Property &options, const std::string &tag, const yarp::sig::Vector &item) const;
84  bool getItem(const yarp::os::Property &options, const std::string &tag, yarp::sig::Vector &item) const;
85 
86 public:
90  ff2LayNN();
91 
98  ff2LayNN(const yarp::os::Property &options);
99 
143  virtual bool configure(const yarp::os::Property &options);
144 
149  virtual bool isValid() const;
150 
156  virtual yarp::sig::Vector predict(const yarp::sig::Vector &x) const;
157 
164  virtual bool getStructure(yarp::os::Property &options) const;
165 
171  virtual bool printStructure(std::ostream &stream=std::cout) const;
172 
177  std::deque<yarp::sig::Vector> &get_IW() { return IW; }
178 
183  std::deque<yarp::sig::Vector> &get_LW() { return LW; }
184 
189  yarp::sig::Vector &get_b1() { return b1; }
190 
195  yarp::sig::Vector &get_b2() { return b2; }
196 
202  virtual yarp::sig::Vector scaleInputToNetFormat(const yarp::sig::Vector &x) const;
203 
209  virtual yarp::sig::Vector scaleInputFromNetFormat(const yarp::sig::Vector &x) const;
210 
216  virtual yarp::sig::Vector scaleOutputToNetFormat(const yarp::sig::Vector &x) const;
217 
223  virtual yarp::sig::Vector scaleOutputFromNetFormat(const yarp::sig::Vector &x) const;
224 
230  virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const=0;
231 
237  virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const=0;
238 
244  virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const=0;
245 
251  virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const=0;
252 };
253 
254 
261 class ff2LayNN_tansig_purelin : virtual public ff2LayNN
262 {
263 public:
268 
275  ff2LayNN_tansig_purelin(const yarp::os::Property &options);
276 
282  virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const;
283 
289  virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const;
290 
296  virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const;
297 
303  virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const;
304 };
305 
306 }
307 
308 }
309 
310 
311 #endif
312 
313 
314 
Feed-Forward 2 layers Neural Network with a tansig function for the hidden nodes and a purelin for th...
virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const
Gradient of the Hidden Layer Function.
ff2LayNN_tansig_purelin()
Create an empty network.
virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const
Gradient of the Output Layer Function.
virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const
Output Layer Function.
ff2LayNN_tansig_purelin(const yarp::os::Property &options)
Create and configure the network.
virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const
Hidden Layer Function.
Feed-Forward 2 layers Neural Network.
virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const =0
Hidden Layer Function.
yarp::sig::Vector outRatio
virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const =0
Gradient of the Hidden Layer Function.
virtual yarp::sig::Vector scaleOutputToNetFormat(const yarp::sig::Vector &x) const
Scale output to be used with the network.
yarp::sig::Vector inRatio
virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const =0
Gradient of the Output Layer Function.
yarp::sig::Vector b2
yarp::sig::Vector b1
void setItem(yarp::os::Property &options, const std::string &tag, const yarp::sig::Vector &item) const
bool getItem(const yarp::os::Property &options, const std::string &tag, yarp::sig::Vector &item) const
yarp::sig::Vector & get_b1()
Retrieve first layer bias.
yarp::sig::Vector & get_b2()
Retrieve second layer bias.
virtual bool isValid() const
Return the internal status after a configuration.
std::deque< minmax > inMinMaxX
virtual bool printStructure(std::ostream &stream=std::cout) const
Dump tadily the network structure on the stream.
yarp::sig::Vector outMinY
std::deque< minmax > inMinMaxY
std::deque< yarp::sig::Vector > & get_IW()
Retrieve first layer weights.
yarp::sig::Vector inMinY
ff2LayNN()
Create an empty network.
std::deque< yarp::sig::Vector > IW
virtual yarp::sig::Vector scaleOutputFromNetFormat(const yarp::sig::Vector &x) const
Scale back output from the network's format.
virtual yarp::sig::Vector scaleInputToNetFormat(const yarp::sig::Vector &x) const
Scale input to be used with the network.
yarp::sig::Vector inMinX
std::deque< minmax > outMinMaxX
virtual bool configure(const yarp::os::Property &options)
Configure/reconfigure the network.
virtual yarp::sig::Vector predict(const yarp::sig::Vector &x) const
Predict the output given a certain input to the network.
std::deque< minmax > outMinMaxY
yarp::sig::Vector outMinX
virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const =0
Output Layer Function.
std::deque< yarp::sig::Vector > LW
virtual bool getStructure(yarp::os::Property &options) const
Retrieve the network structure as a Property object.
virtual yarp::sig::Vector scaleInputFromNetFormat(const yarp::sig::Vector &x) const
Scale back input from the network's format.
std::deque< yarp::sig::Vector > & get_LW()
Retrieve second layer weights.
ff2LayNN(const yarp::os::Property &options)
Create and configure the network.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.