libradialfunctions.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 "libradialfunctions.h"
21 
22 namespace farsa {
23 
24 GaussFunction::GaussFunction( double centre, double variance, double maxvalue )
25  : OutputFunction() {
26  this->centre = centre;
27  this->variancev = variance;
28  msqrvar = -( variancev*variancev );
29  this->max = maxvalue;
30 }
31 
32 bool GaussFunction::setVariance( double v ) {
33  variancev = v;
34  msqrvar = -( variancev*variancev );
35  return true;
36 }
37 
39  return variancev;
40 }
41 
42 void GaussFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
43  // --- out <- max * exp( (centre-inputs)^2 / -(variance^2) )
44  square( subtract( outputs, centre, inputs ) );
45  exp( outputs /= msqrvar ) *= max;
46 }
47 
48 bool GaussFunction::derivate( const DoubleVector& x, const DoubleVector& y, DoubleVector& d ) const {
49  // --- d <- ( 2.0*(centre-x) / variance^2 ) * y
50  subtract( d, centre, x ) *= 2.0;
51  d /= -msqrvar;
52  d *= y;
53  return true;
54 }
55 
57 {
58  centre = 0.0;
59  QString str = params.getValue(prefix + "centre");
60  if (!str.isEmpty()) {
61  bool ok;
62  centre = str.toDouble(&ok);
63  if (!ok) {
64  centre = 0.0;
65  }
66  }
67 
68  variancev = 1.0;
69  str = params.getValue(prefix + "variance");
70  if (!str.isEmpty()) {
71  bool ok;
72  variancev = str.toDouble(&ok);
73  if (!ok) {
74  variancev = 1.0;
75  }
76  }
77  // Also recomputing minus squared variance
78  msqrvar = -( variancev*variancev );
79 
80  max = 1.0;
81  str = params.getValue(prefix + "max");
82  if (!str.isEmpty()) {
83  bool ok;
84  max = str.toDouble(&ok);
85  if (!ok) {
86  max = 1.0;
87  }
88  }
89 }
90 
91 void GaussFunction::save(ConfigurationParameters& params, QString prefix)
92 {
93  params.startObjectParameters(prefix, "GaussFunction", this);
94  params.createParameter(prefix, "centre", QString::number(centre));
95  params.createParameter(prefix, "variance", QString::number(variancev));
96  params.createParameter(prefix, "max", QString::number(max));
97 }
98 
99 }