Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backpropagation for (some) user-defined activation functions #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions genann.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ double genann_act_sigmoid(const genann *ann unused, double a) {
return 1.0 / (1 + exp(-a));
}

double genann_act_diffexpr_sigmoid(const genann * ann unused, double y) {
return y*(1.0-y);
}

void genann_init_sigmoid_lookup(const genann *ann) {
const double f = (sigmoid_dom_max - sigmoid_dom_min) / LOOKUP_SIZE;
int i;
Expand Down Expand Up @@ -143,6 +147,9 @@ genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs) {

genann_init_sigmoid_lookup(ret);

ret->diffexpr_activation_hidden = genann_act_diffexpr_sigmoid;
ret->diffexpr_activation_output = genann_act_diffexpr_sigmoid;

return ret;
}

Expand Down Expand Up @@ -296,7 +303,7 @@ void genann_train(genann const *ann, double const *inputs, double const *desired
}
} else {
for (j = 0; j < ann->outputs; ++j) {
*d++ = (*t - *o) * *o * (1.0 - *o);
*d++ = (*t - *o) * ann->diffexpr_activation_output(ann, *o);
++o; ++t;
}
}
Expand Down Expand Up @@ -328,7 +335,7 @@ void genann_train(genann const *ann, double const *inputs, double const *desired
delta += forward_delta * forward_weight;
}

*d = *o * (1.0-*o) * delta;
*d = ann->diffexpr_activation_hidden(ann, *o) * delta;
++d; ++o;
}
}
Expand Down
6 changes: 6 additions & 0 deletions genann.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ typedef struct genann {
/* Which activation function to use for output. Default: gennann_act_sigmoid_cached*/
genann_actfun activation_output;

/* Derivative of the activation function, expressed in terms of the function value; i.e. f'(f_inverse(y))
* Used for backpropagation. Default: y(1-y), corresponding to the sigmoid. */
genann_actfun diffexpr_activation_hidden;
genann_actfun diffexpr_activation_output;

/* Total number of weights, and size of weights buffer. */
int total_weights;

Expand Down Expand Up @@ -97,6 +102,7 @@ void genann_write(genann const *ann, FILE *out);
void genann_init_sigmoid_lookup(const genann *ann);
double genann_act_sigmoid(const genann *ann, double a);
double genann_act_sigmoid_cached(const genann *ann, double a);
double genann_act_diffexpr_sigmoid(const genann *ann, double y);
double genann_act_threshold(const genann *ann, double a);
double genann_act_linear(const genann *ann, double a);

Expand Down