summaryrefslogtreecommitdiffstats
path: root/example/ann_xor.c
diff options
context:
space:
mode:
authorEthan Werner <mail@ethanwerner.com>2026-06-09 01:17:41 +0400
committerEthan Werner <mail@ethanwerner.com>2026-06-09 01:17:41 +0400
commit96ecf29e8be977f51f351308ef2c7c492bb829aa (patch)
treec664aa61262e6f7627a732f0e4fbb84281b99952 /example/ann_xor.c
parentf6a144987cc403de8ad6bddd055f756120f8d405 (diff)
refactor
Diffstat (limited to 'example/ann_xor.c')
-rw-r--r--[-rwxr-xr-x]example/ann_xor.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/example/ann_xor.c b/example/ann_xor.c
index a39264c..ac1623e 100755..100644
--- a/example/ann_xor.c
+++ b/example/ann_xor.c
@@ -1,4 +1,4 @@
-// example_xor.c - Example XOR training
+// ann_xor.c - Example: training an ANN to solve XOR using backpropagation
#define ANN_IMPLEMENTATION
#include "../include/ann.h"
@@ -10,39 +10,44 @@
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);
+ ann_t ann = {0};
+ if (!ann_init(&ann, 3, (u64[]){2, 2, 1})) {
+ fprintf(stderr, "Failed to init ann\n");
+ return 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;
+ f64 input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
+ f64 output[4] = {0, 0, 0, 0};
+ f64 expected[4] = {0, 1, 1, 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],
+ for (i64 iteration = 0; iteration < 100000; iteration++) {
+ for (i64 training = 0; training < 4; training++) {
+ ann_propagation_forward(&ann, (f64 const *const)&input[training],
&output[training]);
- error += ann_error_total(&output[training], &expected[training],
- ann->layer_neuron_n[ann->layer_n - 1]);
+ (void)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],
+ ann_propagation_backward(&ann, (f64 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_train_numeric( ann, ( f64 const * const ) &i[b], &t[b], 0.03 );
}
}
- ann_print_weight(ann);
+ 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]);
+ ann_propagation_forward(&ann, (f64 const *const)&input[i], &output[i]);
+ ann_print_neuron(&ann, (f64 const *const)&input[i], &output[i]);
}
+
+ ann_free(&ann);
}