Remove the genann_act compile-time activation override

This commit is contained in:
Lewis Van Winkle
2026-08-05 07:23:03 -05:00
parent f94546b5d2
commit bfdd84122c
2 changed files with 5 additions and 26 deletions

View File

@@ -33,27 +33,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef genann_act
#define genann_act_hidden genann_act_hidden_indirect
#define genann_act_output genann_act_output_indirect
#else
#define genann_act_hidden genann_act
#define genann_act_output genann_act
#endif
#define LOOKUP_SIZE 4096 #define LOOKUP_SIZE 4096
/* Bounds the size calculations in genann_init so they cannot overflow. */ /* Bounds the size calculations in genann_init so they cannot overflow. */
#define GENANN_MAX_DIMENSION (1 << 20) #define GENANN_MAX_DIMENSION (1 << 20)
double genann_act_hidden_indirect(const struct genann *ann, double a) {
return ann->activation_hidden(ann, a);
}
double genann_act_output_indirect(const struct genann *ann, double a) {
return ann->activation_output(ann, a);
}
const double sigmoid_dom_min = -15.0; const double sigmoid_dom_min = -15.0;
const double sigmoid_dom_max = 15.0; const double sigmoid_dom_max = 15.0;
double interval; double interval;
@@ -244,7 +228,7 @@ double const *genann_run(genann const *ann, double const *inputs) {
for (k = 0; k < ann->inputs; ++k) { for (k = 0; k < ann->inputs; ++k) {
sum += *w++ * i[k]; sum += *w++ * i[k];
} }
*o++ = genann_act_output(ann, sum); *o++ = ann->activation_output(ann, sum);
} }
return ret; return ret;
@@ -256,7 +240,7 @@ double const *genann_run(genann const *ann, double const *inputs) {
for (k = 0; k < ann->inputs; ++k) { for (k = 0; k < ann->inputs; ++k) {
sum += *w++ * i[k]; sum += *w++ * i[k];
} }
*o++ = genann_act_hidden(ann, sum); *o++ = ann->activation_hidden(ann, sum);
} }
i += ann->inputs; i += ann->inputs;
@@ -268,7 +252,7 @@ double const *genann_run(genann const *ann, double const *inputs) {
for (k = 0; k < ann->hidden; ++k) { for (k = 0; k < ann->hidden; ++k) {
sum += *w++ * i[k]; sum += *w++ * i[k];
} }
*o++ = genann_act_hidden(ann, sum); *o++ = ann->activation_hidden(ann, sum);
} }
i += ann->hidden; i += ann->hidden;
@@ -282,7 +266,7 @@ double const *genann_run(genann const *ann, double const *inputs) {
for (k = 0; k < ann->hidden; ++k) { for (k = 0; k < ann->hidden; ++k) {
sum += *w++ * i[k]; sum += *w++ * i[k];
} }
*o++ = genann_act_output(ann, sum); *o++ = ann->activation_output(ann, sum);
} }
/* Sanity check that we used all weights and wrote all outputs. */ /* Sanity check that we used all weights and wrote all outputs. */
@@ -318,8 +302,7 @@ void genann_train(genann const *ann, double const *inputs, double const *desired
/* Set output layer deltas. */ /* Set output layer deltas. */
if (genann_act_output == genann_act_linear || if (ann->activation_output == genann_act_linear) {
ann->activation_output == genann_act_linear) {
for (j = 0; j < ann->outputs; ++j) { for (j = 0; j < ann->outputs; ++j) {
*d++ = *t++ - *o++; *d++ = *t++ - *o++;
} }

4
test.c
View File

@@ -193,7 +193,6 @@ void train_xor() {
#ifndef genann_act
void train_xor_act(genann_actfun act) { void train_xor_act(genann_actfun act) {
double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
double output[4] = {0, 1, 1, 0}; double output[4] = {0, 1, 1, 0};
@@ -292,7 +291,6 @@ void gradient_tanh() {
void gradient_relu() { void gradient_relu() {
gradient_act(genann_act_relu, genann_act_sigmoid); gradient_act(genann_act_relu, genann_act_sigmoid);
} }
#endif
void persist() { void persist() {
@@ -368,12 +366,10 @@ int main(int argc, char *argv[])
lrun("train and", train_and); lrun("train and", train_and);
lrun("train or", train_or); lrun("train or", train_or);
lrun("train xor", train_xor); lrun("train xor", train_xor);
#ifndef genann_act
lrun("train tanh", train_xor_tanh); lrun("train tanh", train_xor_tanh);
lrun("train relu", train_xor_relu); lrun("train relu", train_xor_relu);
lrun("gradient tanh", gradient_tanh); lrun("gradient tanh", gradient_tanh);
lrun("gradient relu", gradient_relu); lrun("gradient relu", gradient_relu);
#endif
lrun("persist", persist); lrun("persist", persist);
lrun("copy", copy); lrun("copy", copy);
lrun("sigmoid", sigmoid); lrun("sigmoid", sigmoid);