nnfw/src/biasedcluster.cpp

00001 /********************************************************************************
00002  *  Neural Network Framework.                                                   *
00003  *  Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "biasedcluster.h"
00021 #include "liboutputfunctions.h"
00022 #include "randomgenerator.h"
00023 #include "configurationhelper.h"
00024 
00025 namespace farsa {
00026 
00027 BiasedCluster::BiasedCluster( unsigned int numNeurons, QString name )
00028     : Cluster( numNeurons, name), biasesdata(numNeurons, true), tempdata(numNeurons) {
00029     //--- create the delegate for biases
00030     setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
00031     biasesdata.zeroing();
00032 }
00033 
00034 BiasedCluster::BiasedCluster( ConfigurationParameters& params, QString prefix )
00035     : Cluster( params, prefix ), biasesdata(numNeurons(), true), tempdata(numNeurons()) {
00036     //--- create the delegate for biases
00037     setDelegateFor<BiasedCluster, &BiasedCluster::biases>( "biases" );
00038 
00039     QString vectorSizeErrorTmpl( "The number of elements of the %1 vector in configuration file (%1) is different from the number of neurons (%2)");
00040     // biases is a vector, that is a list of space-separated values
00041     QVector<double> vect = ConfigurationHelper::getVector( params, prefix + "biases" );
00042 #ifdef FARSA_DEBUG
00043     if ( !vect.isEmpty() && vect.size() != (int)numNeurons() ) {
00044         qWarning() << vectorSizeErrorTmpl.arg( "biases" ).arg( vect.size() ).arg( numNeurons() );
00045     }
00046 #endif
00047     biasesdata.copyValues( vect );
00048 }
00049 
00050 BiasedCluster::~BiasedCluster() {
00051 }
00052 
00053 void BiasedCluster::update() {
00054     //--- in order to avoid to create a temporary vector at each call
00055     //--- it use the tempdata and the subtract function of algebra.h
00056     outFunction()->apply( subtract( tempdata, inputs(), biases() ), outputs() );
00057     setNeedReset( true );
00058 }
00059 
00060 void BiasedCluster::setBias( unsigned int neuron, double bias ) {
00061     biasesdata[neuron] = bias;
00062 }
00063 
00064 void BiasedCluster::setAllBiases( double bias ) {
00065     biasesdata.setAll( bias );
00066 }
00067 
00068 void BiasedCluster::setBiases( const DoubleVector& bias ) {
00069     biasesdata.copyValues( bias );
00070 }
00071 
00072 double BiasedCluster::getBias( unsigned int neuron ) {
00073     return biasesdata[neuron];
00074 }
00075 
00076 void BiasedCluster::randomize( double min, double max ) {
00077     for ( unsigned int i = 0; i < numNeurons(); i++ ) {
00078         biasesdata[i] = globalRNG->getDouble( min, max );
00079     }
00080 }
00081 
00082 void BiasedCluster::save(ConfigurationParameters& params, QString prefix)
00083 {
00084     Cluster::save( params, prefix );
00085     params.startObjectParameters(prefix, "BiasedCluster", this);
00086     // First creating a string list, then transforming to a single string
00087     QStringList list;
00088     for (unsigned int i = 0; i < biasesdata.size(); i++) {
00089         list.push_back(QString::number(biasesdata[i]));
00090     }
00091     params.createParameter(prefix, "biases", list.join(" "));
00092 }
00093 
00094 void BiasedCluster::describe( QString type ) {
00095     Cluster::describe( type );
00096     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" );
00097     d.describeReal( "biases" ).props( IsList ).help( "The vector of bias values. It must contains numNeurons elements" );
00098 }
00099 
00100 }