From 4b2fec340e01d09902ca790621b476a05f4b8c2a Mon Sep 17 00:00:00 2001 From: jvech Date: Tue, 8 Aug 2023 17:04:04 -0500 Subject: add: pass activations to a separate file The point is have this file for new activation function without bloating nn.c file --- src/activations.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/activations.c (limited to 'src/activations.c') 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); } -- cgit v1.2.3-70-g09d2