blob: e2704eef9e16523a67cb9c50d13a11c7acc00847 (
plain)
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
|
#include "nn.h"
double leaky_relu(double x);
double dleaky_relu(double x);
double relu(double x);
double drelu(double x);
double sigmoid(double x);
double dsigmoid(double x);
double softplus(double x);
double dsoftplus(double x);
struct Activation NN_LEAKY_RELU = {
.func = leaky_relu,
.dfunc = dleaky_relu
};
struct Activation NN_SOFTPLUS = {
.func = softplus,
.dfunc = dsoftplus
};
struct Activation NN_RELU = {
.func = relu,
.dfunc = drelu
};
struct Activation NN_SIGMOID = {
.func = sigmoid,
.dfunc = dsigmoid
};
double sigmoid(double x) { return 1 / (1 + exp(-x)); }
double dsigmoid(double x) { return sigmoid(x) * (1 - sigmoid(x)); }
double relu(double x) { return (x > 0) ? x : 0; }
double drelu(double x) { return (x > 0) ? 1 : 0; }
double leaky_relu(double x) { return (x > 0) ? x : 0.01 * x; }
double dleaky_relu(double x) { return (x > 0) ? 1 : 0.01; }
double softplus(double x) { return log1p(exp(x)); }
double dsoftplus(double x) { return sigmoid(x); }
|