learningalgorithm.h
1 /********************************************************************************
2  * Neural Network Framework. *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef LEARNINGALGORITHM_H
21 #define LEARNINGALGORITHM_H
22 
23 #include "nnfwconfig.h"
24 #include "neuralnet.h"
25 #include <QMap>
26 #include <QVector>
27 #include <cmath>
28 #include <parametersettable.h>
30 
31 namespace farsa {
32 
33 class NeuralNet;
34 
60 class FARSA_NNFW_API Pattern : public ParameterSettableWithConfigureFunction {
61 public:
62  class PatternInfo {
63  public:
64  DoubleVector inputs;
65  DoubleVector outputs;
66  };
68  Pattern() : ParameterSettableWithConfigureFunction(), pinfo() { /*nothing to do*/ };
70  ~Pattern() { /*nothing to do*/ };
72  void setInputsOf( Cluster*, const DoubleVector& );
74  void setOutputsOf( Cluster*, const DoubleVector& );
76  void setInputsOutputsOf( Cluster*, const DoubleVector& inputs, const DoubleVector& outputs );
78  const DoubleVector& inputsOf( Cluster* ) const;
80  const DoubleVector& outputsOf( Cluster* ) const;
83  PatternInfo& operator[]( Cluster* );
109  virtual void configure(ConfigurationParameters& params, QString prefix);
117  virtual void save(ConfigurationParameters& params, QString prefix);
119  static void describe( QString type );
120 private:
121  mutable QMap<Cluster*, PatternInfo> pinfo;
122 };
123 
131 typedef QVector<Pattern> PatternSet;
132 
138 public:
144  virtual ~LearningAlgorithm();
146  void setNeuralNet( NeuralNet* net ) {
147  netp = net;
148  this->neuralNetChanged();
149  };
152  return netp;
153  };
155  virtual void learn() = 0;
157  virtual void learn( const Pattern& ) = 0;
159  virtual void learnOnSet( const PatternSet& set ) {
160  for( int i=0; i<(int)set.size(); i++ ) {
161  learn( set[i] );
162  }
163  };
165  virtual double calculateMSE( const Pattern& ) = 0;
167  virtual double calculateMSEOnSet( const PatternSet& set ) {
168  double mseacc = 0.0;
169  int dim = (int)set.size();
170  for( int i=0; i<dim; i++ ) {
171  mseacc += calculateMSE( set[i] );
172  }
173  return mseacc/dim;
174  };
176  double calculateRMSD( const Pattern& p ) {
177  return sqrt( calculateMSE( p ) );
178  };
180  double calculateRMSDOnSet( const PatternSet& p ) {
181  return sqrt( calculateMSEOnSet( p ) );
182  };
184  PatternSet loadPatternSet( ConfigurationParameters& params, QString path, QString prefix );
186  void savePatternSet( PatternSet& set, ConfigurationParameters& params, QString prefix );
187 protected:
189  virtual void neuralNetChanged() = 0;
190 private:
191  NeuralNet* netp;
192 };
193 
194 }
195 
196 #endif
197 
This file contains the common type defitions used on the whole framework.
~Pattern()
Destructor.
double calculateRMSDOnSet(const PatternSet &p)
Calculate the Root Mean Square Deviation, i.e.
double calculateRMSD(const Pattern &p)
Calculate the Root Mean Square Deviation, i.e.
virtual double calculateMSEOnSet(const PatternSet &set)
Calculate the Mean Square Error respect to all Patterns passed.
Define the common interface among Clusters.
Definition: cluster.h:73
Pattern()
Construct an empty Pattern.
void setNeuralNet(NeuralNet *net)
Set the NeuralNet to learn.
LearningAlgorithm object.
NeuralNet * neuralNet()
Return the NeuralNet setted.
The Neural Network Class.
Definition: neuralnet.h:221
Pattern object.
This file contains the declaration of Neural Network Class.
virtual void learnOnSet(const PatternSet &set)
Modify the object tring to learn all patterns present into PatternSet passed.