biasedcluster.cpp
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 #include "biasedcluster.h"
21 #include "liboutputfunctions.h"
22 #include "randomgenerator.h"
23 #include "configurationhelper.h"
24 
25 using namespace Eigen;
26 
27 namespace farsa {
28 
29 BiasedCluster::BiasedCluster( unsigned int numNeurons, QString name ) :
30  Cluster( numNeurons, name),
31  biasesdata(VectorXd::Zero(numNeurons)),
32  tempdata(VectorXd::Zero(numNeurons)) {
33  //--- create the delegate for biases
34  setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
35 }
36 
38  Cluster( params, prefix ),
39  biasesdata(VectorXd::Zero(numNeurons())),
40  tempdata(VectorXd::Zero(numNeurons())) {
41  //--- create the delegate for biases
42  setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
43 
44  QString vectorSizeErrorTmpl( "The number of elements of the %1 vector in configuration file (%1) is different from the number of neurons (%2)");
45  // biases is a vector, that is a list of space-separated values
46  QVector<double> vect = ConfigurationHelper::getVector( params, prefix + "biases" );
47 #ifdef FARSA_DEBUG
48  if ( !vect.isEmpty() && vect.size() != (int)numNeurons() ) {
49  qWarning() << vectorSizeErrorTmpl.arg( "biases" ).arg( vect.size() ).arg( numNeurons() );
50  }
51 #endif
52  biasesdata = Map<VectorXd>(vect.data(), numNeurons());
53 }
54 
56 }
57 
59  tempdata = inputs()-biasesdata;
60  outFunction()->apply( tempdata, outputs() );
61  setNeedReset( true );
62 }
63 
64 void BiasedCluster::setBias( unsigned int neuron, double bias ) {
65  biasesdata[neuron] = bias;
66 }
67 
68 void BiasedCluster::setAllBiases( double bias ) {
69  biasesdata.setConstant( bias );
70 }
71 
72 void BiasedCluster::setBiases( const DoubleVector& bias ) {
73  biasesdata = bias;
74 }
75 
76 double BiasedCluster::getBias( unsigned int neuron ) {
77  return biasesdata[neuron];
78 }
79 
80 void BiasedCluster::randomize( double min, double max ) {
81  for ( unsigned int i = 0; i < numNeurons(); i++ ) {
82  biasesdata[i] = globalRNG->getDouble( min, max );
83  }
84 }
85 
86 void BiasedCluster::save(ConfigurationParameters& params, QString prefix)
87 {
88  Cluster::save( params, prefix );
89  params.startObjectParameters(prefix, "BiasedCluster", this);
90  // First creating a string list, then transforming to a single string
91  QStringList list;
92  for (int i = 0; i < biasesdata.size(); i++) {
93  list.push_back(QString::number(biasesdata[i]));
94  }
95  params.createParameter(prefix, "biases", list.join(" "));
96 }
97 
98 void BiasedCluster::describe( QString type ) {
99  Cluster::describe( type );
100  Descriptor d = addTypeDescription( type, "A Cluster where neurons have also a bias value", "The bias values are subtracted from the input values before the calculation of the output" );
101  d.describeReal( "biases" ).props( IsList ).help( "The vector of bias values. It must contains numNeurons elements" );
102 }
103 
104 }
static QVector< double > getVector(ConfigurationParameters &params, QString paramPath, QString def=QString())
FARSA_UTIL_API RandomGenerator * globalRNG
virtual void apply(DoubleVector &inputs, DoubleVector &outputs)=0
Calculate the outputs of neurons by the net inputs given.
void setBiases(const DoubleVector &biases)
Set the biases from the vector given.
Library of Common OutputFunction.
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
DoubleVector & inputs()
Get the array of inputs.
Definition: cluster.h:122
void setBias(unsigned int neuron, double bias)
Set the bias of the neuron.
static void describe(QString type)
Add to Factory::typeDescriptions() the descriptions of all parameters and subgroups.
Define the common interface among Clusters.
Definition: cluster.h:73
void setAllBiases(double bias)
Set all biases with the same value.
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: cluster.cpp:129
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
static void describe(QString type)
Add to Factory::typeDescriptions() the descriptions of all parameters and subgroups.
Definition: cluster.cpp:151
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
OutputFunction * outFunction() const
Get the Output function.
Definition: cluster.h:149
BiasedCluster(unsigned int numNeurons, QString name="unnamed")
Construct a Cluster that contains numNeurons neurons.
DoubleVector & outputs()
Get the array of outputs.
Definition: cluster.h:136
double getBias(unsigned int neuron)
Get bias of the neuron.
void randomize(double min, double max)
Randomize the biases of BiasedCluster.
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
void update()
Update the outputs of neurons.
double getDouble(double min, double max)
unsigned int numNeurons() const
Return the number of neurons (the length of input and output arrays)
Definition: cluster.h:82
void setNeedReset(bool b)
Set the state of 'needReset' Used by subclasses into update implementation.
Definition: cluster.h:210
void createParameter(QString groupPath, QString parameter)
This file contains the declaration of BiasedCluster class.
virtual ~BiasedCluster()
Destructor.