-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivations.cpp
More file actions
63 lines (50 loc) · 947 Bytes
/
Activations.cpp
File metadata and controls
63 lines (50 loc) · 947 Bytes
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
#include "Activations.h"
namespace nncpp {
mathFunction* Activations::TANH = new Tanh();
mathFunction* Activations::SIGMOID = new sigmoid();
mathFunction* Activations::RELU = new relu();
mathFunction* Activations::LINEAR = new linear();
double Tanh::f(double x)
{
if (x == std::numeric_limits<double>::max())
return 1;
if (x == std::numeric_limits<double>::min())
return -1;
double e2x = exp(2.0 * x);
return (e2x - 1.0) / (e2x + 1.0);
}
double Tanh::dfdx(double x)
{
double f = Tanh::f(x);
return 1 - f * f;
}
double relu::f(double x)
{
if (x < 0)
return 0;
return x;
}
double relu::dfdx(double x)
{
if (x > 0)
return 1;
return 0;
}
double linear::f(double x)
{
return x;
}
double linear::dfdx(double x)
{
return 1;
}
double sigmoid::f(double x)
{
return 1.0 / (1.0 + exp(-x));
}
double sigmoid::dfdx(double x)
{
double f = sigmoid::f(x);
return f * (1 - f);
}
}