ae2f_docs
MlpTrainXORFour.cc
Go to the documentation of this file.
1#define ae2f_MAC_BUILD OFF
2
3#include "ae2f/Float.auto.h"
4#include "ae2f/Float.h"
5#include <ae2f/Ann/Mlp.h>
6#include <stdio.h>
7#include <math.h>
8
9static void Act(ae2f_float* r, ae2f_float_t x) {
10 *r = 1.0 / (1.0 + exp(-x));
11}
12
13static void ActDeriv(ae2f_float_t* r, ae2f_float_t output) {
14 *r = output * (1.0 - output);
15}
16
17static void LossDeriv(ae2f_float_t* r, const ae2f_float_t* output, const ae2f_float_t* target, size_t i, size_t c) {
18 *r = ((output[i] - target[i]) / c);
19}
20
21const ae2f_float_t inp[4][2] = {
22 {0, 0},
23 {0, 1},
24 {1, 0},
25 {1, 1}
26};
27
28const ae2f_float_t goal_xor[4] = {0, 1, 1, 0};
29
30ae2f_float_t output[1] = { 0.5 };
31ae2f_err_t err[1] = {0, };
32ae2f_AnnMlp* mlp;
33size_t lenv[] = {2, 3, 3, 1};
34
35size_t i, j, k;
36
37int main() {
38 ae2f_AnnMlpMk(err, &mlp, 4
39 , lenv
40 , 0, 0, 0
41 , LossDeriv
42 , 0, 0, 0, 0
43 , 0.006, 0.005
44 , 5, 9
45 );
46
47 assert(mlp->m_learningrate != 0);
48 assert(mlp->m_learningrate_bias != 0);
49
50 for(i = 0; i < 3; i++) {
51 mlp->m_act[i] = Act;
52 mlp->m_actderiv[i] = ActDeriv;
53 for(j = 0; j < 9; j++) {
54 mlp->m_weight[j + i * 9] = ((double)rand() / RAND_MAX) - 0.5;
55 }
56
57 for(j = 0; j < 3; j++) {
58 mlp->m_bias[j] = ((double)rand() / RAND_MAX) - 0.5;
59 }
60 }
61
62 if(err[0]) {
63 printf("[Error]: %d\n", err[0]);
64 assert(0 && "errval has occurred.");
65 }
66
67 for(j = 0; j < 4; j++) {
68 ae2f_AnnMlpPredict(err, mlp, inp[j], output);
69 assert(!err[0] && "err from predict");
70 printf("%f %f -> %f\n", inp[j][0], inp[j][1], output[0]);
71 }
72
73 for(i = 0; i < 99000; i++) {
74 for(j = 0; j < 4; j++) {
76 err, mlp, inp[j]
77 , &goal_xor[j]);
78
79 assert(!err[0] && "err from TrainAutoStream");
80 }
81 }
82
83 for(j = 0; j < 4; j++) {
84 ae2f_AnnMlpPredict(err, mlp, inp[j], output);
85 assert(!err[0] && "err from predict");
86 printf("%f %f -> %f\n", inp[j][0], inp[j][1], output[0]);
87 }
88
89 ae2f_AnnMlpDel(mlp);
90 return 0;
91}
#define OFF
Definition cmake.hpp:4
#define ae2f_float
Predefined floating point type.
Definition Float.auto.h:17
ae2f_float ae2f_float_t
Definition Float.h:38
ae2f_float_t output[1]
ae2f_AnnMlp_t mlp
const ae2f_float_t goal_xor[4]
const ae2f_float_t inp[4][2]
ae2f_err_t err[1]
Definition MlpTrainXOR.c:40
size_t lenv[]
Definition MlpTrainXOR.c:42
int main()
uint8_t ae2f_err_t
Informs that this number represents the error.
Definition errGlob.h:19
#define ae2f_AnnMlpMk
Definition Mlp.h:282
#define ae2f_AnnMlpTrainAutoStream
Definition Mlp.h:292
#define ae2f_AnnMlpPredict
Definition Mlp.h:285
#define ae2f_AnnMlpDel
Definition Mlp.h:283