Deep Neuronal Filter
Neuron.h
1 #pragma once
2 
3 
4 #include <stdio.h>
5 #include <assert.h>
6 #include <iostream>
7 #include <ctgmath>
8 #include <cstdlib>
9 #include <cstdio>
10 #include <cassert>
11 #include <fstream>
12 #include <iostream>
13 #include <math.h>
14 #include <fstream>
15 #include <iostream>
16 #include <string>
17 #include <numeric>
18 #include <vector>
19 
20 
21 using namespace std;
22 
28 class Neuron {
29 public:
30 
35  Neuron(int _nInputs);
40  ~Neuron();
41 
48  enum biasInitMethod { B_NONE = 0, B_RANDOM = 1 };
55  enum weightInitMethod { W_ZEROS = 0, W_ONES = 1, W_RANDOM = 2, W_ONES_NORM = 3, W_RANDOM_NORM = 5, };
62  enum actMethod {Act_Sigmoid = 1, Act_Tanh = 2, Act_ReLU = 3, Act_NONE = 0};
69  enum whichError {onBackwardError = 0, onMidError = 1, onForwardError = 2};
70 
80  void initNeuron(int _neuronIndex, int _layerIndex, weightInitMethod _wim, biasInitMethod _bim, actMethod _am);
81 
85  void setLearningRate(double _learningRate, double _b_learningRate);
86 
92  void setInput(int _index, double _value);
93 
99  void propInputs(int _index, double _value);
100 
106  int calcOutput(int _layerHasReported);
107 
112  void setError(double _value);
113 
118  double getError();
119 
123  void updateWeights();
124 
130  inline double doActivation(const double sum) const {
131  switch(actMet){
132  case Act_Sigmoid:
133  return (1/(1+(exp(-sum)))) - 0.5;
134  case Act_Tanh:
135  return tanh(sum);
136  case Act_ReLU:
137  if (sum > 0) return sum; else return 0;
138  case Act_NONE:
139  return sum;
140  }
141  return sum;
142  }
143 
149  inline double doActivationPrime(const double input) const {
150  switch(actMet){
151  case Act_Sigmoid:
152  return 1 * (0.5 + doActivation(input)) * (0.5 - doActivation(input));
153  case Act_Tanh:
154  return 1 - pow (tanh(input), 2);
155  case Act_ReLU:
156  if (sum > 0) return 1; else return 0;
157  case Act_NONE:
158  return 1;
159  }
160  return 1;
161  }
162 
167  inline void setBackpropError(const double upstreamDeltaErrorSum) {
168  error = doActivationPrime(getSumOutput()) * upstreamDeltaErrorSum;
169  }
170 
175  inline double getOutput() {
176  return output;
177  }
178 
183  inline double getSumOutput() {
184  return sum;
185  }
186 
192  double getWeights(int _inputIndex);
193 
199  double getInitWeights(int _inputIndex);
200 
205  double getWeightChange();
206 
211  double getMaxWeight(){
212  return maxWeight;
213  }
214 
219  double getMinWeight(){
220  return minWeight;
221  }
222 
227  double getSumWeight(){
228  return weightSum;
229  }
230 
235  double getWeightDistance();
236 
241  int getnInputs();
242 
246  void saveWeights();
247 
251  void printNeuron();
252 
258  inline void setWeight(int _index, double _weight) {
259  assert((_index >= 0) && (_index < nInputs));
260  weights[_index] = _weight;
261  }
262 
263 private:
264  // initialisation:
265  int nInputs = 0;
266  int myLayerIndex = 0;
267  int myNeuronIndex = 0;
268  double *initialWeights = 0;
269 
270  double w_learningRate = 0;
271  double b_learningRate = 0;
272 
273  int iHaveReported = 0;
274 
275  double bias = 0;
276  double sum = 0;
277  double output = 0;
278 
279  //learning:
280  double backwardsCoeff = 0;
281  double midCoeff = 0;
282  double forwardCoeff = 0;
283  double globalCoeff = 0;
284  double *weights = 0;
285  double *inputs = 0;
286  double weightSum = 0;
287  double maxWeight = 1;
288  double minWeight = 1;
289  double weightChange=0;
290  double weightsDifference = 0;
291  int actMet = 0;
292 
293  double error = 0;
294 };
Neuron::actMethod
actMethod
Options for activation functions of the neuron 0 for using the logistic function 1 for using the hype...
Definition: Neuron.h:62
Neuron::setBackpropError
void setBackpropError(const double upstreamDeltaErrorSum)
Sets the internal backprop error.
Definition: Neuron.h:167
Neuron::doActivationPrime
double doActivationPrime(const double input) const
Performs inverse activation on any input that is passed to this function.
Definition: Neuron.h:149
Neuron::getSumWeight
double getSumWeight()
Requests for the total sum of weights located in this neuron.
Definition: Neuron.h:227
Neuron::getOutput
double getOutput()
Requests the output of this neuron.
Definition: Neuron.h:175
Neuron::doActivation
double doActivation(const double sum) const
Performs the activation of the sum output of the neuron.
Definition: Neuron.h:130
Neuron::biasInitMethod
biasInitMethod
Options for method of initialising biases 0 for initialising all weights to zero 1 for initialising a...
Definition: Neuron.h:48
Neuron::whichError
whichError
Options for choosing an error to monitor the gradient of 0 for monitoring the error that propagates b...
Definition: Neuron.h:69
Neuron
This is the class for creating neurons inside the Layer class.
Definition: Neuron.h:28
Neuron::setWeight
void setWeight(int _index, double _weight)
Sets the weights of the neuron.
Definition: Neuron.h:258
Neuron::getMinWeight
double getMinWeight()
Requests for the minimum weights located in this neuron.
Definition: Neuron.h:219
Neuron::getMaxWeight
double getMaxWeight()
Requests for the maximum weights located in this neuron.
Definition: Neuron.h:211
Neuron::weightInitMethod
weightInitMethod
Options for method of initialising weights 0 for initialising all weights to zero 1 for initialising ...
Definition: Neuron.h:55
Neuron::getSumOutput
double getSumOutput()
Requests the sum output of the neuron.
Definition: Neuron.h:183