From f6a144987cc403de8ad6bddd055f756120f8d405 Mon Sep 17 00:00:00 2001 From: Ethan Werner Date: Wed, 17 Apr 2024 16:00:00 -0700 Subject: archival commit --- example/ann_xor.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 example/ann_xor.c (limited to 'example/ann_xor.c') diff --git a/example/ann_xor.c b/example/ann_xor.c new file mode 100755 index 0000000..a39264c --- /dev/null +++ b/example/ann_xor.c @@ -0,0 +1,48 @@ +// example_xor.c - Example XOR training + +#define ANN_IMPLEMENTATION +#include "../include/ann.h" + +#include +#include +#include + +int main(void) { + srand(time(0)); + + ann_t *ann = ann_init(3, (uint_t[]){2, 2, 1}); + ann_set_activation(ann, SIGMOID, SIGMOID); + ann_random(ann); + + fp_t input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; + fp_t output[4] = {0, 0, 0, 0}; + fp_t expected[4] = {0, 1, 1, 0}; + fp_t error = 0; + + for (int_t iteration = 0; iteration < 100000; iteration++) { + for (int_t training = 0; training < 4; training++) { + ann_propagation_forward(ann, (fp_t const *const)&input[training], + &output[training]); + + error += ann_error_total(&output[training], &expected[training], + ann->layer_neuron_n[ann->layer_n - 1]); + + // Train using backpropagation + ann_propagation_backward(ann, (fp_t const *const)&input[training], + &output[training], &expected[training], 0.4); + + // Train using numeric integration + // ann_train_numeric( ann, ( fp_t const * const ) &i[b], &t[b], 0.03 ); + } + } + + ann_print_weight(ann); + + fputs("------------------------------------------------------------\n\n", + stderr); + + for (int i = 0; i < 4; i++) { + ann_propagation_forward(ann, (fp_t const *const)&input[i], &output[i]); + ann_print_neuron(ann, (fp_t const *const)&input[i], &output[i]); + } +} -- cgit v1.3.1