GestureRecognitionToolkit  Version: 0.2.0
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(){}
35 
36 bool Neuron::init(const UINT numInputs,const UINT activationFunction){
37 
38  if( !validateActivationFunction(activationFunction) ){
39  return false;
40  }
41 
42  this->numInputs = numInputs;
43  this->activationFunction = activationFunction;
44 
45  weights.resize(numInputs);
46  previousUpdate.resize(numInputs);
47 
48  //Set the random seed
49  Random random;
50  random.setSeed( (unsigned long long)time(NULL) );
51 
52  //Randomise the weights between [-0.1 0.1]
53  //Note, it's better to set the random values using small weights rather than [-1.0 1.0]
54  for(unsigned int i=0; i<numInputs; i++){
55  weights[i] = random.getRandomNumberUniform(-0.1,0.1);
56  previousUpdate[i] = 0;
57  }
58 
59  //Randomise the bias between [-0.1 0.1]
60  bias = random.getRandomNumberUniform(-0.1,0.1);
61 
62  return true;
63 }
64 
65 void Neuron::clear(){
66  numInputs = 0;
67  bias = 0;
68  previousBiasUpdate = 0;
69  weights.clear();
70  previousUpdate.clear();
71 }
72 
73 Float Neuron::fire(const VectorFloat &x){
74 
75  Float y = 0;
76  UINT i=0;
77 
78  switch( activationFunction ){
79  case(LINEAR):
80  y = bias;
81  for(i=0; i<numInputs; i++){
82  y += x[i] * weights[i];
83  }
84  break;
85  case(SIGMOID):
86  y = bias;
87  for(i=0; i<numInputs; i++){
88  y += x[i] * weights[i];
89  }
90 
91  //Trick for stopping overflow
92  if( y < -45.0 ){ y = 0; }
93  else if( y > 45.0 ){ y = 1.0; }
94  else{
95  y = 1.0/(1.0+exp(-y));
96  }
97  break;
98  case(BIPOLAR_SIGMOID):
99  y = bias;
100  for(i=0; i<numInputs; i++){
101  y += x[i] * weights[i];
102  }
103 
104  if( y < -45.0 ){ y = 0; }
105  else if( y > 45.0 ){ y = 1.0; }
106  else{
107  y = (2.0 / (1.0 + exp(-gamma * y))) - 1.0;
108  }
109  break;
110  }
111  return y;
112 
113 }
114 
115 Float Neuron::getDerivative(const Float &y){
116 
117  Float yy = 0;
118  switch( activationFunction ){
119  case(LINEAR):
120  yy = 1.0;
121  break;
122  case(SIGMOID):
123  yy = y * (1.0 - y);
124  break;
125  case(BIPOLAR_SIGMOID):
126  yy = (gamma * (1.0 - (y*y))) / 2.0;
127  break;
128  }
129  return yy;
130 }
131 
132 bool Neuron::validateActivationFunction(const UINT actvationFunction){
133  if( actvationFunction >= LINEAR && actvationFunction < NUMBER_OF_ACTIVATION_FUNCTIONS ) return true;
134  return false;
135 }
136 
137 GRT_END_NAMESPACE
This class implements a Neuron that is used by the Multilayer Perceptron.
Definition: Random.h:40
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.h:198
void setSeed(unsigned long long seed=0)
Definition: Random.h:68