GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
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 namespace GRT{
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 double Neuron::fire(const VectorDouble &x){
73 
74  double y = 0;
75 
76  switch( activationFunction ){
77  case(LINEAR):
78  y = bias;
79  for(UINT i=0; i<numInputs; i++){
80  y += x[i] * weights[i];
81  }
82  break;
83  case(SIGMOID):
84  y = bias;
85  for(UINT i=0; i<numInputs; i++){
86  y += x[i] * weights[i];
87  }
88 
89  //Trick for stopping overflow
90  if( y < -45.0 ){ y = 0; }
91  else if( y > 45.0 ){ y = 1.0; }
92  else{
93  y = 1.0/(1.0+exp(-y));
94  }
95  break;
96  case(BIPOLAR_SIGMOID):
97  y = bias;
98  for(UINT i=0; i<numInputs; i++){
99  y += x[i] * weights[i];
100  }
101 
102  if( y < -45.0 ){ y = 0; }
103  else if( y > 45.0 ){ y = 1.0; }
104  else{
105  y = (2.0 / (1.0 + exp(-gamma * y))) - 1.0;
106  }
107  break;
108  }
109  return y;
110 
111 }
112 
113 double Neuron::getDerivative(const double &y){
114 
115  double yy = 0;
116  switch( activationFunction ){
117  case(LINEAR):
118  yy = 1.0;
119  break;
120  case(SIGMOID):
121  yy = y * (1.0 - y);
122  break;
123  case(BIPOLAR_SIGMOID):
124  yy = (gamma * (1.0 - (y*y))) / 2.0;
125  break;
126  }
127  return yy;
128 }
129 
130 bool Neuron::validateActivationFunction(const UINT actvationFunction){
131  if( actvationFunction >= LINEAR && actvationFunction < NUMBER_OF_ACTIVATION_FUNCTIONS ) return true;
132  return false;
133 }
134 
135 }//End of namespace GRT
This class implements a Neuron that is used by the Multilayer Perceptron.
Definition: AdaBoost.cpp:25