Feedforward Closedloop Learning: neuron.h Source File

Feedforward Closedloop Learning

neuron.h
1 #ifndef __Neuron_H_
2 #define __Neuron_H_
3
4 #include<math.h>
5 #include<stdio.h>
6 #include<assert.h>
7
8 // bypasses the sigmoid function
9 // #define LINEAR_OUTPUT
10
11 // enables denbug output to sdt out
12 // #define DEBUG_NEURON
13
14 #include "globals.h"
15
19 class Neuron {
20
21 public:
22
26 Neuron(int _nInputs);
27
31 ~Neuron();
32
36 void calcOutput();
37
40 static void* calcOutputThread(void* object) {
41 reinterpret_cast<Neuron*>(object)->calcOutput();
42 return NULL;
43 };
44
48 void doLearning();
49
52 static void* doLearningThread(void* object) {
53 reinterpret_cast<Neuron*>(object)->doLearning();
54 return NULL;
55 };
56
60 void doMaxDet();
61
64 static void* doMaxDetThread(void* object) {
65 reinterpret_cast<Neuron*>(object)->doMaxDet();
66 return NULL;
67 };
68
71 enum WeightInitMethod { MAX_OUTPUT_RANDOM = 0, MAX_WEIGHT_RANDOM = 1, MAX_OUTPUT_CONST = 2, CONST_WEIGHTS = 3};
72
78 void initWeights(double _max = 1, int initBias = 1, WeightInitMethod _wm = MAX_OUTPUT_RANDOM);
79
86 enum ActivationFunction { LINEAR = 0, TANH = 1, RELU = 2, REMAXLU = 3, TANHLIMIT = 4};
87
91 void setActivationFunction(ActivationFunction _activationFunction) {
92 activationFunction = _activationFunction;
93 }
94
98 double dActivation();
99
103 double getMinWeightValue();
104
108 double getMaxWeightValue();
109
113 double getWeightDistanceFromInitialWeights();
114
118 inline double getOutput() { return output; };
119
123 inline double getSum() { return sum; };
124
129 inline double getWeight( int _index) {
130 assert((_index>=0)&&(_index<nInputs));
131 return mask[_index] ? weights[_index] : 0;
132 };
133
138 inline void setWeight( int _index, double _weight) {
139 assert((_index>=0)&&(_index<nInputs));
140 weights[_index]=_weight;
141 };
142
147 void setError(double _error);
148
152 inline double getError() { return error; };
153
158 inline void setInput( int _index, double _value) {
159 assert((_index>=0)&&(_index<nInputs));
160 inputs[_index] = _value;
161 };
162
167 inline double getInput( int _index) {
168 assert((_index>=0)&&(_index<nInputs));
169 return inputs[_index];
170 };
171
175 inline double getBiasWeight() {return biasweight; };
176
180 inline void setBiasWeight(double _biasweight) { biasweight=_biasweight; };
181
185 inline void setBias( double _bias) { bias=_bias; };
186
190 inline void setLearningRate( double _learningrate) { learningRate = _learningrate; };
191
196 inline void setMomentum( double _momentum) { momentum = _momentum; };
197
201 inline void setDecay(double _decay) {
202 decay = _decay;
203 }
204
208 inline double getDecay() {
209 return decay;
210 }
211
215 inline int getNinputs() { return nInputs; };
216
225 void setGeometry( int _width, int _height) {
226 assert((_width*_height)==nInputs);
227 width = _width;
228 height = _height;
229 }
230
237 void setMask( int x, int y, unsigned char c);
238
242 void setMask( unsigned char c);
243
249 unsigned char getMask( int x, int y);
250
255 unsigned char getMask(int index) { return mask[index]; };
256
260 double getSumOfSquaredWeightVector();
261
265 double getEuclideanNormOfWeightVector() {
266 return sqrt(getSumOfSquaredWeightVector());
267 }
268
272 double getManhattanNormOfWeightVector();
273
277 double getInfinityNormOfWeightVector();
278
282 double getAverageOfWeightVector();
283
287 void normaliseWeights(double norm);
288
292 void saveInitialWeights();
293
298 void setDebugInfo(int _layerIndex, int _neuronIndex) {
299 layerIndex = _layerIndex;
300 neuronIndex = _neuronIndex;
301 }
302
306 inline void setStep(long int _step) {
307 step = _step;
308 }
309
310 private:
311 int nInputs;
312 unsigned char* mask = 0;
313 double* weights = 0;
314 double* initialWeights = 0;
315 double* weightChange = 0;
316 double decay = 0;
317 double biasweight = 0;
318 double biasweightChange = 0;
319 double bias = 0;
320 double* inputs = 0;
321 double output = 0;
322 double sum = 0;
323 double error = 0;
324 double learningRate = 0;
325 double learningRateFactor = 1;
326 double momentum = 0;
327 int width = 0;
328 int height = 0;
329 int maxDet = 0;
330 int layerIndex = 0;
331 int neuronIndex = 0;
332 long int step = 0;
333 ActivationFunction activationFunction = TANH;
334 };
335
336 #endif
Neuron::dActivation
double dActivation()
Returns the output of the neuron fed through the derivative of the activation.
Definition: neuron.cpp:114
Neuron::getWeight
double getWeight(int _index)
Gets one weight.
Definition: neuron.h:129
Neuron::getMask
unsigned char getMask(int index)
Boundary safe return of the mask in flat form.
Definition: neuron.h:255
Neuron::getMask
unsigned char getMask(int x, int y)
Boundary safe return of the mask in (x,y) coordinates.
Definition: neuron.cpp:413
Neuron::getNinputs
int getNinputs()
Get the number of inputs to the neuron.
Definition: neuron.h:215
Neuron::setActivationFunction
void setActivationFunction(ActivationFunction _activationFunction)
Sets the activation function.
Definition: neuron.h:91
Neuron::getManhattanNormOfWeightVector
double getManhattanNormOfWeightVector()
Calculates the Manhattan length of the weight vector /return Manhattan length of the weight vector.
Definition: neuron.cpp:201
Neuron::setMask
void setMask(int x, int y, unsigned char c)
Boundary safe manipulation of the convolution mask.
Definition: neuron.cpp:399
Neuron::~Neuron
~Neuron()
Destructor Tidies up any memory allocations.
Definition: neuron.cpp:38
Neuron::saveInitialWeights
void saveInitialWeights()
Save the initial weights.
Definition: neuron.cpp:344
Neuron::getMaxWeightValue
double getMaxWeightValue()
Maximum weight value.
Definition: neuron.cpp:352
Neuron::setDecay
void setDecay(double _decay)
Sets the weight decay over time.
Definition: neuron.h:201
Neuron::getSum
double getSum()
Gets the weighted sum of all inputs pre-activation function.
Definition: neuron.h:123
Neuron::setMomentum
void setMomentum(double _momentum)
Sets the momentum.
Definition: neuron.h:196
Neuron::doMaxDetThread
static void * doMaxDetThread(void *object)
Wrapper for thread callback for maxdet.
Definition: neuron.h:64
Neuron::getBiasWeight
double getBiasWeight()
Gets the bias weight.
Definition: neuron.h:175
Neuron::setStep
void setStep(long int _step)
Sets the simulation step for debugging and logging.
Definition: neuron.h:306
Neuron::normaliseWeights
void normaliseWeights(double norm)
Normalises the weights with a divisor.
Definition: neuron.cpp:256
Neuron::getInput
double getInput(int _index)
Get the value at one input.
Definition: neuron.h:167
Neuron::getDecay
double getDecay()
Gets the weight decay over time.
Definition: neuron.h:208
Neuron::setDebugInfo
void setDebugInfo(int _layerIndex, int _neuronIndex)
Sets debug info populated from Layer.
Definition: neuron.h:298
Neuron::WeightInitMethod
WeightInitMethod
Constants how to init the weights in the neuron.
Definition: neuron.h:71
Neuron::initWeights
void initWeights(double _max=1, int initBias=1, WeightInitMethod _wm=MAX_OUTPUT_RANDOM)
Inits the weights in the neuron.
Definition: neuron.cpp:298
Neuron::setGeometry
void setGeometry(int _width, int _height)
Tells the layer that it's been a 2D array originally to be a convolutional layer.
Definition: neuron.h:225
Neuron::setError
void setError(double _error)
Sets the error in the neuron If the derivative is activated then the derivative of the error is calcu...
Definition: neuron.cpp:393
Neuron::getSumOfSquaredWeightVector
double getSumOfSquaredWeightVector()
Calculates the sum of the squared weight vector values.
Definition: neuron.cpp:183
Neuron::getWeightDistanceFromInitialWeights
double getWeightDistanceFromInitialWeights()
Weight development.
Definition: neuron.cpp:380
Neuron::getOutput
double getOutput()
Gets the output of the neuron.
Definition: neuron.h:118
Neuron::setInput
void setInput(int _index, double _value)
Sets one input.
Definition: neuron.h:158
Neuron::doLearning
void doLearning()
Performs the learning Performs ICO learning in the neuron: pre * error.
Definition: neuron.cpp:150
Neuron::Neuron
Neuron(int _nInputs)
Constructor.
Definition: neuron.cpp:17
Neuron::getEuclideanNormOfWeightVector
double getEuclideanNormOfWeightVector()
Calculates the Eucledian length of the weight vector.
Definition: neuron.h:265
Neuron::doMaxDet
void doMaxDet()
Detects max of an input Switches the highest weight to 1 and the others to 0.
Definition: neuron.cpp:274
Neuron::getAverageOfWeightVector
double getAverageOfWeightVector()
Calculates the average of the weight values.
Definition: neuron.cpp:236
Neuron
Neuron which calculates the output and performs learning.
Definition: neuron.h:19
Neuron::setWeight
void setWeight(int _index, double _weight)
Sets one weight.
Definition: neuron.h:138
Neuron::getInfinityNormOfWeightVector
double getInfinityNormOfWeightVector()
Calculates the Infinity norm of the vector.
Definition: neuron.cpp:218
Neuron::calcOutput
void calcOutput()
Calculate the output of the neuron This runs the filters, activation functions, sum it all up.
Definition: neuron.cpp:47
Neuron::setBias
void setBias(double _bias)
Sets the bias input value.
Definition: neuron.h:185
Neuron::getMinWeightValue
double getMinWeightValue()
Minimum weight value.
Definition: neuron.cpp:366
Neuron::setLearningRate
void setLearningRate(double _learningrate)
Sets the learning rate.
Definition: neuron.h:190
Neuron::ActivationFunction
ActivationFunction
Activation functions on offer LINEAR: linear unit, TANH: tangens hyperbolicus, RELU: linear rectifier...
Definition: neuron.h:86
Neuron::getError
double getError()
Gets the error as set by setError.
Definition: neuron.h:152
Neuron::setBiasWeight
void setBiasWeight(double _biasweight)
Sets the bias weight.
Definition: neuron.h:180
Neuron::calcOutputThread
static void * calcOutputThread(void *object)
Wrapper for thread callback for output calc.
Definition: neuron.h:40
Neuron::doLearningThread
static void * doLearningThread(void *object)
Wrapper for thread callback for learning.
Definition: neuron.h:52