-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNN_Neuron.hpp
150 lines (130 loc) · 3.66 KB
/
RNN_Neuron.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* RNN_Neuron.hpp
*
* Revision: November 2018
* Author: Thassyo Pinto - [email protected]
*/
#ifndef RNN_NEURON_HPP_
#define RNN_NEURON_HPP_
// Standard libraries
#include <cmath>
// A neuron class for neural networks.
class Neuron{
public:
// Enumerator for the different possible activation functions.
// - linear: identity, except that it truncates values to lie in [-1, 1]
// - sin: calculates the sine-wave (f(x)=sin(x))
// - gaussian: calculates a gaussian (f(x)=e^(-x*x)), scaled to lie in [-1, 1]
// - sigmoid: calculates the sigmoid (f(x)=tanh(x*lambda))
enum af_t{
linear,
sine,
gaussian,
sigmoid,
nbActivationFunctions
};
// Constructor. Creates a new neuron with the indicated number of incoming connection.
// The current and new activations are initialized to zero.
Neuron():
_value(0),
_incoming(0),
_bias(0),
_lambda(5.0),
_activationFunction(sigmoid){
}
// Return the current activation value of this neuron.
double getValue(){
return _value;
}
// Sets the current value.
void setValue(double value){
_value = value;
}
// Returns the current amount of incoming potential.
const double& getIncoming(){
return _incoming;
}
// Sets the amount of incoming potential.
void setIncoming(double incoming){
_incoming = incoming;
}
// Adds the supplied value to the current incoming potential.
void updateIncoming(double update){
_incoming += update;
}
// Returns the bias of this neuron.
double getBias(){
return _bias;
}
// Sets the bias of this neuron.
void setBias(double bias){
_bias = bias;
}
// Adds the index of an incoming connection.
void addIncoming(size_t incoming){
_incomingIndices.push_back(incoming);
}
// Adds the index of an outgoing connection.
void addOutgoing(size_t outgoing){
_outgoingIndices.push_back(outgoing);
}
// Returns the vector of incoming connection indices.
const std::vector<size_t>& getIncomingIndices(){
return _incomingIndices;
}
// Returns the vector of outgoing connection indices.
const std::vector<size_t>& getOutgoingIndices(){
return _outgoingIndices;
}
// Resets the state of this neuron.
void reset(){
_value = 0;
_incoming = 0;
}
// Updates the activation value based on the incoming potential and the bias.
void propagate(){
double x = this->getIncoming() + this->getBias();
switch(_activationFunction){
case linear:
if(x > 1) _value = 1;
else if(x < -1) _value = -1;
else _value = x;
break;
case sine:
_value = std::sin(x);
break;
case gaussian:
_value = std::exp(float(-x*x)) * 2.0 - 1.0;
break;
case sigmoid:
_value = std::tanh(x * _lambda);
break;
default:
std::cout << "Error! No activation function found!" << std::endl;
break;
}
}
// Sets the current activation function.
// Possible values are:
// - linear
// - sin
// - guassian
// - sigmoid
void setActivationFunction(af_t activation){
_activationFunction = activation;
}
// Returns the current activation function.
af_t getActivationFunction(){
return _activationFunction;
}
protected:
// ANN attributes
af_t _activationFunction;
std::vector<size_t> _incomingIndices;
std::vector<size_t> _outgoingIndices;
double _value;
double _incoming;
double _bias;
double _lambda;
};
#endif /* RNN_NEURON_HPP_ */