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 namespace farsa {
26 
27 BiasedCluster::BiasedCluster( unsigned int numNeurons, QString name )
28  : Cluster( numNeurons, name), biasesdata(numNeurons, true), tempdata(numNeurons) {
29  //--- create the delegate for biases
30  setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
31  biasesdata.zeroing();
32 }
33 
35  : Cluster( params, prefix ), biasesdata(numNeurons(), true), tempdata(numNeurons()) {
36  //--- create the delegate for biases
37  setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
38 
39  QString vectorSizeErrorTmpl( "The number of elements of the %1 vector in configuration file (%1) is different from the number of neurons (%2)");
40  // biases is a vector, that is a list of space-separated values
41  QVector<double> vect = ConfigurationHelper::getVector( params, prefix + "biases" );
42 #ifdef FARSA_DEBUG
43  if ( !vect.isEmpty() && vect.size() != (int)numNeurons() ) {
44  qWarning() << vectorSizeErrorTmpl.arg( "biases" ).arg( vect.size() ).arg( numNeurons() );
45  }
46 #endif
47  biasesdata.copyValues( vect );
48 }
49 
51 }
52 
54  //--- in order to avoid to create a temporary vector at each call
55  //--- it use the tempdata and the subtract function of algebra.h
56  outFunction()->apply( subtract( tempdata, inputs(), biases() ), outputs() );
57  setNeedReset( true );
58 }
59 
60 void BiasedCluster::setBias( unsigned int neuron, double bias ) {
61  biasesdata[neuron] = bias;
62 }
63 
64 void BiasedCluster::setAllBiases( double bias ) {
65  biasesdata.setAll( bias );
66 }
67 
69  biasesdata.copyValues( bias );
70 }
71 
72 double BiasedCluster::getBias( unsigned int neuron ) {
73  return biasesdata[neuron];
74 }
75 
76 void BiasedCluster::randomize( double min, double max ) {
77  for ( unsigned int i = 0; i < numNeurons(); i++ ) {
78  biasesdata[i] = globalRNG->getDouble( min, max );
79  }
80 }
81 
82 void BiasedCluster::save(ConfigurationParameters& params, QString prefix)
83 {
84  Cluster::save( params, prefix );
85  params.startObjectParameters(prefix, "BiasedCluster", this);
86  // First creating a string list, then transforming to a single string
87  QStringList list;
88  for (unsigned int i = 0; i < biasesdata.size(); i++) {
89  list.push_back(QString::number(biasesdata[i]));
90  }
91  params.createParameter(prefix, "biases", list.join(" "));
92 }
93 
94 void BiasedCluster::describe( QString type ) {
95  Cluster::describe( type );
96  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" );
97  d.describeReal( "biases" ).props( IsList ).help( "The vector of bias values. It must contains numNeurons elements" );
98 }
99 
100 }