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