ae2f_docs
Slp.h
1#ifndef ae2f_NEED_CLASS
2#define ae2f_NEED_CLASS 1
3#endif
4
5
6#ifndef ae2f_Ann_Slp_h
7#define ae2f_Ann_Slp_h
8
9#include "./Slp.core.h"
10#include "./Act.h"
11#include <ae2f/Pack/Beg.h>
12
13/**
14 * @brief
15 * # Single Layered Perceptron
16 *
17 * This is the main structure for the SLP.
18 */
19ae2f_structdef(struct, ae2f_AnnSlp)
21{
22 /**
23 * @brief
24 * Weights of the network.
25 */
26 ae2f_float_t* ae2f_restrict m_weight;
27 /**
28 * @brief
29 * Biases of the network.
30 */
31 ae2f_float_t* ae2f_restrict m_bias;
32 /**
33 * @brief
34 * Cache for layer outputs.
35 */
36 ae2f_float_t* ae2f_restrict m_cachedelta;
37
38 /** @brief cache for activation input */
39 ae2f_float_t* ae2f_restrict m_cacheact;
40
41 /**
42 * @brief
43 * SLP data.
44 */
45 ae2f_AnnSlpREG_t m_Slp[1];
46
47 /**
48 * @brief
49 * Activation function.
50 */
51 ae2f_AnnActFFN_t * m_act;
52 /**
53 * @brief
54 * Derivative of the activation function.
55 */
56 ae2f_AnnActFFN_t * m_actderiv;
57 /**
58 * @brief
59 * Derivative of the loss function.
60 */
61 ae2f_AnnLossFFN_t* m_lossderiv;
62
63 /**
64 * @brief
65 * Learning rate for weights.
66 */
67 ae2f_float_t m_learningrate;
68 /**
69 * @brief
70 * Learning rate for biases.
71 */
72 ae2f_float_t m_learningrate_bias;
73
74#if ae2f_WhenCXX(!)0
75#define ae2f_TMP
76
77 inline static void ae2f_TMP operator delete(void* end);
78 inline static void* ae2f_TMP operator new(
79 size_t oneonly
80 , ae2f_LP(inc * outc) ae2f_float_t* ae2f_restrict const weight_opt,
81 ae2f_LP(outc) ae2f_float_t* ae2f_restrict const bias_opt,
82 ae2f_LP(outc) ae2f_float_t* ae2f_restrict const cache_opt,
83
84 const size_t inc,
85 const size_t outc,
86 const size_t offset_opt,
87
88 ae2f_opt ae2f_AnnActFFN_t* const act,
89 ae2f_opt ae2f_AnnActFFN_t* const actderiv,
90 ae2f_AnnLossFFN_t* const lossderiv,
91 ae2f_float_t learningrate,
92 ae2f_float_t learningrate_bias,
93 ae2f_opt ae2f_err_t* ae2f_restrict const err_opt
94 ) throw();
95
96 inline void ae2f_TMP Predict(
97 ae2f_err_t* const err_opt
98 , ae2f_LP(_this::inc)
99 const ae2f_float_t* const prm_in
100 , ae2f_LP(_this::outc)
101 ae2f_float_t* const out
102 ) const;
103
104 inline void ae2f_TMP Follow(
105 ae2f_err_t* const reterr_opt
106 , ae2f_LP(_this::inc)
107 const ae2f_float_t* const prm_in
108 , ae2f_LP(_this::outc)
109 const ae2f_float_t* const delta
110 );
111
112 inline void ae2f_TMP Fit(
113 ae2f_err_t* const reterr_opt
114 , ae2f_LP(_this::inc) const ae2f_float_t* const prm_inp
115 , ae2f_LP(_this::outc) const ae2f_float_t* const prm_out
116 , ae2f_LP(_this::outc) const ae2f_float_t* const prm_out_desired
117 );
118
119 inline void ae2f_TMP Train(
120 ae2f_err_t* const err
121 , ae2f_LP(slp::inc) const ae2f_float_t* inp
122 , ae2f_LP(slp::outc) const ae2f_float_t* out_desired
123 );
124
125#undef ae2f_TMP
126#endif
127}
128#endif
129;
130
131#include <ae2f/Pack/End.h>
132
133
134/* @memberof ae2f_mAnnSlp
135 * @brief
136 * Desired least byte size for initialising.
137 *
138 * Cache (output size)
139 * Bias (output size)
140 * Weight (input * output)
141 */
142#define ae2f_AnnSlpFieldSz(inc, outc) ((((inc) + 2) * ((outc)) * sizeof(ae2f_float_t)) + sizeof(ae2f_AnnSlp))
143
145
146/**
147 * @brief
148 * Initiator
149 *
150 * No allocation is occurred on this function.
151 */
152ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpInit(
153 ae2f_AnnSlpREG_t* ae2f_restrict _this,
154 const size_t inc,
155 const size_t outc,
156 const size_t offset_opt,
157 ae2f_err_t* ae2f_restrict const err_opt,
158 size_t* ae2f_restrict const initsz_opt
159 ) ae2f_noexcept;
160
161/*
162 * @memberof ae2f_AnnSlp
163 * @brief
164 * Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron.
165 * See @ref ae2f_mAnnSlpInit.
166 *
167 * It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use.
168 */
169ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpMk(
170 ae2f_LP(inc * outc) ae2f_float_t* ae2f_restrict const weight_opt,
171 ae2f_LP(outc) ae2f_float_t* ae2f_restrict const bias_opt,
172 ae2f_LP(outc) ae2f_float_t* ae2f_restrict const cache_opt,
173
174 const size_t inc,
175 const size_t outc,
176 const size_t offset_opt,
177 const size_t extra_opt,
178 ae2f_FREE(ae2f_AnnSlpDel, __ae2f_AnnSlpDel)
179 ae2f_AnnSlp* ae2f_restrict * ae2f_restrict const slp,
180 ae2f_opt ae2f_AnnActFFN_t* const act,
181 ae2f_opt ae2f_AnnActFFN_t* const actderiv,
182 ae2f_AnnLossFFN_t* const lossderiv,
183 ae2f_float_t learningrate,
184 ae2f_float_t learningrate_bias,
185 ae2f_opt ae2f_err_t* ae2f_restrict const err_opt
186 ) ae2f_noexcept;
187
188ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpDel(
189 ae2f_AnnSlp* ae2f_restrict const slp
190 ) ae2f_noexcept;
191
192ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpPredict(
193 ae2f_err_t* ae2f_restrict const err_opt
194 , const ae2f_AnnSlp* ae2f_restrict const _this
195 , ae2f_LP(_this::inc)
196 const ae2f_float_t* ae2f_restrict const prm_in
197 , ae2f_LP(_this::outc)
198 ae2f_float_t* ae2f_restrict const out
199 ) ae2f_noexcept;
200
201 /**
202 * @brief
203 * Adjusts the field based on delta
204 *
205 * @param delta
206 *
207 * @param prm_in
208 *
209 * @param field
210 * Contains weights and biases
211 *
212 * @param learningrate
213 * Learning rate for weights
214 *
215 * @param learningrate_bias
216 * Learning rate for biases
217 * */
218ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpFollow(
219 ae2f_err_t* ae2f_restrict const reterr_opt
220 , const ae2f_AnnSlp* ae2f_restrict const _this
221 , ae2f_LP(_this::inc)
222 const ae2f_float_t* ae2f_restrict const prm_in
223 , ae2f_LP(_this::outc)
224 const ae2f_float_t* ae2f_restrict const delta
225 ) ae2f_noexcept;
226
227 /**
228 * @brief
229 * Adjusts the delta based on output and desired output.
230 * */
231 ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpFit(
232 ae2f_err_t* const reterr_opt
233 , const ae2f_AnnSlp* const _this
234 , ae2f_LP(_this::inc) const ae2f_float_t* ae2f_restrict const prm_inp
235 , ae2f_LP(_this::outc) const ae2f_float_t* ae2f_restrict const prm_out
236 , ae2f_LP(_this::outc) const ae2f_float_t* ae2f_restrict const prm_out_desired
237 ) ae2f_noexcept;
238
239 /** @brief Calculates the delta based on output and desired output. */
240 ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpFetchDelta(
241 ae2f_opt ae2f_err_t* ae2f_restrict const err
242 , const ae2f_AnnSlp* ae2f_restrict slp
243
244 , ae2f_LP(slp::outc) const ae2f_float_t* ae2f_restrict const out
245 , ae2f_LP(slp::outc) const ae2f_float_t* ae2f_restrict const out_desired
246
247 , ae2f_LP(slp::outc) ae2f_float_t* ae2f_restrict const retdelta
248 ) ae2f_noexcept;
249
250 /** @brief Adjusts the weights and biases based on predicted output from input and desired outpu. */
251 ae2f_extern ae2f_SHAREDCALL void ae2f_AnnSlpTrain(
252 ae2f_err_t* const ae2f_restrict err
253 , ae2f_AnnSlp* ae2f_restrict slp
254 , ae2f_LP(slp::inc) const ae2f_float_t* ae2f_restrict inp
255 , ae2f_LP(slp::outc) const ae2f_float_t* ae2f_restrict out_desired
256 ) ae2f_noexcept;
257
258#endif
259
260#if !(ae2f_MAC_BUILD) || 1
261
262#define ae2f_AnnSlpInit __ae2f_AnnSlpInit_C
263#define ae2f_AnnSlpMk __ae2f_AnnSlpMk_C
264#define ae2f_AnnSlpDel __ae2f_AnnSlpDel_C
265#define ae2f_AnnSlpPredict __ae2f_AnnSlpPredict_C
266#define ae2f_AnnSlpFollow __ae2f_AnnSlpFollow_C
267#define ae2f_AnnSlpFit __ae2f_AnnSlpFit_C
268#define ae2f_AnnSlpTrain __ae2f_AnnSlpTrain_C
269#define ae2f_AnnSlpFetchDelta __ae2f_AnnSlpFetchDelta_C
270
271#endif /** macbuild */
272#endif
273
274
275
276#include "./Slp.auto.h"
277
278#define __ae2f_AnnSlpMk_imp(v_mk, p_weight_opt, p_bias_opt, p_cache_opt,
279 inc, outc, offset_opt, extra_opt, act, actderiv, lossderiv, lr_w, lr_b)
280 __ae2f_AnnSlpMkVerbose_imp(
281 v_mk, p_weight_opt, p_bias_opt, p_cache_opt
282 , ae2f_reinterpret_cast(ae2f_float_t* const, NULL)
283 , inc, outc, offset_opt, extra_opt, act, actderiv, lossderiv, lr_w, lr_b)
284
285#if ae2f_WhenCXX(!)0 && !defined(ae2f_Ann_Slp_cc) && ae2f_NEED_CLASS
286#define ae2f_Ann_Slp_cc
287
288#define ae2f_TMP ae2f_AnnSlp::
289
290inline void ae2f_TMP operator delete(void* end) {
291 ae2f_AnnSlpDel(ae2f_reinterpret_cast(ae2f_AnnSlp*, end));
292}
293
294inline void* ae2f_TMP operator new(
295 size_t oneonly
296 , ae2f_LP(inc * outc) ae2f_float_t* const weight_opt,
297 ae2f_LP(outc) ae2f_float_t* const bias_opt,
298 ae2f_LP(outc) ae2f_float_t* const cache_opt,
299
300 const size_t inc,
301 const size_t outc,
302 const size_t offset_opt,
303
304 ae2f_opt ae2f_AnnActFFN_t* const act,
305 ae2f_opt ae2f_AnnActFFN_t* const actderiv,
306 ae2f_AnnLossFFN_t* const lossderiv,
307 ae2f_float_t learningrate,
308 ae2f_float_t learningrate_bias,
309 ae2f_opt ae2f_err_t* const err_opt
310 ) throw()
311{
312 if(oneonly != sizeof(ae2f_AnnSlp)) return 0;
313 ae2f_AnnSlp* __v[1] = { NULL };
314
316 weight_opt, bias_opt, cache_opt, inc, outc, offset_opt, 0
317 , __v, act, actderiv, lossderiv, learningrate, learningrate_bias, err_opt);
318
319 return __v[0];
320}
321
322inline void ae2f_TMP Predict(
323 ae2f_err_t* const err_opt
324 , ae2f_LP(_this::inc)
325 const ae2f_float_t* const prm_in
326 , ae2f_LP(_this::outc)
327 ae2f_float_t* const out
328 ) const
329{
330 ae2f_AnnSlpPredict(err_opt, this, prm_in, out);
331}
332
333inline void ae2f_TMP Follow(
334 ae2f_err_t* const reterr_opt
335 , ae2f_LP(_this::inc)
336 const ae2f_float_t* const prm_in
337 , ae2f_LP(_this::outc)
338 const ae2f_float_t* const delta
339 )
340{
341 ae2f_AnnSlpFollow(reterr_opt, this, prm_in, delta);
342}
343
344inline void ae2f_TMP Fit(
345 ae2f_err_t* const reterr_opt
346 , ae2f_LP(_this::inc) const ae2f_float_t* const prm_inp
347 , ae2f_LP(_this::outc) const ae2f_float_t* const prm_out
348 , ae2f_LP(_this::outc) const ae2f_float_t* const prm_out_desired
349 )
350{
351 ae2f_AnnSlpFit(reterr_opt, this, prm_inp, prm_out, prm_out_desired);
352}
353
354inline void ae2f_TMP Train(
355 ae2f_err_t* const err
356 , ae2f_LP(slp::inc) const ae2f_float_t* inp
357 , ae2f_LP(slp::outc) const ae2f_float_t* out_desired
358 )
359{
360 ae2f_AnnSlpTrain(err, this, inp, out_desired);
361}
362
363#undef ae2f_TMP
364
365#endif
#define ae2f_structdef(key, name)
Definition Cast.h:110
#define ae2f_reinterpret_cast(t, v)
Definition Cast.h:52
#define unless(...)
Invokes when condition is false.
Definition Cast.h:103
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
Definition Cast.h:88
#define ae2f_WhenCXX(...)
Appears when the current language is C.
Definition Cxx.h:37
#define ae2f_NONE
Literally nothing.
Definition Cxx.h:16
#define ae2f_LP(...)
Definition Guide.h:23
#define ae2f_FREE(...)
Definition Guide.h:33
#define ae2f_opt
Definition Guide.h:26
#define ae2f_errGlob_ALLOC_FAILED
stdlib allocating functions (malloc, calloc, realloc) has been failed.
Definition errGlob.h:40
uint8_t ae2f_err_t
Informs that this number represents the error.
Definition errGlob.h:19
#define ae2f_errGlob_PTR_IS_NULL
Failed to refer the pointer either l-value inside the function.
Definition errGlob.h:32
#define __ae2f_MACRO_GENERATED
Definition Conv.auto.h:2
#define ae2f_MAC_BUILD
Definition Util.h:5
#define ae2f_NEED_CLASS
Definition Mlp.cl.c:8
#define OPER_NONE
Definition Mlp.def.c:21
#define OPER_NEG
Definition Mlp.def.c:20
#define __ae2f_AnnMlpSz_imp(ret_sz, outc, weightc, depth, szswap, act, actderiv, deltastream, outcache, weight, bias)
Definition Mlp.auto.h:272
#define __ae2f_AnnMlpBwd_imp(v_tmp, v_send, slp_then, retdelta_then, deltaseed, actderiv_then, inp)
delta to delta
Definition Mlp.auto.h:588
#define __ae2f_AnnMlpHidDeltaSingle_imp(v_single, slp, weight, delta, iidx)
Definition Mlp.auto.h:566
#define __ae2f_AnnMlpPredictStream_imp(v_predict, mlp, inp, out, sz, weight, bias, outcache, act_opt)
Definition Mlp.auto.h:554
#define __ae2f_AnnMlpMk_imp(reg_mk, prm_depth, pprm_szvector, propptr_szswap_opt, lppfn_act_opt, lppfn_actderiv_opt, pfn_lossderiv, propptr_deltastream_opt, propptr_outcache_opt, propptr_weight_opt, propptr_bias_opt, prm_learningrate, prm_learningrate_bias, prm_offset_opt, prm_extra_opt)
Automatically allocates ae2f_AnnMlp and store its pointer at (reg_mk).m_mkbase.
Definition Mlp.auto.h:163
#define __ae2f_AnnMlpInitWithOutSz_imp(v_mlp, v_init, depth, outsz, weightsz, szvector, szswap_opt, act, actderiv, lossderiv, deltastream, outcache, weight, bias, learningrate, learningrate_bias)
Definition Mlp.auto.h:303
#define __ae2f_AnnMlpTrainPrimal(OPER_NEG, OPER_NONE, reterr, mlp, inp, out, out_desired)
Definition Mlp.auto.h:884
#define __ae2f_AnnMlpFollowPrimal_imp(OPER_NEG, OPER_NONE, v_follow, mlp, inp, delta, lenv, outstream, deltacache, weight, bias, learningrate, learningrate_bias, actderiv)
Definition Mlp.auto.h:615
#define __ae2f_AnnMlpPredictPrimal_imp(OPER_NEG, OPER_NONE, v_predict, mlp, inp, out, sz, weight, bias, outcache, act_opt)
layer must be more than 2
Definition Mlp.auto.h:397
#define __ae2f_AnnMlpTrainAutoPrimal(OPER_NEG, OPER_NONE, reterr, mlp, inp, out_desired)
Definition Mlp.auto.h:915
#define __ae2f_AnnMlpPredictPrimal(OPER_NEG, OPER_NONE, reterr, mlp, inp, out)
Definition Mlp.auto.h:524
#define __ae2f_AnnMlpFollowPrimal(OPER_NEG, OPER_NONE, reterr, mlp, inp, delta)
Definition Mlp.auto.h:785
#define __ae2f_AnnMlpTrainPrimal_imp(OPER_NEG, OPER_NONE, v_train, mlp, inp, out, out_desired, lenv, outstream, deltacache, weight, bias, learningrate, learningrate_bias, act, actderiv, lossderiv)
Definition Mlp.auto.h:822
#define __ae2f_AnnSlpFollow_imp(reg_follow, prm_slp, pprm_in, pprm_delta, ptr_weight, ptr_bias, prm_learningrate, prm_learningrate_bias)
Definition Slp.auto.h:435
#define __ae2f_AnnSlpFit_C(...)
Definition Slp.auto.h:870
#define __ae2f_AnnSlpFollow_C(...)
Definition Slp.auto.h:524
#define __ae2f_AnnSlpFetchDelta_imp(tmp_delta, prm_slp, pprm_out, pprm_out_desired, fn_actderiv, fn_lossderiv, pret_delta)
Definition Slp.auto.h:586
#define __ae2f_AnnSlpFetchDelta_C(...)
Definition Slp.auto.h:701
#define __ae2f_AnnSlpDel_C
Definition Slp.auto.h:20
#define __ae2f_AnnSlpTrain_C
Definition Slp.auto.h:984
#define __ae2f_AnnSlpInit_C
Definition Slp.auto.h:139
#define __ae2f_AnnSlpPredict_imp(reg_predict, prmreg_slp, pprm_in, ret, ptr_outcache, pprm_weight, pprm_bias, fn_act)
Definition Slp.auto.h:278
#define __ae2f_AnnSlpMk_C
Definition Slp.auto.h:275
#define __ae2f_AnnSlpPredict_C(...)
Definition Slp.auto.h:376
#define ae2f_AnnSlpMk
Definition Slp.h:263
#define ae2f_AnnSlpDel
Definition Slp.h:264
#define ae2f_AnnSlpTrain
Definition Slp.h:268
#define ae2f_AnnSlpPredict
Definition Slp.h:265
#define ae2f_AnnSlpFollow
Definition Slp.h:266
#define ae2f_AnnSlpFit
Definition Slp.h:267
#define ae2f_MAC(...)
Definition mac.h:28