nnfw/src/libradialfunctions.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 "libradialfunctions.h"
00021 
00022 namespace farsa {
00023 
00024 GaussFunction::GaussFunction( double centre, double variance, double maxvalue )
00025     : OutputFunction() {
00026     this->centre = centre;
00027     this->variancev = variance;
00028     msqrvar = -( variancev*variancev );
00029     this->max = maxvalue;
00030 }
00031 
00032 bool GaussFunction::setVariance( double v ) {
00033     variancev = v;
00034     msqrvar = -( variancev*variancev );
00035     return true;
00036 }
00037 
00038 double GaussFunction::variance() {
00039     return variancev;
00040 }
00041 
00042 void GaussFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
00043     // --- out <- max * exp( (centre-inputs)^2 / -(variance^2) )
00044     square( subtract( outputs, centre, inputs ) );
00045     exp( outputs /= msqrvar ) *= max;
00046 }
00047 
00048 bool GaussFunction::derivate( const DoubleVector& x, const DoubleVector& y, DoubleVector& d ) const {
00049     // --- d <- ( 2.0*(centre-x) / variance^2 ) * y
00050     subtract( d, centre, x ) *= 2.0;
00051     d /= -msqrvar;
00052     d *= y;
00053     return true;
00054 }
00055 
00056 void GaussFunction::configure(ConfigurationParameters& params, QString prefix)
00057 {
00058     centre = 0.0;
00059     QString str = params.getValue(prefix + "centre");
00060     if (!str.isEmpty()) {
00061         bool ok;
00062         centre = str.toDouble(&ok);
00063         if (!ok) {
00064             centre = 0.0;
00065         }
00066     }
00067 
00068     variancev = 1.0;
00069     str = params.getValue(prefix + "variance");
00070     if (!str.isEmpty()) {
00071         bool ok;
00072         variancev = str.toDouble(&ok);
00073         if (!ok) {
00074             variancev = 1.0;
00075         }
00076     }
00077     // Also recomputing minus squared variance
00078     msqrvar = -( variancev*variancev );
00079 
00080     max = 1.0;
00081     str = params.getValue(prefix + "max");
00082     if (!str.isEmpty()) {
00083         bool ok;
00084         max = str.toDouble(&ok);
00085         if (!ok) {
00086             max = 1.0;
00087         }
00088     }
00089 }
00090 
00091 void GaussFunction::save(ConfigurationParameters& params, QString prefix)
00092 {
00093     params.startObjectParameters(prefix, "GaussFunction", this);
00094     params.createParameter(prefix, "centre", QString::number(centre));
00095     params.createParameter(prefix, "variance", QString::number(variancev));
00096     params.createParameter(prefix, "max", QString::number(max));
00097 }
00098 
00099 }