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 
iCub::ctrl::ff2LayNN::minmax::min
double min
Definition: neuralNetworks.h:52
iCub::ctrl::ff2LayNN::outMinY
yarp::sig::Vector outMinY
Definition: neuralNetworks.h:77
iCub::ctrl::ff2LayNN::minmax
Definition: neuralNetworks.h:50
iCub
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
Definition: emotionInterface.h:17
iCub::ctrl::ff2LayNN
Definition: neuralNetworks.h:47
iCub::ctrl::ff2LayNN::configured
bool configured
Definition: neuralNetworks.h:80
iCub::ctrl::ff2LayNN::inMinX
yarp::sig::Vector inMinX
Definition: neuralNetworks.h:72
iCub::ctrl::ff2LayNN::printStructure
virtual bool printStructure(std::ostream &stream=std::cout) const
Dump tadily the network structure on the stream.
Definition: neuralNetworks.cpp:329
iCub::ctrl::ff2LayNN::outputLayerFcn
virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const =0
Output Layer Function.
iCub::ctrl::ff2LayNN_tansig_purelin::hiddenLayerGrad
virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const
Gradient of the Hidden Layer Function.
Definition: neuralNetworks.cpp:392
iCub::ctrl::ff2LayNN::outMinX
yarp::sig::Vector outMinX
Definition: neuralNetworks.h:76
iCub::ctrl::ff2LayNN_tansig_purelin::ff2LayNN_tansig_purelin
ff2LayNN_tansig_purelin()
Create an empty network.
Definition: neuralNetworks.cpp:360
iCub::ctrl::ff2LayNN::b2
yarp::sig::Vector b2
Definition: neuralNetworks.h:62
iCub::ctrl::ff2LayNN::hiddenLayerGrad
virtual yarp::sig::Vector hiddenLayerGrad(const yarp::sig::Vector &x) const =0
Gradient of the Hidden Layer Function.
iCub::ctrl::ff2LayNN::b1
yarp::sig::Vector b1
Definition: neuralNetworks.h:58
iCub::ctrl::ff2LayNN::inMinMaxY
std::deque< minmax > inMinMaxY
Definition: neuralNetworks.h:66
iCub::ctrl::ff2LayNN::outputLayerGrad
virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const =0
Gradient of the Output Layer Function.
iCub::ctrl::ff2LayNN::outMinMaxY
std::deque< minmax > outMinMaxY
Definition: neuralNetworks.h:70
iCub::ctrl::ff2LayNN::minmax::max
double max
Definition: neuralNetworks.h:53
iCub::ctrl::ff2LayNN::get_LW
std::deque< yarp::sig::Vector > & get_LW()
Retrieve second layer weights.
Definition: neuralNetworks.h:183
iCub::ctrl::ff2LayNN_tansig_purelin::hiddenLayerFcn
virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const
Hidden Layer Function.
Definition: neuralNetworks.cpp:374
iCub::ctrl::ff2LayNN::scaleOutputToNetFormat
virtual yarp::sig::Vector scaleOutputToNetFormat(const yarp::sig::Vector &x) const
Scale output to be used with the network.
Definition: neuralNetworks.cpp:218
iCub::ctrl::ff2LayNN::outMinMaxX
std::deque< minmax > outMinMaxX
Definition: neuralNetworks.h:69
math.h
iCub::ctrl::ff2LayNN::scaleOutputFromNetFormat
virtual yarp::sig::Vector scaleOutputFromNetFormat(const yarp::sig::Vector &x) const
Scale back output from the network's format.
Definition: neuralNetworks.cpp:225
iCub::ctrl::ff2LayNN_tansig_purelin::outputLayerFcn
virtual yarp::sig::Vector outputLayerFcn(const yarp::sig::Vector &x) const
Output Layer Function.
Definition: neuralNetworks.cpp:385
iCub::ctrl::ff2LayNN::getItem
bool getItem(const yarp::os::Property &options, const std::string &tag, yarp::sig::Vector &item) const
Definition: neuralNetworks.cpp:58
iCub::ctrl::ff2LayNN::LW
std::deque< yarp::sig::Vector > LW
Definition: neuralNetworks.h:61
iCub::ctrl::ff2LayNN::get_b1
yarp::sig::Vector & get_b1()
Retrieve first layer bias.
Definition: neuralNetworks.h:189
iCub::ctrl::ff2LayNN::inMinY
yarp::sig::Vector inMinY
Definition: neuralNetworks.h:73
iCub::ctrl::ff2LayNN_tansig_purelin::outputLayerGrad
virtual yarp::sig::Vector outputLayerGrad(const yarp::sig::Vector &x) const
Gradient of the Output Layer Function.
Definition: neuralNetworks.cpp:407
iCub::ctrl::ff2LayNN::scaleInputToNetFormat
virtual yarp::sig::Vector scaleInputToNetFormat(const yarp::sig::Vector &x) const
Scale input to be used with the network.
Definition: neuralNetworks.cpp:204
iCub::ctrl::ff2LayNN::hiddenLayerFcn
virtual yarp::sig::Vector hiddenLayerFcn(const yarp::sig::Vector &x) const =0
Hidden Layer Function.
iCub::ctrl::ff2LayNN::scaleInputFromNetFormat
virtual yarp::sig::Vector scaleInputFromNetFormat(const yarp::sig::Vector &x) const
Scale back input from the network's format.
Definition: neuralNetworks.cpp:211
x
x
Definition: compute_ekf_sym.m:21
iCub::ctrl::ff2LayNN::get_b2
yarp::sig::Vector & get_b2()
Retrieve second layer bias.
Definition: neuralNetworks.h:195
iCub::ctrl::ff2LayNN::predict
virtual yarp::sig::Vector predict(const yarp::sig::Vector &x) const
Predict the output given a certain input to the network.
Definition: neuralNetworks.cpp:232
string
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}") include_directories($
Definition: CMakeLists.txt:9
iCub::ctrl::ff2LayNN::prepare
void prepare()
Definition: neuralNetworks.cpp:74
iCub::ctrl::ff2LayNN_tansig_purelin
Definition: neuralNetworks.h:261
iCub::ctrl::ff2LayNN::IW
std::deque< yarp::sig::Vector > IW
Definition: neuralNetworks.h:57
iCub::ctrl::ff2LayNN::getStructure
virtual bool getStructure(yarp::os::Property &options) const
Retrieve the network structure as a Property object.
Definition: neuralNetworks.cpp:260
iCub::ctrl::ff2LayNN::setItem
void setItem(yarp::os::Property &options, const std::string &tag, const yarp::sig::Vector &item) const
Definition: neuralNetworks.cpp:47
iCub::ctrl::ff2LayNN::get_IW
std::deque< yarp::sig::Vector > & get_IW()
Retrieve first layer weights.
Definition: neuralNetworks.h:177
iCub::ctrl::ff2LayNN::ff2LayNN
ff2LayNN()
Create an empty network.
Definition: neuralNetworks.cpp:26
iCub::ctrl::ff2LayNN::configure
virtual bool configure(const yarp::os::Property &options)
Configure/reconfigure the network.
Definition: neuralNetworks.cpp:101
iCub::ctrl::ff2LayNN::inRatio
yarp::sig::Vector inRatio
Definition: neuralNetworks.h:74
iCub::ctrl::ff2LayNN::isValid
virtual bool isValid() const
Return the internal status after a configuration.
Definition: neuralNetworks.cpp:40
iCub::ctrl::ff2LayNN::inMinMaxX
std::deque< minmax > inMinMaxX
Definition: neuralNetworks.h:65
iCub::ctrl::ff2LayNN::outRatio
yarp::sig::Vector outRatio
Definition: neuralNetworks.h:78