learningalgorithm.h
Go to the documentation of this file.
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 
26 #include "nnfwconfig.h"
27 #include "neuralnet.h"
28 #include <QMap>
29 #include <QVector>
30 #include <cmath>
31 #include <parametersettable.h>
32 #include <configurationparameters.h>
33 
34 namespace farsa {
35 
36 class NeuralNet;
37 
66 class FARSA_NNFW_API Pattern : public ParameterSettableWithConfigureFunction {
67 public:
68  class PatternInfo {
69  public:
70  DoubleVector inputs;
71  DoubleVector outputs;
72  };
74  Pattern() : ParameterSettableWithConfigureFunction(), pinfo() { /*nothing to do*/ };
76  ~Pattern() { /*nothing to do*/ };
78  void setInputsOf( Cluster*, const DoubleVector& );
80  void setOutputsOf( Cluster*, const DoubleVector& );
82  void setInputsOutputsOf( Cluster*, const DoubleVector& inputs, const DoubleVector& outputs );
84  DoubleVector inputsOf( Cluster* ) const;
86  DoubleVector outputsOf( Cluster* ) const;
89  PatternInfo& operator[]( Cluster* );
115  virtual void configure(ConfigurationParameters& params, QString prefix);
123  virtual void save(ConfigurationParameters& params, QString prefix);
125  static void describe( QString type );
126 private:
127  mutable QMap<Cluster*, PatternInfo> pinfo;
128 };
129 
137 typedef QVector<Pattern> PatternSet;
138 
144 public:
150  virtual ~LearningAlgorithm();
152  void setNeuralNet( NeuralNet* net ) {
153  netp = net;
154  this->neuralNetChanged();
155  };
158  return netp;
159  };
161  virtual void learn() = 0;
163  virtual void learn( const Pattern& ) = 0;
165  virtual void learnOnSet( const PatternSet& set ) {
166  for( int i=0; i<(int)set.size(); i++ ) {
167  learn( set[i] );
168  }
169  };
171  virtual double calculateMSE( const Pattern& ) = 0;
173  virtual double calculateMSEOnSet( const PatternSet& set ) {
174  double mseacc = 0.0;
175  int dim = (int)set.size();
176  for( int i=0; i<dim; i++ ) {
177  mseacc += calculateMSE( set[i] );
178  }
179  return mseacc/dim;
180  };
182  double calculateRMSD( const Pattern& p ) {
183  return sqrt( calculateMSE( p ) );
184  };
186  double calculateRMSDOnSet( const PatternSet& p ) {
187  return sqrt( calculateMSEOnSet( p ) );
188  };
190  PatternSet loadPatternSet( ConfigurationParameters& params, QString path, QString prefix );
192  void savePatternSet( PatternSet& set, ConfigurationParameters& params, QString prefix );
193 protected:
195  virtual void neuralNetChanged() = 0;
196 private:
197  NeuralNet* netp;
198 };
199 
200 }
201 
202 #endif
203