ae2f_docs
SlpTrainAND.c
Go to the documentation of this file.
1#include "ae2f/Float.h"
2#include "ae2f/errGlob.h"
3#include <ae2f/Ann/Slp.h>
4#include <stdio.h>
5#include <math.h>
6
7static ae2f_AnnAct_t Act, ActDeriv;
8
9static void
10Act(ae2f_float_t* r, ae2f_float_t x) {
11 r[0] = 1.0 / (1.0 + exp(-x));
12}
13
14static void
15ActDeriv(ae2f_float_t* r, ae2f_float_t output) {
16 output += 1e-7;
17 r[0] = output * (1.0 - output);
18}
19
20static ae2f_AnnLoss_t LossDeriv;
21
22/** Cross entrophy */
23static void
24LossDeriv(ae2f_float_t* r, const ae2f_float_t* output, const ae2f_float_t* target, size_t i, size_t c) {
25 const ae2f_float_t epsilon = 1e-7; // Small value to prevent division by zero
26 ae2f_float_t o_i = output[i];
27 // Clip output to avoid log(0) or division by zero
28 o_i = o_i < epsilon ? epsilon : (o_i > 1.0 - epsilon ? 1.0 - epsilon : o_i);
29 r[0] = (o_i - target[i]) / (c * o_i * (1.0 - o_i));
30}
31
32const ae2f_float_t inp[4][2] = { 1, 1, 0, 0, 0, 1, 1, 0 };
33const ae2f_float_t goal_andor[4][2] = {
34 {1, 1},
35 {0, 0},
36 {0, 1},
37 {0, 1}
38};
39
41
43ae2f_AnnSlp* slp = 0;
44
45size_t i, j;
46bool mismatch = 0;
47
48#define THRESHOLD 0.01
49#define DIFF_GOOD(a, b) (((a) - (b)) * ((a) - (b))) < THRESHOLD
50
51int tryand() {
52 puts("Trial: tryand");
53 err = 0;
54
55 do {
57 NULL, NULL, NULL
58 , 2
59 , 2, 3, 4
60 , &slp
61 , Act, ActDeriv, LossDeriv
62 , 0.1, 0.1
63 , &err
64 );
65
66 printf("Learningrate dump: %f, %f\n", slp->m_learningrate, slp->m_learningrate_bias);
67 printf("function points: %p %p %p\n", slp->m_act, slp->m_actderiv, slp->m_lossderiv);
68
69 if(err) break;
70
71 for(i = 0 ; i < 2 * 1 + 1; ++i) {
72 slp->m_weight[i << 1] = 0.2;
73 slp->m_weight[(i << 1) + 1] = 0.2;
74 }
75
76 for(i = 0; i < 3000; ++i) {
77 for(j = 0; j < sizeof(inp) / sizeof(inp[0]); ++j) {
79 if(err) break;
80 }
81 }
82
83 for(j = 0; j < sizeof(inp) / sizeof(inp[0]); ++j) {
85 if(err) break;
86
87 printf("Got %f, %f\n", *output, output[1]);
88 if(DIFF_GOOD(output[0], goal_andor[j][0])) {
89 printf("Match. Expected %f\n.", goal_andor[j][0]);
90 } else {
91 printf("Expected %f. Unexpected.\n", goal_andor[j][0]);
92 mismatch = 1;
93 }
94 }
95 } while(0);
96
97 if(mismatch) {
99 }
100
102 printf("Code from tryand: %d\n", err);
103
104 return err;
105}
106
107int main() {
108 return tryand();
109}
void ae2f_AnnAct_t(ae2f_float_t *ret, ae2f_float_t x)
Customisable activasion function type.
Definition Act.h:19
void ae2f_AnnLoss_t(ae2f_float_t *ret, const ae2f_float_t *out, const ae2f_float_t *goal, size_t index, size_t count)
Specify the way of calculating loss.
Definition Act.h:29
ae2f_float ae2f_float_t
Definition Float.h:38
ae2f_float_t output[1]
const ae2f_float_t inp[4][2]
ae2f_err_t err[1]
Definition MlpTrainXOR.c:40
bool mismatch
Definition SlpTrainAND.c:46
const ae2f_float_t goal_andor[4][2]
Definition SlpTrainAND.c:33
ae2f_AnnSlp * slp
Definition SlpTrainAND.c:43
#define THRESHOLD
Definition SlpTrainAND.c:48
int tryand()
Definition SlpTrainAND.c:51
#define DIFF_GOOD(a, b)
Definition SlpTrainAND.c:49
ae2f_SHAREDEXPORT void ae2f_AnnSlpTrain(ae2f_err_t *restrict const err, ae2f_AnnSlp *restrict slp, ae2f_LP(slp::inc) const ae2f_float_t *restrict inp, ae2f_LP(slp::outc) const ae2f_float_t *restrict out_desired) noexcept
Definition Slp.imp.c:110
ae2f_SHAREDEXPORT void ae2f_AnnSlpPredict(ae2f_err_t *restrict const err_opt, const ae2f_AnnSlp *restrict const _this, const ae2f_float_t *restrict const prm_in, ae2f_float_t *restrict const out) noexcept
Definition Slp.imp.c:59
ae2f_SHAREDEXPORT void ae2f_AnnSlpDel(ae2f_AnnSlp *restrict const slp) noexcept
Definition Slp.imp.c:55
ae2f_SHAREDEXPORT void ae2f_AnnSlpMk(ae2f_LP(inc *outc) ae2f_float_t *restrict const weight_opt, ae2f_LP(outc) ae2f_float_t *restrict const bias_opt, ae2f_LP(outc) ae2f_float_t *restrict const cache_opt, const size_t inc, const size_t outc, const size_t offset_opt, const size_t extra_opt, ae2f_FREE(ae2f_AnnSlpDel, __ae2f_AnnSlpDel) ae2f_AnnSlp *restrict *restrict const slp, ae2f_opt ae2f_AnnAct_t *const act, ae2f_opt ae2f_AnnAct_t *const actderiv, ae2f_AnnLoss_t *const lossderiv, ae2f_float_t learningrate, ae2f_float_t learningrate_bias, ae2f_opt ae2f_err_t *restrict const err_opt) noexcept
Definition Slp.imp.c:26
#define ae2f_errGlob_NFOUND
Found some errors, but not by parameters. The operation has failed.
Definition errGlob.h:50
uint8_t ae2f_err_t
Informs that this number represents the error.
Definition errGlob.h:19