1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
// ann_xor.c - Example: training an ANN to solve XOR using backpropagation
#define ANN_IMPLEMENTATION
#include "../include/ann.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(0));
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);
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 (i64 iteration = 0; iteration < 100000; iteration++) {
for (i64 training = 0; training < 4; training++) {
ann_propagation_forward(&ann, (f64 const *const)&input[training],
&output[training]);
(void)ann_error_total(&output[training], &expected[training],
ann.layer_neuron_n[ann.layer_n - 1]);
// Train using backpropagation
ann_propagation_backward(&ann, (f64 const *const)&input[training],
&output[training], &expected[training], 0.4);
// Train using numeric integration
// ann_train_numeric( ann, ( f64 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, (f64 const *const)&input[i], &output[i]);
ann_print_neuron(&ann, (f64 const *const)&input[i], &output[i]);
}
ann_free(&ann);
}
|