GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Neuron.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #define GRT_DLL_EXPORTS
22 #include "Neuron.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 Neuron::Neuron(){
27  activationFunction = LINEAR;
28  numInputs = 0;
29  gamma = 2.0;
30  bias = 0;
31  previousBiasUpdate = 0;
32 }
33 
34 Neuron::Neuron( const Neuron &rhs ){
35  this->gamma = rhs.gamma;
36  this->bias = rhs.bias;
37  this->previousBiasUpdate = rhs.previousBiasUpdate;
38  this->weights = rhs.weights;
39  this->previousUpdate = rhs.previousUpdate;
40  this->numInputs = rhs.numInputs;
41  this->activationFunction = rhs.activationFunction;
42 }
43 
44 Neuron::~Neuron(){}
45 
46 Neuron& Neuron::operator=(const Neuron &rhs){
47  if( this != &rhs ){
48  this->gamma = rhs.gamma;
49  this->bias = rhs.bias;
50  this->previousBiasUpdate = rhs.previousBiasUpdate;
51  this->weights = rhs.weights;
52  this->previousUpdate = rhs.previousUpdate;
53  this->numInputs = rhs.numInputs;
54  this->activationFunction = rhs.activationFunction;
55  }
56 
57  return *this;
58 }
59 
60 bool Neuron::init(const UINT numInputs,const Type activationFunction,Random &random,const Float minWeightRange,const Float maxWeightRange,const Float minBiasRange,const Float maxBiasRange){
61 
62  if( !validateActivationFunction(activationFunction) ){
63  return false;
64  }
65 
66  this->numInputs = numInputs;
67  this->activationFunction = activationFunction;
68 
69  weights.resize(numInputs);
70  previousUpdate.resize(numInputs);
71 
72  //Randomise the weights
73  //Note, it's better to set the random values using small weights rather than [-1.0 1.0]
74  for(unsigned int i=0; i<numInputs; i++){
75  weights[i] = random.getUniform(minWeightRange,maxWeightRange);
76  previousUpdate[i] = 0;
77  }
78 
79  //Randomise the bias
80  bias = random.getUniform(minBiasRange,maxBiasRange);
81  previousBiasUpdate = 0;
82 
83  return true;
84 }
85 
86 void Neuron::clear(){
87  numInputs = 0;
88  bias = 0;
89  previousBiasUpdate = 0;
90  weights.clear();
91  previousUpdate.clear();
92 }
93 
94 Float Neuron::fire(const VectorFloat &x){
95 
96  Float y = 0;
97  UINT i=0;
98 
99  switch( activationFunction ){
100  case(LINEAR):
101  y = bias;
102  for(i=0; i<numInputs; i++){
103  y += x[i] * weights[i];
104  }
105  break;
106  case(SIGMOID):
107  y = bias;
108  for(i=0; i<numInputs; i++){
109  y += x[i] * weights[i];
110  }
111 
112  y = 1.0/(1.0+exp(-y));
113  break;
114  case(BIPOLAR_SIGMOID):
115  y = bias;
116  for(i=0; i<numInputs; i++){
117  y += x[i] * weights[i];
118  }
119 
120  y = (2.0 / (1.0 + exp(-gamma * y))) - 1.0;
121  break;
122  case(TANH):
123  y = bias;
124  for(i=0; i<numInputs; i++){
125  y += x[i] * weights[i];
126  }
127  y = tanh( y );
128  break;
129  }
130  return y;
131 
132 }
133 
134 Float Neuron::getDerivative(const Float &y){
135 
136  Float yy = 0;
137  switch( activationFunction ){
138  case(LINEAR):
139  yy = 1.0;
140  break;
141  case(SIGMOID):
142  yy = y * (1.0 - y);
143  break;
144  case(BIPOLAR_SIGMOID):
145  yy = (gamma * (1.0 - (y*y))) / 2.0;
146  break;
147  case(TANH):
148  yy = 1.0 - (y*y);
149  break;
150  }
151  return yy;
152 }
153 
154 bool Neuron::validateActivationFunction(const Type actvationFunction){
155  if( actvationFunction >= LINEAR && actvationFunction < NUMBER_OF_ACTIVATION_FUNCTIONS ) return true;
156  return false;
157 }
158 
159 GRT_END_NAMESPACE
This class implements a Neuron that is used by the Multilayer Perceptron.
This file contains the Random class, a useful wrapper for generating cross platform random functions...
Definition: Random.h:46
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
Definition: Neuron.h:38
Float getUniform(const Float &minRange=0.0, const Float &maxRange=1.0)
Definition: Random.cpp:133