aboutsummaryrefslogtreecommitdiff
path: root/src/activations.c
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-08-08 17:04:04 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-08-08 17:04:04 -0500
commit4b2fec340e01d09902ca790621b476a05f4b8c2a (patch)
tree542e59795357b9a94c873f6cd44371e3bd48ea38 /src/activations.c
parentfff35116eed83c9368e8bd07d02c9e95c447e018 (diff)
add: pass activations to a separate file
The point is have this file for new activation function without bloating nn.c file
Diffstat (limited to 'src/activations.c')
-rw-r--r--src/activations.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/activations.c b/src/activations.c
new file mode 100644
index 0000000..011734d
--- /dev/null
+++ b/src/activations.c
@@ -0,0 +1,37 @@
+#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_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); }
Feel free to download, copy and edit any repo