-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivationFunctions.h
177 lines (148 loc) · 3.74 KB
/
ActivationFunctions.h
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Recursive Neural Networks: neural networks for data structures
*
* Copyright (C) 2018 Alessandro Vullo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _ACTIVATION_FUNCTIONS_H
#define _ACTIVATION_FUNCTIONS_H
#include <cmath>
/***
Callback inlining technique based on STL-style function objects.
The aim is to create parameterized evaluation and derivation
function of non linear (sigmoid, tanh) and linear activation
functions commonly adopted in Neural Networks programming practice.
***/
class Sigmoid {
public:
double operator()(double x) {
return (1.0 / (1.0 + exp(-x)));
}
double deriv(double x) {
return x * (1.0 - x);
}
};
class TanH {
public:
double operator()(double x) {
return tanh(x);
}
double deriv(double x) {
return (1.0 - x * x);
}
};
class Linear {
public:
double operator()(double x) {
return x;
}
double deriv(double x) {
return 1.0;
}
};
class LinearSaturated {
public:
double operator()(double x) {
if(x >= 1)
return 1;
else if(x <= -1)
return 0;
else
return (x + 1) / 2;
}
double deriv(double x) {
if(x > 1 || x < -1)
return 0;
else
return .5;
}
};
class ReLU {
public:
double operator()(double x) {
if(x>=0)
return x;
else
return 0;
}
double deriv(double x) {
if(x>=0)
return 1.0;
else
return 0;
}
};
// We can now define templatized functions
// to evaluate and derivate the unit activation functions.
template<class T_function>
double evaluate(T_function f, double x) {
return f(x);
}
template<class T_function>
double derivate(T_function f, double x) {
return f.deriv(x);
}
/***
First attempt to create parameterized activation function,
based on polymorphism.
Rejected because virtual function dispatch will cause
poor performance.
***/
/*
class ActivationFunction {
public:
virtual double function(double x) = 0;
virtual double derivative_of_function(double x) = 0;
double evaluate(double x) {
return function(x);
}
double derivate(double x) {
return derivative_of_function(x);
}
};
class Sigmoid: public ActivationFunction {
public:
virtual double function(double x) {
return (1.0 / (1.0 + exp(-x)));
}
virtual double derivative_of_function(double x) {
return x * (1.0 - x);
}
};
class Tanh: public ActivationFunction {
public:
virtual double function(double x) {
return tanh(x);
}
virtual double derivative_of_function(double x) {
return (1.0 - x * x);
}
};
class Linear: public ActivationFunction {
public:
virtual double function(double x) {
return x;
}
virtual double derivative_of_function(double x) {
return 1.0;
}
};
*/
#endif // _ACTIVATION_FUNCTIONS_H