diff --git a/CMakeLists.txt b/CMakeLists.txt index aac31f3..8457071 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,14 +19,17 @@ if(NOT TARGET ${PROJECT_NAME}) "include/ae2f/Ann/Sp.h.c/clean.script.h" "include/ae2f/Ann/Sp.h.c/cleandef.h" "include/ae2f/Ann/Sp.h.cxx/Sp.hh" + "include/ae2f/Ann/Sp.h.cxx/mSp.hh" "include/ae2f/Ann/Sp.h.cxx/imp.hh" "include/ae2f/Ann/Slp.h" "include/ae2f/Ann/Slp.h.cxx/Slp.hh" + "include/ae2f/Ann/Slp.h.cxx/mSlp.hh" "include/ae2f/Ann/Slp.h.cxx/imp.hh" "include/ae2f/Ann/Mlp.h" "include/ae2f/Ann/Mlp.h.cxx/Mlp.hh" + "include/ae2f/Ann/Mlp.h.cxx/mMlp.hh" "include/ae2f/Ann/Mlp.h.cxx/imp.hh" ) target_link_libraries(Ann-H INTERFACE Core) @@ -77,9 +80,16 @@ if(ae2fCL_needed) include ae2fCL ${PROJECT_SOURCE_DIR}/Config/CL-Ann-H.cmake.in "include/ae2fCL/Ann.h" + "include/ae2fCL/Ann/Sp.h.cxx/Sp.hh" + "include/ae2fCL/Ann/Sp.h.cxx/mSp.hh" "include/ae2fCL/Ann/Sp.h.cxx/imp.hh" "include/ae2fCL/Ann/Sp.h" + + "include/ae2fCL/Ann/Slp.h.cxx/Slp.hh" + "include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh" + "include/ae2fCL/Ann/Slp.h.cxx/imp.hh" + "include/ae2fCL/Ann/Slp.h" ) ae2f_RulerMeasure( diff --git a/README.md b/README.md index 212396f..20c8f72 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Ann -> Rel-v0.0.0 +> Rel-v1.0.0 ANN interfaces with basic implementation for example. @@ -14,7 +14,7 @@ ANN interfaces with basic implementation for example. - cmake # CL-Ann -> Rel-v2.1.0 +> Rel-v3.1.0 A simple ann project with opencl written in C. diff --git a/include/ae2f/Ann/Mlp.h b/include/ae2f/Ann/Mlp.h index 2077e39..62aaf9d 100644 --- a/include/ae2f/Ann/Mlp.h +++ b/include/ae2f/Ann/Mlp.h @@ -16,101 +16,80 @@ #include /// @brief -/// Element of @ref ae2f_AnnMlp -typedef ae2f_AnnSlp ae2f_AnnMlpEl; +/// Element of @ref ae2f_mAnnMlp +typedef ae2f_mAnnSlp ae2f_mAnnMlpEl; #if ae2f_WhenCXX(!) 0 -struct ae2f_AnnMlp : ae2f_AnnSlp { -#include "Mlp.h.cxx/Mlp.hh" +struct ae2f_mAnnMlp { +#include "Mlp.h.cxx/mMlp.hh" }; #else -/// @class ae2f_AnnMlp -/// @extends ae2f_AnnSlp +/// @class ae2f_mAnnMlp +/// @extends ae2f_mAnnSlp /// @brief /// # Multi Layered Perceptron /// /// -typedef ae2f_AnnSlp ae2f_AnnMlp; +typedef ae2f_mAnnSlp ae2f_mAnnMlp; #endif -/// @memberof ae2f_AnnMlp -/// @details ae2f_AnnSlpPerVPad -#define ae2f_AnnMlpLayerVPad ae2f_AnnSlpPerVPad -/// @memberof ae2f_AnnMlp -#define ae2f_AnnMlpLayerV(mlp, i, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_AnnMlpEl*, ae2f_AnnMlpLayerVPad(mlp, __VA_ARGS__)[i] + 1) +typedef union ae2f_AnnMlp { + ae2f_mAnnSlp Slp; + ae2f_mAnnMlp Mlp; -/// @memberof ae2f_AnnMlp -#define ae2f_AnnMlpInitSz(layerc, add) \ -(sizeof(ae2f_AnnMlp) + (sizeof(void*) * ((layerc))) + (sizeof(size_t) * 1) + (add)) + #if ae2f_WhenCXX(!)0 + #include "Mlp.h.cxx/Mlp.hh" + #endif +} ae2f_AnnMlp; + +/// @memberof ae2f_mAnnMlp +/// @details ae2f_mAnnSlpPerVPad +#define ae2f_mAnnMlpLayerVPad ae2f_mAnnSlpPerVPad + +/// @memberof ae2f_mAnnMlp +#define ae2f_mAnnMlpLayerV(mlp, i, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_mAnnMlpEl*, ae2f_mAnnMlpLayerVPad(mlp, __VA_ARGS__)[i] + 1) + +/// @memberof ae2f_mAnnMlp +#define ae2f_mAnnMlpInitSz(layerc, add) \ +(sizeof(ae2f_mAnnMlp) + (sizeof(void*) * ((layerc))) + (sizeof(size_t) * 1) + (add)) /// @brief /// Predict function api. \n -/// For details see @ref ae2f_AnnMlpPredict_t. -typedef ae2f_AnnSlpPredict_t ae2f_AnnMlpPredict_t; +/// For details see @ref ae2f_mAnnMlpPredict_t. +typedef ae2f_mAnnSlpPredict_t ae2f_mAnnMlpPredict_t; /// @brief /// Training function api. \n -/// For details see @ref ae2f_AnnMlpTrain_t -typedef ae2f_AnnSlpTrain_t ae2f_AnnMlpTrain_t; +/// For details see @ref ae2f_mAnnMlpTrain_t +typedef ae2f_mAnnSlpTrain_t ae2f_mAnnMlpTrain_t; /// @brief /// Cleaning function api. -typedef ae2f_AnnSlpClean_t ae2f_AnnMlpClean_t; +typedef ae2f_mAnnSlpClean_t ae2f_mAnnMlpClean_t; -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief /// The predicted max buffer count among all perceptron's possible length of I/O. -#define ae2f_AnnMlpLayerBuffCount(mlp, ...) \ -ae2f_AnnSlpX(mlp, __VA_ARGS__ size_t*, __VA_ARGS__) - -/// @memberof ae2f_AnnMlp -/// @brief -/// Its length is @ref (*ae2f_AnnMlpLayerBuffCount(mlp) * mlp->layerc, 3 * sizeof(ae2f_float_t)). -#define ae2f_AnnMlpCache(mlp, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t**, ae2f_AnnMlpLayerBuffCount(mlp, __VA_ARGS__) + 1) - -/// @memberof ae2f_AnnMlp -/// @brief -#define ae2f_AnnMlpX(mlp,type,...) \ -ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_AnnMlpCache(mlp, __VA_ARGS__) + 1) +#define ae2f_mAnnMlpLayerBuffCount(mlp, ...) \ +ae2f_mAnnSlpX(mlp, __VA_ARGS__ size_t*, __VA_ARGS__) -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief -/// Predict function call. \n -/// See @ref ae2f_AnnSlpPredict -#define ae2f_AnnMlpPredict ae2f_AnnSlpPredict +/// Its length is @ref (*ae2f_mAnnMlpLayerBuffCount(mlp) * mlp->layerc, 3 * sizeof(ae2f_float_t)). +#define ae2f_mAnnMlpCache(mlp, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t**, ae2f_mAnnMlpLayerBuffCount(mlp, __VA_ARGS__) + 1) -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief -/// Training function call. \n -/// See @ref ae2f_AnnSlpTrain -#define ae2f_AnnMlpTrain ae2f_AnnSlpTrain +#define ae2f_mAnnMlpX(mlp,type,...) \ +ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_mAnnMlpCache(mlp, __VA_ARGS__) + 1) -/// @memberof ae2f_AnnMlp -/// @brief -/// See @ref ae2f_AnnSlpTrainA -#define ae2f_AnnMlpTrainA ae2f_AnnSlpTrainA - -/// @memberof ae2f_AnnMlp -/// @brief -/// See @ref ae2f_AnnSlpTrainB -#define ae2f_AnnMlpTrainB ae2f_AnnSlpTrainB - -/// @memberof ae2f_AnnMlp -/// @brief -/// Deleting function. \n -/// We are reusing the previous definition since the process is literally saem. -/// -/// See @ref ae2f_AnnSlpDel -#define ae2f_AnnMlpDel ae2f_AnnSlpDel -#define ae2f_AnnMlpClean ae2f_AnnSlpClean - -/// @memberof ae2f_AnnMlp +/// @memberof ae2f_mAnnMlp /// @brief /// d /// @param _this @@ -125,8 +104,8 @@ ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_AnnMlpCache(mlp, __VA_ARGS__) + 1) /// @param errret_opt /// @return ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnMlpInit( - ae2f_AnnMlp* _this, +size_t ae2f_mAnnMlpInit( + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -151,6 +130,10 @@ ae2f_AnnMlp* ae2f_AnnMlpMk( ae2f_err_t* errret_opt ) noexcept; + +#define ae2f_AnnMlpClean ae2f_AnnSlpClean +#define ae2f_AnnMlpDel ae2f_AnnSlpDel + #if ae2f_WhenCXX(!) 0 #include "Mlp.h.cxx/imp.hh" #endif diff --git a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh index 5925d98..237c47c 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/Mlp.hh @@ -1,19 +1,9 @@ -/** - * @file Mlp.hh - * @author ae2f - * @brief - * @version 0.1 - * @date 2025-02-19 - * - * @copyright Copyright (c) 2025 - * - * Additional definition for @ref ae2f_AnnMlp, @ref ae2f_AnnMlpEl - * - */ - #include "../Mlp.h" #define ae2f_TMP -constexprfun ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a); -constexprfun const ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) const; -#undef ae2f_TMP \ No newline at end of file +inline ae2f_TMP ~ae2f_AnnMlp(); +#undef ae2f_TMP + +private: +uint8_t szero; +constexpr ae2f_AnnMlp() : szero(0) {} \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/imp.hh b/include/ae2f/Ann/Mlp.h.cxx/imp.hh index 2f63292..6717f7c 100644 --- a/include/ae2f/Ann/Mlp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Mlp.h.cxx/imp.hh @@ -11,13 +11,27 @@ * */ -#define ae2f_TMP ae2f_AnnMlp:: +#define ae2f_TMP ae2f_mAnnMlp:: + +constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) { + return ae2f_mAnnMlpLayerV(this->_this, a); +} +constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const { + return ae2f_mAnnMlpLayerV(this->_this, a, const); +} -constexprfun ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) { - return ae2f_AnnMlpLayerV(this, a); +constexprfun size_t* ae2f_TMP PercPad(size_t a) { + return ae2f_mAnnMlpLayerVPad(this->_this)[a]; } -constexprfun const ae2f_AnnMlpEl* ae2f_TMP Perc(size_t a) const { - return ae2f_AnnMlpLayerV(this, a, const); +constexprfun const size_t* ae2f_TMP PercPad(size_t a) const { + return ae2f_mAnnMlpLayerVPad(this->_this, const)[a]; } +#undef ae2f_TMP + + +#define ae2f_TMP ae2f_AnnMlp:: +inline ae2f_TMP ~ae2f_AnnMlp() { + ae2f_AnnMlpClean(this); +} #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh new file mode 100644 index 0000000..f3340ba --- /dev/null +++ b/include/ae2f/Ann/Mlp.h.cxx/mMlp.hh @@ -0,0 +1,25 @@ +/** + * @file Mlp.hh + * @author ae2f + * @brief + * @version 0.1 + * @date 2025-02-19 + * + * @copyright Copyright (c) 2025 + * + * Additional definition for @ref ae2f_mAnnMlp, @ref ae2f_mAnnMlpEl + * + */ + +#include "../Mlp.h" + +#define ae2f_TMP +constexprfun ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a); +constexprfun const ae2f_mAnnMlpEl* ae2f_TMP Perc(size_t a) const; + +constexprfun size_t* ae2f_TMP PercPad(size_t); +constexprfun const size_t* ae2f_TMP PercPad(size_t) const; +#undef ae2f_TMP + +private: +ae2f_mAnnSlp _this[1]; \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h b/include/ae2f/Ann/Slp.h index a8c164d..cf2b0b1 100644 --- a/include/ae2f/Ann/Slp.h +++ b/include/ae2f/Ann/Slp.h @@ -15,22 +15,22 @@ #include "./Sp.h" #include -struct ae2f_AnnSlp; +struct ae2f_mAnnSlp; /// @brief cleaning function -typedef ae2f_err_t ae2f_AnnSlpClean_t (ae2f_struct ae2f_AnnSlp*) noexcept; +typedef ae2f_err_t ae2f_mAnnSlpClean_t (ae2f_struct ae2f_mAnnSlp*) noexcept; /// @brief /// Predict one output from multiple inputs. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[out] outret_opt /// That one predicted. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @return State -typedef ae2f_err_t ae2f_AnnSlpPredict_t ( - const ae2f_struct ae2f_AnnSlp* _this, +typedef ae2f_err_t ae2f_mAnnSlpPredict_t ( + const ae2f_struct ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept; @@ -39,7 +39,7 @@ typedef ae2f_err_t ae2f_AnnSlpPredict_t ( /// Training function, multiple input from multiple output. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[in] delta_optA /// Delta vector when you pre-calculated it. /// Its count is one. @@ -47,8 +47,8 @@ typedef ae2f_err_t ae2f_AnnSlpPredict_t ( /// Desired output. /// @param learningrate /// Learning rate -typedef ae2f_err_t ae2f_AnnSlpTrain_t ( - ae2f_struct ae2f_AnnSlp* _this, +typedef ae2f_err_t ae2f_mAnnSlpTrain_t ( + ae2f_struct ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -56,19 +56,19 @@ typedef ae2f_err_t ae2f_AnnSlpTrain_t ( ) noexcept; /// @brief -/// The element type for @ref ae2f_AnnSlp. -typedef struct ae2f_AnnSp ae2f_AnnSlpEl; +/// The element type for @ref ae2f_mAnnSlp. +typedef struct ae2f_mAnnSp ae2f_mAnnSlpEl; /// @brief /// # Single Layered Perceptron /// /// Multiple input, multiple output. \n /// For predicting & training operations are able to be parallel. -typedef struct ae2f_AnnSlp { +typedef struct ae2f_mAnnSlp { /// @brief /// True when its shape is same as expected. \n - /// Expected structure is generated by @ref ae2f_AnnSpInit. + /// Expected structure is generated by @ref ae2f_mAnnSpInit. int expected; /// @brief @@ -85,80 +85,80 @@ typedef struct ae2f_AnnSlp { /// @brief /// Cleaning function - ae2f_AnnSlpClean_t* vClean; + ae2f_mAnnSlpClean_t* vClean; /// @brief /// Predict function - ae2f_AnnSlpPredict_t* vPredict; + ae2f_mAnnSlpPredict_t* vPredict; /// @brief /// Training function - ae2f_AnnSlpTrain_t* vTrain; + ae2f_mAnnSlpTrain_t* vTrain; - #if ae2f_WhenCXX(1) + 0 - #include "Slp.h.cxx/Slp.hh" + #if ae2f_WhenCXX(!)0 + #include "Slp.h.cxx/mSlp.hh" #endif -} ae2f_AnnSlp; +} ae2f_mAnnSlp; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Input padding for perceptron. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSlpPerVPad(slp, ...) \ +#define ae2f_mAnnSlpPerVPad(slp, ...) \ (ae2f_CmpGetMem(slp, expected, 0) ? \ -ae2f_reinterpret_cast(__VA_ARGS__ size_t* __VA_ARGS__ *, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSlp*, slp) + 1) : \ +ae2f_reinterpret_cast(__VA_ARGS__ size_t* __VA_ARGS__ *, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSlp*, slp) + 1) : \ 0) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Perceptron vector. \n -/// Its length is same as @ref ae2f_AnnSlp::outc. +/// Its length is same as @ref ae2f_mAnnSlp::outc. /// -/// Valid when @ref ae2f_AnnSlp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSlp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSlpPerV(slp, i, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ ae2f_AnnSlpEl*, (ae2f_AnnSlpPerVPad(slp, __VA_ARGS__)[i] + 1)) +#define ae2f_mAnnSlpPerV(slp, i, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ ae2f_mAnnSlpEl*, (ae2f_mAnnSlpPerVPad(slp, __VA_ARGS__)[i] + 1)) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Additional buffer allocated. -#define ae2f_AnnSlpX(slp, type, ...) \ -ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f_AnnSlpPerVPad(slp, __VA_ARGS__) + (slp)->layerc) : 0) +#define ae2f_mAnnSlpX(slp, type, ...) \ +ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f_mAnnSlpPerVPad(slp, __VA_ARGS__) + (slp)->layerc) : 0) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// Desired least byte size for initialising. -#define ae2f_AnnSlpInitSz(outc, off) ((off) + sizeof(ae2f_AnnSlp) + (outc) * sizeof(void*)) +#define ae2f_mAnnSlpInitSz(outc, off) ((off) + sizeof(ae2f_mAnnSlp) + (outc) * sizeof(void*)) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpPredict_t. -#define ae2f_AnnSlpPredict ae2f_AnnSpPredict +/// Caller of @ref ae2f_mAnnSlpPredict_t. +#define ae2f_mAnnSlpPredict ae2f_mAnnSpPredict -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t. -#define ae2f_AnnSlpTrain ae2f_AnnSpTrain +/// Caller of @ref ae2f_mAnnSlpTrain_t. +#define ae2f_mAnnSlpTrain ae2f_mAnnSpTrain -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t \n +/// Caller of @ref ae2f_mAnnSlpTrain_t \n /// Train with Pre-calculated delta. -#define ae2f_AnnSlpTrainA ae2f_AnnSpTrainA +#define ae2f_mAnnSlpTrainA ae2f_mAnnSpTrainA -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Caller of @ref ae2f_AnnSlpTrain_t \n +/// Caller of @ref ae2f_mAnnSlpTrain_t \n /// Train for desired output: goal. -#define ae2f_AnnSlpTrainB ae2f_AnnSpTrainB +#define ae2f_mAnnSlpTrainB ae2f_mAnnSpTrainB -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief Cleaning function -#define ae2f_AnnSlpClean ae2f_AnnSpClean +#define ae2f_mAnnSlpClean ae2f_mAnnSpClean -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -182,10 +182,10 @@ ae2f_reinterpret_cast(__VA_ARGS__ type, ae2f_CmpGetMem(slp, expected, 0) ? (ae2f /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2f_mAnnSlpInit( + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -197,7 +197,7 @@ size_t ae2f_AnnSlpInit( ae2f_err_t* err_opt ) noexcept; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -219,11 +219,11 @@ size_t ae2f_AnnSlpInit( /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) -#define ae2f_AnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ -ae2f_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) +#define ae2f_mAnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ +ae2f_mAnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief /// @param[out] _this /// Allocated raw vector. @@ -245,16 +245,23 @@ ae2f_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_o /// State number when needed /// @return /// Its desired size -/// @ref ae2f_AnnSlpInitSz(outc, offset_opt) -#define ae2f_AnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ -ae2f_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) +/// @ref ae2f_mAnnSlpInitSz(outc, offset_opt) +#define ae2f_mAnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ +ae2f_mAnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) + +typedef union ae2f_AnnSlp { + ae2f_mAnnSlp Slp; + #if ae2f_WhenCXX(!) 0 + #include "Slp.h.cxx/Slp.hh" + #endif +} ae2f_AnnSlp; /// @memberof ae2f_AnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. +/// See @ref ae2f_mAnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL ae2f_AnnSlp* ae2f_AnnSlpMk( const size_t* incs_optA, @@ -268,34 +275,39 @@ ae2f_AnnSlp* ae2f_AnnSlpMk( ae2f_err_t* err_opt ) noexcept; -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. +/// See @ref ae2f_mAnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkA(incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @memberof ae2f_AnnSlp +/// @memberof ae2f_mAnnSlp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSlpInit. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. +/// See @ref ae2f_mAnnSlpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. #define ae2f_AnnSlpMkB(ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) \ ae2f_AnnSlpMk(0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt) -/// @fn ae2f_AnnSlpDel -/// @memberof ae2f_AnnSlp +/// @fn ae2f_mAnnSlpDel +/// @memberof ae2f_mAnnSlp /// @param[out] _this /// This memory will be freed. -ae2f_AnnDelDef(ae2f_AnnSlp) noexcept { +ae2f_AnnDelDef(ae2f_mAnnSlp) noexcept { #include "Sp.h.c/clean.script.h" } -#if ae2f_WhenCXX(1) + 0 +#define ae2f_AnnSlpClean(prm_Slp) ae2f_mAnnSlpClean(&(prm_Slp)->Slp) +#define ae2f_AnnSlpDel(prm_Slp) ae2f_mAnnSlpDel(&(prm_Slp)->Slp) + +#if ae2f_WhenCXX(!)0 #include "Slp.h.cxx/imp.hh" #endif +#include + #endif \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/Slp.hh b/include/ae2f/Ann/Slp.h.cxx/Slp.hh index 661ca4a..0ba859d 100644 --- a/include/ae2f/Ann/Slp.h.cxx/Slp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/Slp.hh @@ -1,44 +1,11 @@ -/** - * @file Slp.hh - * @author ae2f - * @brief - * @version 0.1 - * @date 2025-02-13 - * - * @copyright Copyright (c) 2025 - * - */ - -#include "../Slp.h" - #define ae2f_TMP -inline ae2f_err_t ae2f_TMP Predict( - const ae2f_float_t* in, - ae2f_float_t* out_opt -) const noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - const ae2f_float_t* delta_optA, - ae2f_float_t learningrate -) noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - ae2f_float_t* goal_optB, - ae2f_float_t learningrate -) noexcept; - inline ae2f_TMP ~ae2f_AnnSlp(); -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); +private: + +uint8_t _; +constexprfun ae2f_AnnSlp() : _(0) {} -constexprfun ae2f_AnnSlpEl* ae2f_TMP Perc(size_t); -constexprfun const ae2f_AnnSlpEl* ae2f_TMP Perc(size_t) const; #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/imp.hh b/include/ae2f/Ann/Slp.h.cxx/imp.hh index e05878f..34957de 100644 --- a/include/ae2f/Ann/Slp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Slp.h.cxx/imp.hh @@ -11,52 +11,61 @@ #include "../Slp.h" -#pragma region ae2f_AnnSlp -#define ae2f_TMP ae2f_AnnSlp:: +#pragma region ae2f_mAnnSlp +#define ae2f_TMP ae2f_mAnnSlp:: inline ae2f_err_t ae2f_TMP Predict( const ae2f_float_t* in, ae2f_float_t* out_opt ) const noexcept { - return ae2f_AnnSlpPredict(this, in, out_opt); + return ae2f_mAnnSlpPredict(this, in, out_opt); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSlpTrainA(this, in, delta_optA, learningrate); + return ae2f_mAnnSlpTrainA(this, in, delta_optA, learningrate); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* in, - ae2f_float_t* goal_optB, + const ae2f_float_t* goal_optB, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSlpTrainB(this, in, goal_optB, learningrate); -} - -inline ae2f_TMP ~ae2f_AnnSlp() { - ae2f_AnnSlpClean(this); + return ae2f_mAnnSlpTrainB(this, in, goal_optB, learningrate); } template constexprfun const T* ae2f_TMP X() const { - return ae2f_AnnSlpX(this, T*, const); + return ae2f_mAnnSlpX(this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2f_AnnSlpX(this, T*); + return ae2f_mAnnSlpX(this, T*); } -constexprfun ae2f_AnnSlpEl* ae2f_TMP Perc(size_t i) { - return ae2f_AnnSlpPerV(this, i); +constexprfun ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t i) { + return ae2f_mAnnSlpPerV(this, i); } -constexprfun const ae2f_AnnSlpEl* ae2f_TMP Perc(size_t i) const { - return ae2f_AnnSlpPerV(this, i, const); +constexprfun const ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t i) const { + return ae2f_mAnnSlpPerV(this, i, const); +} + +constexprfun size_t* ae2f_TMP PercPad(size_t i) { + return ae2f_mAnnSlpPerVPad(this)[i]; +} +constexprfun const size_t* ae2f_TMP PercPad(size_t i) const { + return ae2f_mAnnSlpPerVPad(this, const)[i]; } #undef ae2f_TMP -#pragma endregion \ No newline at end of file +#pragma endregion + +#define ae2f_TMP ae2f_AnnSlp:: +inline ae2f_TMP ~ae2f_AnnSlp() { + ae2f_AnnSlpClean(this); +} +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Slp.h.cxx/mSlp.hh b/include/ae2f/Ann/Slp.h.cxx/mSlp.hh new file mode 100644 index 0000000..200244e --- /dev/null +++ b/include/ae2f/Ann/Slp.h.cxx/mSlp.hh @@ -0,0 +1,45 @@ +/** + * @file Slp.hh + * @author ae2f + * @brief + * @version 0.1 + * @date 2025-02-13 + * + * @copyright Copyright (c) 2025 + * + */ + +#include "../Slp.h" + +#define ae2f_TMP + +inline ae2f_err_t ae2f_TMP Predict( + const ae2f_float_t* in, + ae2f_float_t* out_opt +) const noexcept; + +inline ae2f_err_t ae2f_TMP TrainA( + const ae2f_float_t* in, + const ae2f_float_t* delta_optA, + ae2f_float_t learningrate +) noexcept; + +inline ae2f_err_t ae2f_TMP TrainB( + const ae2f_float_t* in, + const ae2f_float_t* goal_optB, + ae2f_float_t learningrate +) noexcept; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +constexprfun ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t); +constexprfun const ae2f_mAnnSlpEl* ae2f_TMP Perc(size_t) const; + +constexprfun size_t* ae2f_TMP PercPad(size_t); +constexprfun const size_t* ae2f_TMP PercPad(size_t) const; + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h b/include/ae2f/Ann/Sp.h index 703fff0..1684b4c 100644 --- a/include/ae2f/Ann/Sp.h +++ b/include/ae2f/Ann/Sp.h @@ -22,19 +22,19 @@ #include "Sp.h.c/cleandef.h" -typedef struct ae2f_AnnSp ae2f_AnnSp; +typedef struct ae2f_mAnnSp ae2f_mAnnSp; /// @brief /// Predict one output from multiple inputs. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[out] outret_opt /// That one predicted. \n /// its count is 1. /// @return State -typedef ae2f_err_t ae2f_AnnSpPredict_t ( - const ae2f_AnnSp* _this, +typedef ae2f_err_t ae2f_mAnnSpPredict_t ( + const ae2f_mAnnSp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept; @@ -43,7 +43,7 @@ typedef ae2f_err_t ae2f_AnnSpPredict_t ( /// Train for desired output: @ref goal. /// @param[in] in /// Input vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// @param[in] delta_optA /// Delta vector when you pre-calculated it. /// Its count is one. @@ -51,8 +51,8 @@ typedef ae2f_err_t ae2f_AnnSpPredict_t ( /// Desired output. /// @param learningrate /// Learning rate -typedef ae2f_err_t ae2f_AnnSpTrain_t ( - ae2f_AnnSp* _this, +typedef ae2f_err_t ae2f_mAnnSpTrain_t ( + ae2f_mAnnSp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t goal_optB, @@ -61,18 +61,16 @@ typedef ae2f_err_t ae2f_AnnSpTrain_t ( /// @brief /// Clean the class. -typedef ae2f_err_t ae2f_AnnSpClean_t (ae2f_AnnSp*) noexcept; - -#undef ae2f_AnnSp +typedef ae2f_err_t ae2f_mAnnSpClean_t (ae2f_mAnnSp*) noexcept; /// @brief /// # Single Perceptron /// /// Multiple input, one output. -struct ae2f_AnnSp { +struct ae2f_mAnnSp { /// @brief /// True when its shape is same as expected. \n - /// Expected structure is generated by @ref ae2f_AnnSpInit. + /// Expected structure is generated by @ref ae2f_mAnnSpInit. int expected; /// @brief count of inputs to predict. @@ -84,62 +82,62 @@ struct ae2f_AnnSp { ae2f_AnnAct_t* Act; /// @brief Cleaning function - ae2f_AnnSpClean_t* vClean; + ae2f_mAnnSpClean_t* vClean; /// @brief Predict function - ae2f_AnnSpPredict_t* vPredict; + ae2f_mAnnSpPredict_t* vPredict; /// @brief Training function - ae2f_AnnSpTrain_t* vTrain; + ae2f_mAnnSpTrain_t* vTrain; #if ae2f_WhenCXX(1) + 0 - #include "Sp.h.cxx/Sp.hh" + #include "Sp.h.cxx/mSp.hh" #endif }; -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Bias Address. \n /// Its count is @ref 1. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSpB(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSp*, _this) + 1) : 0) +#define ae2f_mAnnSpB(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSp*, _this) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Actual Weights Vector. \n -/// Its count is @ref ae2f_AnnSp::inc. +/// Its count is @ref ae2f_mAnnSp::inc. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. -#define ae2f_AnnSpW(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? (ae2f_AnnSpB(_this, __VA_ARGS__) + 1) : 0) +#define ae2f_mAnnSpW(_this, ...) (ae2f_CmpGetMem(_this, expected, 0) ? (ae2f_mAnnSpB(_this, __VA_ARGS__) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Additional allocated byte vector. /// -/// Valid when @ref ae2f_AnnSp::expected is 1. \n +/// Valid when @ref ae2f_mAnnSp::expected is 1. \n /// When not, it is null. /// @param prm_type The return value. -#define ae2f_AnnSpX(_this, prm_type, ...) ((_this) ? (_this)->expected ? ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_AnnSpW(_this, __VA_ARGS__) + (_this)->inc) : ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_static_cast(__VA_ARGS__ ae2f_AnnSp*, _this) + 1) : 0) +#define ae2f_mAnnSpX(_this, prm_type, ...) ((_this) ? (_this)->expected ? ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_mAnnSpW(_this, __VA_ARGS__) + (_this)->inc) : ae2f_reinterpret_cast(__VA_ARGS__ prm_type, ae2f_static_cast(__VA_ARGS__ ae2f_mAnnSp*, _this) + 1) : 0) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Calculates the least byte size for your design of structure. \n -/// See @ref ae2f_AnnSpInit +/// See @ref ae2f_mAnnSpInit /// /// @param off /// Additional allocating size for some reason. \n -/// Accessible with @ref ae2f_AnnSpX. +/// Accessible with @ref ae2f_mAnnSpX. /// /// @param inc /// Count of weight vector. -#define ae2f_AnnSpInitSz(off, inc) ((off + 1) + sizeof(ae2f_AnnSp) + (inc) * sizeof(ae2f_float_t)) +#define ae2f_mAnnSpInitSz(off, inc) ((off + 1) + sizeof(ae2f_mAnnSp) + (inc) * sizeof(ae2f_float_t)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Initialises a typical( @ref ae2f_AnnSp::expected ) perceptron. +/// Initialises a typical( @ref ae2f_mAnnSp::expected ) perceptron. /// @param[out] _this_opt /// @param inum /// Input count, (aka weights count, inc) @@ -155,10 +153,10 @@ struct ae2f_AnnSp { /// @return /// Size of param [_this] needed. \n /// @ref -/// ae2f_AnnSpInitSz(offset_opt, inum) +/// ae2f_mAnnSpInitSz(offset_opt, inum) ae2f_extern ae2f_SHAREDCALL -size_t ae2f_AnnSpInit( - ae2f_AnnSp* _this_opt, +size_t ae2f_mAnnSpInit( + ae2f_mAnnSp* _this_opt, size_t inum, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -167,12 +165,20 @@ size_t ae2f_AnnSpInit( size_t offset_opt ) noexcept; +typedef union ae2f_AnnSp { + ae2f_mAnnSp Sp; + + #if ae2f_WhenCXX(!)0 + #include "Sp.h.cxx/Sp.hh" + #endif +} ae2f_AnnSp; + /// @memberof ae2f_AnnSp /// @brief -/// Makes a typical( @ref ae2f_AnnSp::expected ) perceptron. -/// See @ref ae2f_AnnSpInit. +/// Makes a typical( @ref ae2f_mAnnSp::expected ) perceptron. +/// See @ref ae2f_mAnnSpInit. /// -/// It is heap-allocated. pass the output @ref ae2f_AnnSpDel after use. +/// It is heap-allocated. pass the output @ref ae2f_mAnnSpDel after use. ae2f_extern ae2f_SHAREDCALL ae2f_AnnSp* ae2f_AnnSpMk( size_t inum, @@ -183,49 +189,52 @@ ae2f_AnnSp* ae2f_AnnSpMk( size_t offset_opt ) noexcept; -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Clean the class. -#define ae2f_AnnSpClean(_this) \ +#define ae2f_mAnnSpClean(_this) \ ((_this) ? (_this)->vClean ? (_this)->vClean(_this) : ae2f_errGlob_OK : ae2f_errGlob_PTR_IS_NULL) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Clean the class and it will try to free the pointer. /// @param[out] _this /// Its memory will be freed. -/// @name ae2f_AnnSlpDel -/// @memberof ae2f_AnnSlp +/// @name ae2f_mAnnSlpDel +/// @memberof ae2f_mAnnSlp /// @param[out] _this /// This memory will be freed. -ae2f_AnnDelDef(ae2f_AnnSp) noexcept { +ae2f_AnnDelDef(ae2f_mAnnSp) noexcept { #include "Sp.h.c/clean.script.h" } -/// @memberof ae2f_AnnSp +#define ae2f_AnnSpClean(prm_Sp) ae2f_mAnnSpClean(&((prm_Sp)->Sp)) +#define ae2f_AnnSpDel(prm_Sp) ae2f_mAnnSpDel(&((prm_Sp)->Sp)) + +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpPredict_t -#define ae2f_AnnSpPredict(_this, in, out_opt) \ +/// Caller of @ref ae2f_mAnnSpPredict_t +#define ae2f_mAnnSpPredict(_this, in, out_opt) \ (ae2f_CmpGetMem(_this, vPredict, 0) ? (_this)->vPredict(_this, in, out_opt) : (ae2f_errGlob_IMP_NOT_FOUND | ae2f_errGlob_PTR_IS_NULL)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpTrain_t -#define ae2f_AnnSpTrain(_this, in, delta_optA, goal_optB, learningrate) \ +/// Caller of @ref ae2f_mAnnSpTrain_t +#define ae2f_mAnnSpTrain(_this, in, delta_optA, goal_optB, learningrate) \ (ae2f_CmpGetMem(_this, vTrain, 0) ? (_this)->vTrain(_this, in, delta_optA, goal_optB, learningrate) : (ae2f_errGlob_IMP_NOT_FOUND | ae2f_errGlob_PTR_IS_NULL)) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief -/// Caller of @ref ae2f_AnnSpTrain_t \n +/// Caller of @ref ae2f_mAnnSpTrain_t \n /// Train with Pre-calculated delta. -#define ae2f_AnnSpTrainA(_this, in, predelta, learningrate) \ -ae2f_AnnSpTrain(_this, in, predelta, 0, learningrate) +#define ae2f_mAnnSpTrainA(_this, in, predelta, learningrate) \ +ae2f_mAnnSpTrain(_this, in, predelta, 0, learningrate) -/// @memberof ae2f_AnnSp +/// @memberof ae2f_mAnnSp /// @brief /// Train for desired output: goal. -#define ae2f_AnnSpTrainB(_this, in, goal, learningrate) \ -ae2f_AnnSpTrain(_this, in, 0, goal, learningrate) +#define ae2f_mAnnSpTrainB(_this, in, goal, learningrate) \ +ae2f_mAnnSpTrain(_this, in, 0, goal, learningrate) #include diff --git a/include/ae2f/Ann/Sp.h.cxx/Sp.hh b/include/ae2f/Ann/Sp.h.cxx/Sp.hh index 6618a06..2fa56d4 100644 --- a/include/ae2f/Ann/Sp.h.cxx/Sp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/Sp.hh @@ -1,49 +1,11 @@ -/** - * @file Sp.hh - * @author ae2f - * @brief - * @version 0.2 - * @date 2025-02-09 - * - * @copyright Copyright (c) 2025 - * - */ - -#include "../Sp.h" - #define ae2f_TMP - -inline ae2f_err_t ae2f_TMP Predict( - const ae2f_float_t* in, - ae2f_float_t* out_opt -) const noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - const ae2f_float_t* delta_optA, - ae2f_float_t learningrate -) noexcept; - -inline ae2f_err_t ae2f_TMP Train( - const ae2f_float_t* in, - ae2f_float_t goal_optB, - ae2f_float_t learningrate -) noexcept; - -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); -static constexprfun size_t ae2f_TMP sz(size_t off, size_t inc); inline ae2f_TMP ~ae2f_AnnSp(); -constexprfun const ae2f_float_t* ae2f_TMP B() const; -constexprfun ae2f_float_t* ae2f_TMP B(); +private: -constexprfun const ae2f_float_t* ae2f_TMP W() const; -constexprfun ae2f_float_t* ae2f_TMP W(); +uint8_t _; +constexprfun ae2f_AnnSp() : _(0) {} #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h.cxx/imp.hh b/include/ae2f/Ann/Sp.h.cxx/imp.hh index e4fd60a..6115bb2 100644 --- a/include/ae2f/Ann/Sp.h.cxx/imp.hh +++ b/include/ae2f/Ann/Sp.h.cxx/imp.hh @@ -11,61 +11,65 @@ #include "../Sp.h" -#define ae2f_TMP ae2f_AnnSp:: +#define ae2f_TMP ae2f_mAnnSp:: inline ae2f_err_t ae2f_TMP Predict( const ae2f_float_t* in, ae2f_float_t* out_opt ) const noexcept { - return ae2f_AnnSpPredict(this, in, out_opt); + return ae2f_mAnnSpPredict(this, in, out_opt); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainA( const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSpTrainA(this, in, delta_optA, learningrate); + return ae2f_mAnnSpTrainA(this, in, delta_optA, learningrate); } -inline ae2f_err_t ae2f_TMP Train( +inline ae2f_err_t ae2f_TMP TrainB( const ae2f_float_t* in, ae2f_float_t goal_optB, ae2f_float_t learningrate ) noexcept { - return ae2f_AnnSpTrainB(this, in, goal_optB, learningrate); + return ae2f_mAnnSpTrainB(this, in, goal_optB, learningrate); } constexprfun const ae2f_float_t* ae2f_TMP B() const { - return ae2f_AnnSpB(this, const); + return ae2f_mAnnSpB(this, const); } constexprfun ae2f_float_t* ae2f_TMP B() { - return ae2f_AnnSpB(this); + return ae2f_mAnnSpB(this); } constexprfun const ae2f_float_t* ae2f_TMP W() const { - return ae2f_AnnSpW(this, const); + return ae2f_mAnnSpW(this, const); } constexprfun ae2f_float_t* ae2f_TMP W() { - return ae2f_AnnSpW(this, ); + return ae2f_mAnnSpW(this, ); } template constexprfun const T* ae2f_TMP X() const { - return ae2f_AnnSpX(this, T*, const); + return ae2f_mAnnSpX(this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2f_AnnSpX(this, T*, ); + return ae2f_mAnnSpX(this, T*, ); } -inline ae2f_TMP ~ae2f_AnnSp() { - ae2f_AnnSpClean(this); +constexprfun size_t ae2f_TMP sz(size_t off, size_t inc) { + return ae2f_mAnnSpInitSz(off, inc); } -constexprfun size_t ae2f_TMP sz(size_t off, size_t inc) { - return ae2f_AnnSpInitSz(off, inc); +#undef ae2f_TMP + +#define ae2f_TMP ae2f_AnnSp:: + +inline ae2f_TMP ~ae2f_AnnSp() { + ae2f_mAnnSpClean(&this->Sp); } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2f/Ann/Sp.h.cxx/mSp.hh b/include/ae2f/Ann/Sp.h.cxx/mSp.hh new file mode 100644 index 0000000..ffa5610 --- /dev/null +++ b/include/ae2f/Ann/Sp.h.cxx/mSp.hh @@ -0,0 +1,48 @@ +/** + * @file Sp.hh + * @author ae2f + * @brief + * @version 0.2 + * @date 2025-02-09 + * + * @copyright Copyright (c) 2025 + * + */ + +#include "../Sp.h" + +#define ae2f_TMP + +inline ae2f_err_t ae2f_TMP Predict( + const ae2f_float_t* in, + ae2f_float_t* out_opt +) const noexcept; + +inline ae2f_err_t ae2f_TMP TrainA( + const ae2f_float_t* in, + const ae2f_float_t* delta_optA, + ae2f_float_t learningrate +) noexcept; + +inline ae2f_err_t ae2f_TMP TrainB( + const ae2f_float_t* in, + ae2f_float_t goal_optB, + ae2f_float_t learningrate +) noexcept; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +static constexprfun size_t ae2f_TMP sz(size_t off, size_t inc); + +constexprfun const ae2f_float_t* ae2f_TMP B() const; +constexprfun ae2f_float_t* ae2f_TMP B(); + +constexprfun const ae2f_float_t* ae2f_TMP W() const; +constexprfun ae2f_float_t* ae2f_TMP W(); + + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Mlp.h b/include/ae2fCL/Ann/Mlp.h index ba0d678..f96e88f 100644 --- a/include/ae2fCL/Ann/Mlp.h +++ b/include/ae2fCL/Ann/Mlp.h @@ -6,7 +6,7 @@ ae2f_extern ae2f_SHAREDCALL size_t ae2fCL_AnnMlpInit( - ae2f_AnnMlp* _this, + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -33,8 +33,6 @@ ae2f_AnnMlp* ae2fCL_AnnMlpMk( cl_int* errnfound_opt ) noexcept; -#define ae2fCL_AnnMlpInitSz ae2f_AnnMlpInitSz -#define ae2fCL_AnnMlpDel ae2f_AnnMlpDel -#define ae2fCL_AnnMlpClean ae2f_AnnMlpClean +#define ae2fCL_mAnnMlpInitSz ae2f_mAnnMlpInitSz #endif \ No newline at end of file diff --git a/include/ae2fCL/Ann/Slp.h b/include/ae2fCL/Ann/Slp.h index e9b0f11..f99c093 100644 --- a/include/ae2fCL/Ann/Slp.h +++ b/include/ae2fCL/Ann/Slp.h @@ -18,9 +18,31 @@ #include #include +typedef struct ae2fCL_mAnnSlpMemX { + cl_mem In; + + /** @brief Has weights been changed */ + bool Changed; +} ae2fCL_mAnnSlpMemX; + +#if ae2f_WhenCXX(!) 0 +struct ae2fCL_mAnnSlp { +#include "Slp.h.cxx/mSlp.hh" +}; +#else + +/** + * @class ae2fCL_mAnnSlp + * @brief + * SLP with OpenCL acceleration. + */ +typedef ae2f_mAnnSlp ae2fCL_mAnnSlp; + +#endif + ae2f_extern ae2f_SHAREDCALL -size_t ae2fCL_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2fCL_mAnnSlpInit( + ae2fCL_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -33,29 +55,23 @@ size_t ae2fCL_AnnSlpInit( cl_int* err_nfound_opt ) noexcept; -#define ae2fCL_AnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ -ae2fCL_AnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +#define ae2fCL_mAnnSlpInitA(_this, incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ +ae2fCL_mAnnSlpInit(_this, incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) -#define ae2fCL_AnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ -ae2fCL_AnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +#define ae2fCL_mAnnSlpInitB(_this, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ +ae2fCL_mAnnSlpInit(_this, 0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) +typedef union ae2fCL_AnnSlp { + ae2f_mAnnSlp Slp; + ae2fCL_mAnnSlp CL_Slp; -#if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSlp : ae2f_AnnSlp { -#include "Slp.h.cxx/Slp.hh" -}; -#else - -/** - * @class ae2fCL_AnnSlp - * @brief - * SLP with OpenCL acceleration. - */ -typedef ae2f_AnnSlp ae2fCL_AnnSlp; -#endif + #if ae2f_WhenCXX(!)0 + #include "Slp.h.cxx/Slp.hh" + #endif +} ae2fCL_AnnSlp; ae2f_extern ae2f_SHAREDCALL -ae2f_AnnSlp* ae2fCL_AnnSlpMk( +ae2fCL_AnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -68,36 +84,36 @@ ae2f_AnnSlp* ae2fCL_AnnSlpMk( cl_int* err_nfound_opt ) noexcept; +#define ae2fCL_AnnSlpClean ae2f_AnnSlpClean +#define ae2fCL_AnnSlpDel ae2f_AnnSlpDel + #define ae2fCL_AnnSlpMkA(incs, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ ae2fCL_AnnSlpMk(incs, 0, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) #define ae2fCL_AnnSlpMkB(ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) \ ae2fCL_AnnSlpMk(0, ginc, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err_opt, err_nfound_opt) -typedef struct ae2fCL_AnnSlpMemX { - cl_mem In; - - /** @brief Has weights been changed */ - bool Changed; -} ae2fCL_AnnSlpMemX; - /** * @param outc * @param off * */ -#define ae2fCL_AnnSlpInitSz(outc, off) \ - ae2f_AnnSlpInitSz(outc, (off) + sizeof(ae2fCL_AnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) +#define ae2fCL_mAnnSlpInitSz(outc, off) \ + ae2f_mAnnSlpInitSz(outc, (off) + sizeof(ae2fCL_mAnnSlpMemX) + (sizeof(ae2f_float_t) * outc)) -#define ae2fCL_AnnSlpAdd(slp, ...) \ - ae2f_AnnSlpX(slp, ae2fCL_AnnSlpMemX*, __VA_ARGS__) +#define ae2fCL_mAnnSlpAdd(slp, ...) \ + ae2f_mAnnSlpX(slp, ae2fCL_mAnnSlpMemX*, __VA_ARGS__) -#define ae2fCL_AnnSlpOutCache(slp, ...) \ - ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2fCL_AnnSlpAdd(slp, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSlpOutCache(slp, ...) \ + ae2f_reinterpret_cast(__VA_ARGS__ ae2f_float_t*, ae2fCL_mAnnSlpAdd(slp, __VA_ARGS__) + 1) -#define ae2fCL_AnnSlpX(slp, type, ...) \ - ae2f_reinterpret_cast(__VA_ARGS__ type*, ae2fCL_AnnSlpOutCache(slp, __VA_ARGS__) + (slp)->outc) +#define ae2fCL_mAnnSlpX(slp, type, ...) \ + ae2f_reinterpret_cast(__VA_ARGS__ type*, ae2fCL_mAnnSlpOutCache(slp, __VA_ARGS__) + (slp)->outc) #include +#if ae2f_WhenCXX(!)0 +#include "Slp.h.cxx/imp.hh" +#endif + #endif diff --git a/include/ae2fCL/Ann/Slp.h.cxx/imp.hh b/include/ae2fCL/Ann/Slp.h.cxx/imp.hh new file mode 100644 index 0000000..f01f66f --- /dev/null +++ b/include/ae2fCL/Ann/Slp.h.cxx/imp.hh @@ -0,0 +1,30 @@ +#include "../Slp.h" + +#define ae2f_TMP ae2fCL_mAnnSlp:: + +constexprfun const ae2fCL_mAnnSlpMemX* ae2f_TMP Add() const { + return ae2fCL_mAnnSlpAdd(_this, const); +} +constexprfun ae2fCL_mAnnSlpMemX* ae2f_TMP Add() { + return ae2fCL_mAnnSlpAdd(_this); +} + +constexprfun const ae2f_float_t* ae2f_TMP OutCache() const { + return ae2fCL_mAnnSlpOutCache(_this, const); +} + +constexprfun ae2f_float_t* ae2f_TMP OutCache() { + return ae2fCL_mAnnSlpOutCache(_this); +} + +template +constexprfun const T* ae2f_TMP X() const { + return ae2fCL_mAnnSlpX(_this, T, const); +} + +template +constexprfun T* ae2f_TMP X() { + return ae2fCL_mAnnSlpX(_this, T, const); +} + +#undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh b/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh new file mode 100644 index 0000000..1da3035 --- /dev/null +++ b/include/ae2fCL/Ann/Slp.h.cxx/mSlp.hh @@ -0,0 +1,20 @@ +#include "../Slp.h" + +#define ae2f_TMP + +constexprfun const ae2fCL_mAnnSlpMemX* ae2f_TMP Add() const; +constexprfun ae2fCL_mAnnSlpMemX* ae2f_TMP Add(); + +constexprfun const ae2f_float_t* ae2f_TMP OutCache() const; +constexprfun ae2f_float_t* ae2f_TMP OutCache(); + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +#undef ae2f_TMP + +private: +ae2f_mAnnSlp _this[1]; \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h b/include/ae2fCL/Ann/Sp.h index f889a81..2ed68e7 100644 --- a/include/ae2fCL/Ann/Sp.h +++ b/include/ae2fCL/Ann/Sp.h @@ -7,7 +7,7 @@ * * @copyright Copyright (c) 2025 * - * I am adding @ref ae2f_AnnSp ability to + * I am adding @ref ae2f_mAnnSp ability to * make calculation parallelly with OpenCL. * * While keeping the basic structure of base class. @@ -22,35 +22,29 @@ #include #if ae2f_WhenCXX(!) 0 -struct ae2fCL_AnnSp : ae2f_AnnSp { - #include "./Sp.h.cxx/Sp.hh" +struct ae2fCL_mAnnSp { +#include "./Sp.h.cxx/mSp.hh" }; #else -/// @class ae2fCL_AnnSp -/// @extends ae2f_AnnSp +/// @class ae2fCL_mAnnSp +/// @extends ae2f_mAnnSp /// @brief /// Hello World -typedef ae2f_AnnSp ae2fCL_AnnSp; +typedef ae2f_mAnnSp ae2fCL_mAnnSp; #endif -#define ae2fCL_AnnSpPredict ae2f_AnnSpPredict -#define ae2fCL_AnnSpTrain ae2f_AnnSpTrain -#define ae2fCL_AnnSpTrainA ae2f_AnnSpTrainA -#define ae2fCL_AnnSpTrainB ae2f_AnnSpTrainB -#define ae2fCL_AnnSpClean ae2f_AnnSpClean -#define ae2fCL_AnnSpB ae2f_AnnSpB -#define ae2fCL_AnnSpW ae2f_AnnSpW -#define ae2fCL_AnnSpDel ae2f_AnnSpDel +#define ae2fCL_mAnnSpClean ae2f_mAnnSpClean +#define ae2fCL_mAnnSpDel ae2f_mAnnSpDel -#define ae2fCL_AnnSpInitSz(off, inc) ae2f_AnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) -#define ae2fCL_AnnSpWCl(per, ...) ae2f_AnnSpX(per, cl_mem*, __VA_ARGS__) -#define ae2fCL_AnnSpIOCl(per, ...) (ae2fCL_AnnSpWCl(per, __VA_ARGS__) + 1) -#define ae2fCL_AnnSpX(per, type, ...) ae2f_reinterpret_cast(__VA_ARGS__ type, ae2fCL_AnnSpIOCl(per, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSpInitSz(off, inc) ae2f_mAnnSpInitSz((off) + (cl_mem_SIZE << 2), inc) +#define ae2fCL_mAnnSpWCl(per, ...) ae2f_mAnnSpX(per, cl_mem*, __VA_ARGS__) +#define ae2fCL_mAnnSpIOCl(per, ...) (ae2fCL_mAnnSpWCl(per, __VA_ARGS__) + 1) +#define ae2fCL_mAnnSpX(per, type, ...) ae2f_reinterpret_cast(__VA_ARGS__ type, ae2fCL_mAnnSpIOCl(per, __VA_ARGS__) + 1) ae2f_extern ae2f_SHAREDCALL -size_t ae2fCL_AnnSpInit( - ae2fCL_AnnSp* perc_opt, +size_t ae2fCL_mAnnSpInit( + ae2fCL_mAnnSp* perc_opt, size_t icount, const ae2f_float_t* w_opt, ae2f_AnnAct_t Act, @@ -60,6 +54,18 @@ size_t ae2fCL_AnnSpInit( size_t off_opt ) noexcept; +typedef union ae2fCL_AnnSp { + ae2fCL_mAnnSp CL_Sp; + ae2f_mAnnSp Sp; + + #if ae2f_WhenCXX(!)0 + #include "Sp.h.cxx/Sp.hh" + #endif +} ae2fCL_AnnSp; + +#define ae2fCL_AnnSpClean ae2f_AnnSpClean +#define ae2fCL_AnnSpDel ae2f_AnnSpDel + ae2f_extern ae2f_SHAREDCALL ae2fCL_AnnSp* ae2fCL_AnnSpMk( size_t icount, diff --git a/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh b/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh index d78684f..6a892a5 100644 --- a/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh +++ b/include/ae2fCL/Ann/Sp.h.cxx/Sp.hh @@ -2,16 +2,12 @@ #define ae2f_TMP -constexprfun cl_mem* ae2f_TMP WCl(); -constexprfun const cl_mem* ae2f_TMP WCl() const; +inline ae2f_TMP ~ae2fCL_AnnSp(); -constexprfun cl_mem* ae2f_TMP IOCl(); -constexprfun const cl_mem* ae2f_TMP IOCl() const; +private: + +uint8_t _; +constexprfun ae2fCL_AnnSp() : _(0) {} -template -constexprfun const T* ae2f_TMP X() const; - -template -constexprfun T* ae2f_TMP X(); #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h.cxx/imp.hh b/include/ae2fCL/Ann/Sp.h.cxx/imp.hh index 2eeba58..d82515c 100644 --- a/include/ae2fCL/Ann/Sp.h.cxx/imp.hh +++ b/include/ae2fCL/Ann/Sp.h.cxx/imp.hh @@ -2,30 +2,40 @@ -#define ae2f_TMP ae2fCL_AnnSp:: +#define ae2f_TMP ae2fCL_mAnnSp:: constexprfun cl_mem* ae2f_TMP WCl() { - return ae2fCL_AnnSpWCl(this); + return ae2fCL_mAnnSpWCl(_this); } constexprfun const cl_mem* ae2f_TMP WCl() const { - return ae2fCL_AnnSpWCl(this, const); + return ae2fCL_mAnnSpWCl(_this, const); } constexprfun cl_mem* ae2f_TMP IOCl() { - return ae2fCL_AnnSpIOCl(this); + return ae2fCL_mAnnSpIOCl(_this); } constexprfun const cl_mem* ae2f_TMP IOCl() const { - return ae2fCL_AnnSpIOCl(this, const); + return ae2fCL_mAnnSpIOCl(_this, const); } template constexprfun const T* ae2f_TMP X() const { - return ae2fCL_AnnSpX(this, T*, const); + return ae2fCL_mAnnSpX(_this, T*, const); } template constexprfun T* ae2f_TMP X() { - return ae2fCL_AnnSpX(this, T*); + return ae2fCL_mAnnSpX(_this, T*); +} + +#undef ae2f_TMP + + +#define ae2f_TMP ae2fCL_AnnSp:: + +inline ae2f_TMP ~ae2fCL_AnnSp() { + ae2fCL_AnnSpClean(this); + return; } #undef ae2f_TMP \ No newline at end of file diff --git a/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh b/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh new file mode 100644 index 0000000..102cdfd --- /dev/null +++ b/include/ae2fCL/Ann/Sp.h.cxx/mSp.hh @@ -0,0 +1,20 @@ +#include "../Sp.h" + +#define ae2f_TMP + +constexprfun cl_mem* ae2f_TMP WCl(); +constexprfun const cl_mem* ae2f_TMP WCl() const; + +constexprfun cl_mem* ae2f_TMP IOCl(); +constexprfun const cl_mem* ae2f_TMP IOCl() const; + +template +constexprfun const T* ae2f_TMP X() const; + +template +constexprfun T* ae2f_TMP X(); + +#undef ae2f_TMP + +private: +ae2f_mAnnSp _this[1]; \ No newline at end of file diff --git a/src/ann-h/Mlp.h b/src/ann-h/Mlp.h index cb98d7d..5635682 100644 --- a/src/ann-h/Mlp.h +++ b/src/ann-h/Mlp.h @@ -18,12 +18,12 @@ #include "Mlp/Predict.h" #include -static ae2f_AnnMlpPredict_t Predict; -static ae2f_AnnMlpTrain_t Train; -static ae2f_AnnMlpClean_t Clean; +static ae2f_mAnnMlpPredict_t Predict; +static ae2f_mAnnMlpTrain_t Train; +static ae2f_mAnnMlpClean_t Clean; static ae2f_err_t Predict ( - const ae2f_AnnSlp* _this, + const ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* outret_opt ) noexcept { @@ -31,7 +31,7 @@ static ae2f_err_t Predict ( if(!in) return ae2f_errGlob_PTR_IS_NULL; if(!outret_opt) return ae2f_errGlob_DONE_HOWEV | ae2f_errGlob_PTR_IS_NULL; - size_t A = *ae2f_AnnMlpLayerBuffCount(_this, const); + size_t A = *ae2f_mAnnMlpLayerBuffCount(_this, const); union { const ae2f_float_t* CF; @@ -39,18 +39,18 @@ static ae2f_err_t Predict ( uintptr_t P; } tmpVi = { in }, - tmpVo = { *ae2f_AnnMlpCache(_this, const) }; + tmpVo = { *ae2f_mAnnMlpCache(_this, const) }; if(!tmpVo.P) return ae2f_errGlob_ALLOC_FAILED | ae2f_errGlob_PTR_IS_NULL; ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_AnnMlpLayerV(_this, i, const); + const ae2f_mAnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); if(i == _this->layerc - 1) tmpVo.F = outret_opt; - err |= ae2f_AnnSlpPredict( + err |= ae2f_mAnnSlpPredict( slp, tmpVi.CF, tmpVo.F ); @@ -62,7 +62,7 @@ static ae2f_err_t Predict ( } static ae2f_err_t Train ( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -71,13 +71,13 @@ static ae2f_err_t Train ( ae2f_err_t ret = 0; #define return(n) { ret |= n; goto RET; } const size_t - MAXBUFFCOUNT_FOR_LAYER = *ae2f_AnnMlpLayerBuffCount(_this), + MAXBUFFCOUNT_FOR_LAYER = *ae2f_mAnnMlpLayerBuffCount(_this), SkipInput = MAXBUFFCOUNT_FOR_LAYER * _this->layerc; #define _cache_ALLOCCOUNT SkipInput * 3 #define _cache_SIZE sizeof(ae2f_float_t) - ae2f_float_t* const _cache = *ae2f_AnnMlpCache(_this); + ae2f_float_t* const _cache = *ae2f_mAnnMlpCache(_this); #define _cache_Out _cache if(!_cache) return(ae2f_errGlob_ALLOC_FAILED); @@ -92,9 +92,9 @@ static ae2f_err_t Train ( for(size_t i = _this->layerc - 1; i != ((size_t)-1); i--) { - ae2f_AnnSlp - * const LAYER = ae2f_AnnMlpLayerV(_this, i), - * const LAYERNXT = ae2f_AnnMlpLayerV(_this, i + 1); + ae2f_mAnnSlp + * const LAYER = ae2f_mAnnMlpLayerV(_this, i), + * const LAYERNXT = ae2f_mAnnMlpLayerV(_this, i + 1); if(i == _this->layerc - 1) { if (delta_optA) @@ -121,7 +121,7 @@ static ae2f_err_t Train ( ); } - ret |= ae2f_AnnSlpTrainA( + ret |= ae2f_mAnnSlpTrainA( LAYER, i ? cache_Out2 + MAXBUFFCOUNT_FOR_LAYER * (i - 1) : in, cache_Deltas + (i) * MAXBUFFCOUNT_FOR_LAYER, @@ -136,26 +136,27 @@ static ae2f_err_t Train ( } static ae2f_err_t Clean( - ae2f_AnnMlp* _this + ae2f_mAnnMlp* _this ) noexcept { if(!_this) return ae2f_errGlob_PTR_IS_NULL; - for(size_t i = 0; i < _this->layerc - 1; i++) { + for(size_t i = 0; i < _this->layerc; i++) { union { size_t** unused; union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; - } perc = { .unused = ae2f_AnnMlpLayerVPad(_this) + i }; + } perc = { .unused = ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad++; - ae2f_AnnSlpClean(perc.u->slp); + ae2f_mAnnSlpClean(perc.u->slp); free(--perc.u->pad); - ae2f_AnnMlpLayerVPad(_this, )[i] = 0; + ae2f_mAnnMlpLayerVPad(_this, )[i] = 0; } - if(*ae2f_AnnMlpCache(_this)) free(*ae2f_AnnMlpCache(_this)); + if(*ae2f_mAnnMlpCache(_this)) + free(*ae2f_mAnnMlpCache(_this)); _this->inc = 0; _this->outc = 0; diff --git a/src/ann-h/Mlp/HidCompute.h b/src/ann-h/Mlp/HidCompute.h index 0f4e187..b197c8b 100644 --- a/src/ann-h/Mlp/HidCompute.h +++ b/src/ann-h/Mlp/HidCompute.h @@ -1,14 +1,14 @@ #include static ae2f_float_t MlpTrain_HidErr( - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerNxt, const ae2f_float_t* deltasNxt, size_t idxThen ); static void MlpTrain_HidCompute( - const ae2f_AnnSlp* layerThen, - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerThen, + const ae2f_mAnnSlp* layerNxt, ae2f_float_t* retDeltaThen, const ae2f_float_t* deltasNxt, const ae2f_float_t* outThen @@ -16,7 +16,7 @@ static void MlpTrain_HidCompute( for(size_t i = 0; i < layerThen->outc; i++) { const ae2f_float_t err = MlpTrain_HidErr(layerNxt, deltasNxt, i); - retDeltaThen[i] = ae2f_AnnSlpPerV(layerThen, i, const)->CalDelta( + retDeltaThen[i] = ae2f_mAnnSlpPerV(layerThen, i, const)->CalDelta( outThen[i], err + outThen[i] ); diff --git a/src/ann-h/Mlp/HidErr.h b/src/ann-h/Mlp/HidErr.h index 9f77b88..5f77048 100644 --- a/src/ann-h/Mlp/HidErr.h +++ b/src/ann-h/Mlp/HidErr.h @@ -7,7 +7,7 @@ /// /// Sum for multiplying Delta and Weight static ae2f_float_t MlpTrain_HidErr( - const ae2f_AnnSlp* layerNxt, + const ae2f_mAnnSlp* layerNxt, const ae2f_float_t* deltasNxt, size_t idxThen ) { @@ -15,9 +15,9 @@ static ae2f_float_t MlpTrain_HidErr( for(size_t i = 0; i < layerNxt->outc; i++) { union { const size_t* pad; - const ae2f_AnnSp* slp; - } perv = { .pad = ae2f_AnnSlpPerVPad(layerNxt)[i] + 1 }; - ret += ae2f_AnnSpW(perv.slp, const)[idxThen] * deltasNxt[i]; + const ae2f_mAnnSp* slp; + } perv = { .pad = ae2f_mAnnSlpPerVPad(layerNxt)[i] + 1 }; + ret += ae2f_mAnnSpW(perv.slp, const)[idxThen] * deltasNxt[i]; } return ret; diff --git a/src/ann-h/Mlp/OutCompute.h b/src/ann-h/Mlp/OutCompute.h index ee34449..6391cd0 100644 --- a/src/ann-h/Mlp/OutCompute.h +++ b/src/ann-h/Mlp/OutCompute.h @@ -1,13 +1,13 @@ #include static void MlpTrain_OutCompute( - const ae2f_AnnSlp* layerOut, + const ae2f_mAnnSlp* layerOut, const ae2f_float_t* goal, const ae2f_float_t* out, ae2f_float_t* retDeltaOut ) { for(size_t i = 0; i < layerOut->outc; i++) { - retDeltaOut[i] = ae2f_AnnSlpPerV(layerOut, i, const)->CalDelta( + retDeltaOut[i] = ae2f_mAnnSlpPerV(layerOut, i, const)->CalDelta( out[i], goal[i] ); } diff --git a/src/ann-h/Mlp/Predict.h b/src/ann-h/Mlp/Predict.h index 7ee2be1..be53003 100644 --- a/src/ann-h/Mlp/Predict.h +++ b/src/ann-h/Mlp/Predict.h @@ -1,14 +1,14 @@ #include #include static ae2f_err_t MlpTrain_Predict( - const ae2f_AnnMlp* _this, + const ae2f_mAnnMlp* _this, const ae2f_float_t* in, ae2f_float_t* outcache ) { if(!_this) return ae2f_errGlob_PTR_IS_NULL; if(!in) return ae2f_errGlob_PTR_IS_NULL; - size_t A = *ae2f_AnnMlpLayerBuffCount(_this, const); + size_t A = *ae2f_mAnnMlpLayerBuffCount(_this, const); union { const ae2f_float_t* CF; @@ -23,9 +23,9 @@ static ae2f_err_t MlpTrain_Predict( ae2f_err_t err = 0; for(size_t i = 0; i < _this->layerc; i++) { - const ae2f_AnnSlp* slp = ae2f_AnnMlpLayerV(_this, i, const); + const ae2f_mAnnSlp* slp = ae2f_mAnnMlpLayerV(_this, i, const); - err |= ae2f_AnnSlpPredict( + err |= ae2f_mAnnSlpPredict( slp, tmpVi.CF, tmpVo.F ); diff --git a/src/ann-h/Slp.h b/src/ann-h/Slp.h index 481fbd5..9f55aae 100644 --- a/src/ann-h/Slp.h +++ b/src/ann-h/Slp.h @@ -2,12 +2,12 @@ #include #include -static ae2f_AnnSlpPredict_t Predict; -static ae2f_AnnSlpTrain_t Train; -static ae2f_AnnSlpClean_t Clean; +static ae2f_mAnnSlpPredict_t Predict; +static ae2f_mAnnSlpTrain_t Train; +static ae2f_mAnnSlpClean_t Clean; static ae2f_err_t Predict( - const ae2f_AnnSlp* _this, + const ae2f_mAnnSlp* _this, const ae2f_float_t* in, ae2f_float_t* out_opt ) noexcept { @@ -20,14 +20,14 @@ static ae2f_err_t Predict( for(size_t i = 0; i < _this->outc; i++) { union { const size_t* pad; - const ae2f_AnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this, const)[i]}; + const ae2f_mAnnSp* perc; + } layer = {ae2f_mAnnSlpPerVPad(_this, const)[i]}; size_t _pad = *layer.pad; layer.pad++; ae2f_err_t er = - ae2f_AnnSpPredict( + ae2f_mAnnSpPredict( layer.perc, in + _pad, out_opt + i @@ -40,7 +40,7 @@ static ae2f_err_t Predict( } static ae2f_err_t Train( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -57,13 +57,13 @@ static ae2f_err_t Train( ae2f_float_t G = goal_optB ? goal_optB[i] : 0; union { size_t* pad; - ae2f_AnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; + ae2f_mAnnSp* perc; + } layer = {ae2f_mAnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; - _er |= ae2f_AnnSpTrain( + _er |= ae2f_mAnnSpTrain( layer.perc, in + _pad, delta_optA, G, @@ -77,13 +77,13 @@ static ae2f_err_t Train( ae2f_float_t G = goal_optB ? goal_optB[i] : 0; union { size_t* pad; - ae2f_AnnSp* perc; - } layer = {ae2f_AnnSlpPerVPad(_this)[i]}; + ae2f_mAnnSp* perc; + } layer = {ae2f_mAnnSlpPerVPad(_this)[i]}; size_t _pad = *layer.pad; layer.pad++; - _er |= ae2f_AnnSpTrain( + _er |= ae2f_mAnnSpTrain( layer.perc, in + _pad, delta_optA, G, @@ -95,19 +95,19 @@ static ae2f_err_t Train( } static ae2f_err_t Clean( - ae2f_AnnSlp* _this + ae2f_mAnnSlp* _this ) noexcept { if(!_this) return ae2f_errGlob_PTR_IS_NULL; for(size_t i = 0; i < _this->layerc; i++) { const union { - ae2f_AnnSp* v; + ae2f_mAnnSp* v; size_t* S; - } per = { .v = ae2f_AnnSlpPerV(_this, i)}; - ae2f_AnnSpClean(per.v); + } per = { .v = ae2f_mAnnSlpPerV(_this, i)}; + ae2f_mAnnSpClean(per.v); free(per.S - 1); - ae2f_AnnSlpPerVPad(_this, )[i] = 0; + ae2f_mAnnSlpPerVPad(_this, )[i] = 0; } _this->inc = 0; diff --git a/src/ann-h/Sp.h b/src/ann-h/Sp.h index 081371b..bd2d0b7 100644 --- a/src/ann-h/Sp.h +++ b/src/ann-h/Sp.h @@ -18,15 +18,15 @@ #include #include -#define ae2f_AnnSpPredictI(_this, in, i, ...) (ae2f_AnnSpW(_this, __VA_ARGS__ const)[i] * (in)[i]) -#define ae2f_AnnSpTrainI(_this, in, i, ...) (ae2f_AnnSpW(_this, __VA_ARGS__)[i] += _delta * (in)[i]) +#define ae2f_mAnnSpPredictI(_this, in, i, ...) (ae2f_mAnnSpW(_this, __VA_ARGS__ const)[i] * (in)[i]) +#define ae2f_mAnnSpTrainI(_this, in, i, ...) (ae2f_mAnnSpW(_this, __VA_ARGS__)[i] += _delta * (in)[i]) -static ae2f_AnnSpPredict_t Predict; -static ae2f_AnnSpTrain_t Train; +static ae2f_mAnnSpPredict_t Predict; +static ae2f_mAnnSpTrain_t Train; static ae2f_err_t Predict( - const ae2f_AnnSp* _this, + const ae2f_mAnnSp* _this, const ae2f_float_t* in, ae2f_float_t* out_opt ) { @@ -36,10 +36,10 @@ ae2f_err_t Predict( ae2f_float_t sum = 0; for(size_t i = 0; i < _this->inc; i++) { - sum += ae2f_AnnSpPredictI(_this, in, i); + sum += ae2f_mAnnSpPredictI(_this, in, i); } - sum += *ae2f_AnnSpB(_this); + sum += *ae2f_mAnnSpB(_this); if(_this->Act) sum = (_this)->Act(sum); @@ -50,7 +50,7 @@ ae2f_err_t Predict( static ae2f_err_t Train( - ae2f_AnnSp* _this, + ae2f_mAnnSp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, ae2f_float_t goal_optB, @@ -67,7 +67,7 @@ ae2f_err_t Train( if(delta_optA) _delta = *delta_optA; else { - err = ae2f_AnnSpPredict(_this, in, &_delta); + err = ae2f_mAnnSpPredict(_this, in, &_delta); if(err) goto __DONE; _delta = _this->CalDelta(_delta, goal_optB); } @@ -75,10 +75,10 @@ ae2f_err_t Train( _delta *= learningrate; for(size_t i = 0; i < _this->inc; i++) { - ae2f_AnnSpW(_this, )[i] += _delta * in[i]; + ae2f_mAnnSpW(_this, )[i] += _delta * in[i]; } - *ae2f_AnnSpB(_this) += _delta; + *ae2f_mAnnSpB(_this) += _delta; __DONE: return err; diff --git a/src/ann/Mlp.c b/src/ann/Mlp.c index 42241b7..73ad77f 100644 --- a/src/ann/Mlp.c +++ b/src/ann/Mlp.c @@ -1,8 +1,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnMlpInit( - ae2f_AnnMlp* _this, +size_t ae2f_mAnnMlpInit( + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -43,19 +43,19 @@ size_t ae2f_AnnMlpInit( union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; } perc = { - ae2f_AnnMlpLayerVPad(_this) + i + ae2f_mAnnMlpLayerVPad(_this) + i }; perc.u->pad = calloc( - ae2f_AnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), + ae2f_mAnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1 ); perc.u->pad++; - ae2f_AnnSlpInitB( + ae2f_mAnnSlpInitB( perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, @@ -69,16 +69,16 @@ size_t ae2f_AnnMlpInit( err = err | e & ~ae2f_errGlob_DONE_HOWEV; } - ae2f_AnnMlpCache(_this,)[0] = calloc( + ae2f_mAnnMlpCache(_this,)[0] = calloc( (max * layerc) << 2, sizeof(ae2f_float_t) ); - *ae2f_AnnMlpLayerBuffCount(_this) = max; + *ae2f_mAnnMlpLayerBuffCount(_this) = max; EXIT: #undef return if(errret_opt) *errret_opt = err; - return ae2f_AnnMlpInitSz(++layerc, add_opt); + return ae2f_mAnnMlpInitSz(++layerc, add_opt); } ae2f_SHAREDEXPORT @@ -93,9 +93,9 @@ ae2f_AnnMlp* ae2f_AnnMlpMk( const ae2f_float_t* weights_opt, ae2f_err_t* errret_opt ) noexcept { - ae2f_AnnMlp* obj = calloc(ae2f_AnnMlpInitSz(layerc, add_opt), 1); - ae2f_AnnMlpInit( - obj, layerc, add_opt, layerlenv, + ae2f_AnnMlp* obj = calloc(ae2f_mAnnMlpInitSz(layerc, add_opt), 1); + ae2f_mAnnMlpInit( + &obj->Mlp, layerc, add_opt, layerlenv, layerpadv_opt, inpadv_opt, actglob_opt, deltaglob_opt, weights_opt, errret_opt ); diff --git a/src/ann/Slp.c b/src/ann/Slp.c index 08620ef..5c0335f 100644 --- a/src/ann/Slp.c +++ b/src/ann/Slp.c @@ -1,8 +1,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2f_mAnnSlpInit( + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -35,22 +35,22 @@ size_t ae2f_AnnSlpInit( _inc = incs_optA ? incs_optA[i] : ginc_optB, _pad = inpads_opt ? inpads_opt[i] : 0; - if(!(ae2f_AnnSlpPerVPad(_this)[i] - = calloc(ae2f_AnnSpInitSz(sizeof(size_t), _inc), 1))) + if(!(ae2f_mAnnSlpPerVPad(_this)[i] + = calloc(ae2f_mAnnSpInitSz(sizeof(size_t), _inc), 1))) { er |= ae2f_errGlob_ALLOC_FAILED; continue; } - ae2f_AnnSpInit( - ae2f_AnnSlpPerV(_this, i), + ae2f_mAnnSpInit( + ae2f_mAnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, &ertmp, 0 ); er |= ertmp; - *(ae2f_AnnSlpPerVPad(_this)[i]) = _pad; + *(ae2f_mAnnSlpPerVPad(_this)[i]) = _pad; w_opt && (w_opt += _inc); @@ -65,7 +65,7 @@ size_t ae2f_AnnSlpInit( #undef return DONE: if(err) *err = er; - return ae2f_AnnSlpInitSz(outc, offset_opt); + return ae2f_mAnnSlpInitSz(outc, offset_opt); } ae2f_SHAREDEXPORT @@ -81,8 +81,8 @@ ae2f_AnnSlp* ae2f_AnnSlpMk( ae2f_err_t* err ) noexcept { ae2f_AnnSlp* _this = 0; - _this = calloc(ae2f_AnnSlpInitSz(outc, offset_opt), 1); - ae2f_AnnSlpInit(_this, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err); + _this = calloc(ae2f_mAnnSlpInitSz(outc, offset_opt), 1); + ae2f_mAnnSlpInit(&_this->Slp, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, offset_opt, err); if(err) *err &= ~ae2f_errGlob_DONE_HOWEV; return _this; } diff --git a/src/ann/Sp.c b/src/ann/Sp.c index 0987285..cc5f86d 100644 --- a/src/ann/Sp.c +++ b/src/ann/Sp.c @@ -14,8 +14,8 @@ #include ae2f_SHAREDEXPORT -size_t ae2f_AnnSpInit( - ae2f_AnnSp* _this, +size_t ae2f_mAnnSpInit( + ae2f_mAnnSp* _this, size_t inc, const ae2f_float_t* W_opt, ae2f_fpAnnAct_t Act, @@ -42,14 +42,14 @@ size_t ae2f_AnnSpInit( _this->vClean = 0; if(W_opt) { - memcpy(ae2f_AnnSpW(_this), W_opt, sizeof(ae2f_float_t) * inc); + memcpy(ae2f_mAnnSpW(_this), W_opt, sizeof(ae2f_float_t) * inc); } else { uint64_t _ = ae2f_AnnLcgRandSeed.u64; for(size_t i = 0; i < _this->inc; i++) { _ = ae2f_AnnLcgRand(_); ae2f_float_t d = ae2f_AnnLcgRandDistribute(_); - ae2f_AnnSpW(_this)[i] = d; + ae2f_mAnnSpW(_this)[i] = d; } ae2f_AnnLcgRandSeed.u64 = _; } @@ -57,12 +57,12 @@ size_t ae2f_AnnSpInit( { uint64_t _ = ae2f_AnnLcgRandG(); ae2f_AnnLcgRandSeed.u64 = _; - *ae2f_AnnSpB(_this) = ae2f_AnnLcgRandDistribute(_); + *ae2f_mAnnSpB(_this) = ae2f_AnnLcgRandDistribute(_); } DONE: if(erret) *erret = er; - return ae2f_AnnSpInitSz(offset_opt, inc); + return ae2f_mAnnSpInitSz(offset_opt, inc); } ae2f_SHAREDEXPORT @@ -76,8 +76,8 @@ ae2f_AnnSp* ae2f_AnnSpMk( ) { ae2f_AnnSp* _this = 0; - _this = calloc(ae2f_AnnSpInitSz(additional, inc), 1); - ae2f_AnnSpInit(_this, inc, W_opt, Act, CalDelta, erret, additional); + _this = calloc(ae2f_mAnnSpInitSz(additional, inc), 1); + ae2f_mAnnSpInit(&_this->Sp, inc, W_opt, Act, CalDelta, erret, additional); rtn: if(erret) *erret &= ~ae2f_errGlob_DONE_HOWEV; diff --git a/src/cl/Mlp.c b/src/cl/Mlp.c index 7d9b2db..90d44f5 100644 --- a/src/cl/Mlp.c +++ b/src/cl/Mlp.c @@ -4,7 +4,7 @@ ae2f_SHAREDEXPORT size_t ae2fCL_AnnMlpInit( - ae2f_AnnMlp* _this, + ae2f_mAnnMlp* _this, size_t layerc, size_t add_opt, const size_t* layerlenv, @@ -49,13 +49,13 @@ size_t ae2fCL_AnnMlpInit( union { size_t* pad; - ae2f_AnnSlp* slp; + ae2f_mAnnSlp* slp; }* u; - } perc = {ae2f_AnnMlpLayerVPad(_this) + i}; + } perc = {ae2f_mAnnMlpLayerVPad(_this) + i}; - perc.u->pad = calloc(ae2fCL_AnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1); + perc.u->pad = calloc(ae2fCL_mAnnSlpInitSz(LAYERSZ_R, sizeof(size_t)), 1); perc.u->pad++; - ae2fCL_AnnSlpInitB(perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, LAYERSZ_R, 0, &e, &er_cl); + ae2fCL_mAnnSlpInitB(perc.u->slp, LAYERSZ_L, 0, weights_opt, actglob_opt, deltaglob_opt, LAYERSZ_R, 0, &e, &er_cl); if(er_cl) err2 = er_cl; *(--perc.u->pad) = 0; @@ -65,11 +65,11 @@ size_t ae2fCL_AnnMlpInit( err = err | e & ~ae2f_errGlob_DONE_HOWEV; } - ae2f_AnnMlpCache(_this,)[0] = calloc( + ae2f_mAnnMlpCache(_this,)[0] = calloc( (max * layerc) << 2, sizeof(ae2f_float_t) ); - *ae2f_AnnMlpLayerBuffCount(_this) = max; + *ae2f_mAnnMlpLayerBuffCount(_this) = max; EXIT: #undef return @@ -77,7 +77,7 @@ size_t ae2fCL_AnnMlpInit( *errret_opt = err | (err2 ? ae2f_errGlob_NFOUND : 0); } if(errnfound_opt) *errnfound_opt= err2; - return ae2f_AnnMlpInitSz(++layerc, add_opt); + return ae2f_mAnnMlpInitSz(++layerc, add_opt); } ae2f_extern ae2f_SHAREDCALL @@ -95,10 +95,10 @@ ae2f_AnnMlp* ae2fCL_AnnMlpMk( ) noexcept { ae2f_err_t err = 0; cl_int err2 = 0; - ae2f_AnnMlp* obj = calloc(ae2f_AnnMlpInitSz(layerc, add_opt), 1); + ae2f_AnnMlp* obj = calloc(ae2f_mAnnMlpInitSz(layerc, add_opt), 1); ae2fCL_AnnMlpInit( - obj, layerc, add_opt, + &obj->Mlp, layerc, add_opt, layerlenv, layerpadv_opt, inpadv_opt, actglob_opt, deltaglob_opt, weights_opt, diff --git a/src/cl/Slp.c b/src/cl/Slp.c index d24b7e6..3767634 100644 --- a/src/cl/Slp.c +++ b/src/cl/Slp.c @@ -13,7 +13,7 @@ ae2f_err_t TrainCL( - ae2f_AnnSlp* _this, + ae2f_mAnnSlp* _this, const ae2f_float_t* in, const ae2f_float_t* delta_optA, const ae2f_float_t* goal_optB, @@ -39,7 +39,7 @@ ae2f_err_t TrainCL( cl_int err = 0, err2 = 0; - ae2fCL_AnnSlpMemX* memx = ae2fCL_AnnSlpAdd(_this); + ae2fCL_mAnnSlpMemX* memx = ae2fCL_mAnnSlpAdd(_this); const size_t WORKSZ[2] = { _this->inc, _this->outc }, @@ -48,10 +48,12 @@ ae2f_err_t TrainCL( #define IC WORKSZ[0] #define OC WORKSZ[1] - ae2f_float_t* PREDICTED_BUFF = ae2fCL_AnnSlpOutCache(_this); + ae2f_float_t* PREDICTED_BUFF = ae2fCL_mAnnSlpOutCache(_this); + #if 0 if(!(PREDICTED_BUFF = calloc(OC, sizeof(ae2f_float_t)))) return(ae2f_errGlob_ALLOC_FAILED); + #endif if(delta_optA) { @@ -87,12 +89,12 @@ ae2f_err_t TrainCL( { for(size_t i = 0; i < _this->outc; i++) { const size_t - * _padv = ae2f_AnnSlpPerVPad(_this, const)[i], + * _padv = ae2f_mAnnSlpPerVPad(_this, const)[i], pad = *_padv; - const ae2f_AnnSp* + const ae2f_mAnnSp* perc = ae2f_reinterpret_cast( - const ae2f_AnnSp*, + const ae2f_mAnnSp*, _padv + 1 ); @@ -102,7 +104,7 @@ ae2f_err_t TrainCL( ,CL_TRUE ,((IC * (1 + i)) + pad)* sizeof(ae2f_float_t) ,perc->inc * sizeof(ae2f_float_t) - ,ae2f_AnnSpW(perc, const) + ,ae2f_mAnnSpW(perc, const) , 0, NULL, 0 ); if(err2) { @@ -133,9 +135,9 @@ ae2f_err_t TrainCL( _BUFFSET: for(size_t i = 0; i < OC; i++) { - ae2f_AnnSp* perc = ae2f_AnnSlpPerV(_this, i); + ae2f_mAnnSp* perc = ae2f_mAnnSlpPerV(_this, i); - *ae2f_AnnSpB(perc) += PREDICTED_BUFF[i] = + *ae2f_mAnnSpB(perc) += PREDICTED_BUFF[i] = delta_optA ? PREDICTED_BUFF[i] * learningrate : perc->CalDelta(PREDICTED_BUFF[i], goal_optB[i]) * learningrate; @@ -190,10 +192,10 @@ ae2f_err_t TrainCL( #if 1 for(size_t i = 0; i < _this->outc; i++) { size_t - * padv = ae2f_AnnSlpPerVPad(_this)[i] + * padv = ae2f_mAnnSlpPerVPad(_this)[i] , pad = *padv; - ae2f_AnnSp* perc = ae2f_reinterpret_cast(ae2f_AnnSp*, padv + 1); + ae2f_mAnnSp* perc = ae2f_reinterpret_cast(ae2f_mAnnSp*, padv + 1); if((err2 = clEnqueueReadBuffer( ae2fCL_Ann.Q @@ -201,7 +203,7 @@ ae2f_err_t TrainCL( , CL_TRUE , (IC) * (1 + i) * sizeof(ae2f_float_t) , perc->inc * sizeof(ae2f_float_t) - , ae2f_AnnSpW(perc) + , ae2f_mAnnSpW(perc) , 0, NULL, NULL ))) { @@ -228,7 +230,7 @@ ae2f_err_t TrainCL( * OpenCL Code for Predicting SLP. * */ ae2f_err_t PredictCL( - const ae2f_AnnSlp* _ + const ae2f_mAnnSlp* _ , const ae2f_float_t* in , ae2f_float_t* out ) @@ -248,7 +250,7 @@ ae2f_err_t PredictCL( cl_int err = 0, err2 = 0; ae2f_err_t code = 0; - ae2fCL_AnnSlpMemX* __X = ae2fCL_AnnSlpAdd(_); + ae2fCL_mAnnSlpMemX* __X = ae2fCL_mAnnSlpAdd(_); const cl_kernel KERNEL = ae2fCL_AnnKerns[ae2fCL_eAnnKernsSlpPredict]; if(!__X->In) return ae2f_errGlob_PTR_IS_NULL | ae2f_errGlob_ALLOC_FAILED; @@ -287,12 +289,12 @@ ae2f_err_t PredictCL( { for(size_t i = 0; i < _->outc; i++) { const size_t - * _padv = ae2f_AnnSlpPerVPad(_, const)[i], + * _padv = ae2f_mAnnSlpPerVPad(_, const)[i], pad = *_padv; - const ae2f_AnnSp* + const ae2f_mAnnSp* perc = ae2f_reinterpret_cast( - const ae2f_AnnSp*, + const ae2f_mAnnSp*, _padv + 1 ); @@ -302,7 +304,7 @@ ae2f_err_t PredictCL( ,CL_TRUE ,((IC * (1 + i)) + pad)* sizeof(ae2f_float_t) ,perc->inc * sizeof(ae2f_float_t) - ,ae2f_AnnSpW(perc, const) + ,ae2f_mAnnSpW(perc, const) , 0, NULL, 0 ); if(err2) { @@ -368,13 +370,13 @@ ae2f_err_t PredictCL( for(size_t i = 0; i < OC; i++) { - const ae2f_AnnSp* - perc = ae2f_AnnSlpPerV(_,i); + const ae2f_mAnnSp* + perc = ae2f_mAnnSlpPerV(_,i); if(!perc) return(ae2f_errGlob_IMP_NOT_FOUND); if(!perc->Act) return(ae2f_errGlob_IMP_NOT_FOUND); - out[i] = perc->Act(out[i] + *ae2f_AnnSpB(perc)); + out[i] = perc->Act(out[i] + *ae2f_mAnnSpB(perc)); } _DONE: @@ -390,11 +392,11 @@ ae2f_err_t PredictCL( } -static ae2f_AnnSlpClean_t CleanCL; +static ae2f_mAnnSlpClean_t CleanCL; ae2f_SHAREDEXPORT -size_t ae2fCL_AnnSlpInit( - ae2f_AnnSlp* _this, +size_t ae2fCL_mAnnSlpInit( + ae2f_mAnnSlp* _this, const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -429,15 +431,15 @@ size_t ae2fCL_AnnSlpInit( _inc = incs_optA ? incs_optA[i] : ginc_optB, _pad = inpads_opt ? inpads_opt[i] : 0; - ae2f_AnnSlpPerVPad(_this)[i] + ae2f_mAnnSlpPerVPad(_this)[i] = calloc( - ae2f_AnnSpInitSz( + ae2f_mAnnSpInitSz( sizeof(size_t), _inc ), 1 ); - ae2f_AnnSpInit( - ae2f_AnnSlpPerV(_this, i), + ae2f_mAnnSpInit( + ae2f_mAnnSlpPerV(_this, i), _inc, w_opt, Act, CalDelta, &ertmp, 0 @@ -446,7 +448,7 @@ size_t ae2fCL_AnnSlpInit( if(v2 != CL_SUCCESS) V = v2; er |= ertmp; - *ae2f_AnnSlpPerVPad(_this)[i] = _pad; + *ae2f_mAnnSlpPerVPad(_this)[i] = _pad; w_opt && (w_opt += _inc); @@ -455,7 +457,7 @@ size_t ae2fCL_AnnSlpInit( } } - ae2fCL_AnnSlpAdd(_this)->In = + ae2fCL_mAnnSlpAdd(_this)->In = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE, @@ -463,7 +465,7 @@ size_t ae2fCL_AnnSlpInit( NULL, &v2 ); - ae2fCL_AnnSlpAdd(_this)->Changed = 1; + ae2fCL_mAnnSlpAdd(_this)->Changed = 1; if(CL_SUCCESS != v2) V = v2; @@ -471,14 +473,14 @@ size_t ae2fCL_AnnSlpInit( DONE: if(err_opt) *err_opt = er; if(errnfound_opt) *errnfound_opt = V; - return ae2fCL_AnnSlpInitSz(outc, offset_opt); + return ae2fCL_mAnnSlpInitSz(outc, offset_opt); } -static ae2f_err_t CleanCL(ae2f_AnnSlp* _) { +static ae2f_err_t CleanCL(ae2f_mAnnSlp* _) { ae2f_err_t e = 0; - if(_ && ae2fCL_AnnSlpAdd(_)->In) { + if(_ && ae2fCL_mAnnSlpAdd(_)->In) { if((ae2fCL_Ann.LErr = clReleaseMemObject( - ae2fCL_AnnSlpAdd(_)->In + ae2fCL_mAnnSlpAdd(_)->In ))) { e = @@ -491,7 +493,7 @@ static ae2f_err_t CleanCL(ae2f_AnnSlp* _) { } ae2f_SHAREDEXPORT -ae2f_AnnSlp* ae2fCL_AnnSlpMk( +ae2fCL_AnnSlp* ae2fCL_AnnSlpMk( const size_t* incs_optA, size_t ginc_optB, const size_t* inpads_opt, @@ -503,11 +505,11 @@ ae2f_AnnSlp* ae2fCL_AnnSlpMk( ae2f_err_t* err_opt, cl_int* err_nfound_opt ) noexcept { - ae2f_AnnSlp* rtn = calloc(ae2fCL_AnnSlpInitSz(outc, offset_opt), 1); + ae2fCL_AnnSlp* rtn = calloc(ae2fCL_mAnnSlpInitSz(outc, offset_opt), 1); ae2f_err_t err = 0; cl_int err2 = 0; - ae2fCL_AnnSlpInit(rtn, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, 0, &err, &err2); + ae2fCL_mAnnSlpInit(&rtn->CL_Slp, incs_optA, ginc_optB, inpads_opt, w_opt, Act, CalDelta, outc, 0, &err, &err2); if(err_opt) *err_opt = err; if(err_nfound_opt) *err_nfound_opt = err2; diff --git a/src/cl/Sp.c b/src/cl/Sp.c index 2e68c16..0e57f1a 100644 --- a/src/cl/Sp.c +++ b/src/cl/Sp.c @@ -3,12 +3,12 @@ #include "CLCode/Size/ae2f_float_t.auto.h" #include "CLCode/Size/cl_mem.auto.h" -static ae2f_AnnSpPredict_t Predict; -static ae2f_AnnSpTrain_t Train; -static ae2f_AnnSpClean_t Clean; +static ae2f_mAnnSpPredict_t Predict; +static ae2f_mAnnSpTrain_t Train; +static ae2f_mAnnSpClean_t Clean; static ae2f_err_t Predict( - const ae2f_AnnSp *_this, + const ae2f_mAnnSp *_this, const ae2f_float_t *in, ae2f_float_t *outret_opt ) { @@ -16,7 +16,7 @@ static ae2f_err_t Predict( if(!in) return ae2f_errGlob_PTR_IS_NULL; if(!outret_opt) return ae2f_errGlob_PTR_IS_NULL | ae2f_errGlob_DONE_HOWEV; - cl_mem _W = *ae2fCL_AnnSpWCl(_this, const), _IO = *ae2fCL_AnnSpIOCl(_this, const); + cl_mem _W = *ae2fCL_mAnnSpWCl(_this, const), _IO = *ae2fCL_mAnnSpIOCl(_this, const); cl_kernel K = ae2fCL_Ann.Kerns[ae2fCL_eAnnKernsSpPredict]; ae2f_float_t IBuffer; @@ -45,7 +45,7 @@ static ae2f_err_t Predict( ae2fCL_Ann.LErr = clEnqueueReadBuffer(ae2fCL_Ann.Q, _IO, CL_TRUE, _this->inc * ae2f_float_t_SIZE, ae2f_float_t_SIZE, &IBuffer, 0, 0, 0); if(ae2fCL_Ann.LErr != CL_SUCCESS) return ae2f_errGlob_NFOUND; - IBuffer = _this->Act(IBuffer + *ae2fCL_AnnSpB(_this, const)); + IBuffer = _this->Act(IBuffer + *ae2f_mAnnSpB(_this, const)); if(outret_opt) *outret_opt = IBuffer; return ae2f_errGlob_OK; @@ -54,7 +54,7 @@ static ae2f_err_t Predict( #include "./CLCode/uf.h" static ae2f_err_t Train( - ae2f_AnnSp *_this, + ae2f_mAnnSp *_this, const ae2f_float_t *in, const ae2f_float_t *delta_optA, ae2f_float_t goal_optB, @@ -75,9 +75,9 @@ static ae2f_err_t Train( if(er) return er; *uf.F *= learningrate; - *ae2f_AnnSpB(_this) += *uf.F; + *ae2f_mAnnSpB(_this) += *uf.F; - cl_mem _W = *ae2fCL_AnnSpWCl(_this), _IO = *ae2fCL_AnnSpIOCl(_this); + cl_mem _W = *ae2fCL_mAnnSpWCl(_this), _IO = *ae2fCL_mAnnSpIOCl(_this); cl_kernel K = ae2fCL_Ann.Kerns[ae2fCL_eAnnKernsSpTrain]; ae2fCL_Ann.LErr = clSetKernelArg(K, 0, sizeof(cl_mem), &_IO); if(ae2fCL_Ann.LErr != CL_SUCCESS) return ae2f_errGlob_NFOUND; @@ -106,7 +106,7 @@ static ae2f_err_t Train( ae2fCL_Ann.Q, _W, CL_TRUE, 0, _this->inc * sizeof(ae2f_float_t), - ae2f_AnnSpW(_this), + ae2f_mAnnSpW(_this), 0, 0, 0 )) != CL_SUCCESS) return ae2f_errGlob_NFOUND; @@ -114,8 +114,8 @@ static ae2f_err_t Train( } ae2f_SHAREDEXPORT -size_t ae2fCL_AnnSpInit( - ae2fCL_AnnSp* perc_opt, +size_t ae2fCL_mAnnSpInit( + ae2fCL_mAnnSp* perc_opt, size_t icount, const ae2f_float_t* w_opt, ae2f_AnnAct_t Act, @@ -128,19 +128,19 @@ size_t ae2fCL_AnnSpInit( ae2f_err_t err = 0; cl_int err2 = 0; - ae2f_AnnSpInit(perc_opt, icount, w_opt, Act, CalDelta, &err, off_opt); + ae2f_mAnnSpInit(perc_opt, icount, w_opt, Act, CalDelta, &err, off_opt); if(err) goto END; - *ae2fCL_AnnSpWCl(perc_opt) = clCreateBuffer( + *ae2fCL_mAnnSpWCl(perc_opt) = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, - ae2f_float_t_SIZE * icount, ae2f_AnnSpW(perc_opt), + ae2f_float_t_SIZE * icount, ae2f_mAnnSpW(perc_opt), &err2 ); if(err2 != CL_SUCCESS) return(ae2f_errGlob_NFOUND | ae2f_errGlob_ALLOC_FAILED); - *ae2fCL_AnnSpIOCl(perc_opt) = clCreateBuffer( + *ae2fCL_mAnnSpIOCl(perc_opt) = clCreateBuffer( ae2fCL_Ann.Ctx, CL_MEM_READ_WRITE, ae2f_float_t_SIZE * (icount + 1), 0, &err2 @@ -156,7 +156,7 @@ size_t ae2fCL_AnnSpInit( #undef return if(errret_opt) *errret_opt = err; if(erronnfound_opt) *erronnfound_opt = err2; - return ae2fCL_AnnSpInitSz(off_opt, icount); + return ae2fCL_mAnnSpInitSz(off_opt, icount); } ae2f_SHAREDEXPORT @@ -169,17 +169,17 @@ ae2fCL_AnnSp* ae2fCL_AnnSpMk( cl_int* erronnfound_opt, size_t off_opt ) noexcept { - ae2f_AnnSp* v; - v = calloc(ae2fCL_AnnSpInitSz(off_opt, icount), 1); - ae2fCL_AnnSpInit(v, icount, w_opt, Act, CalDelta, errret_opt, erronnfound_opt, off_opt); + ae2fCL_AnnSp* v; + v = calloc(ae2fCL_mAnnSpInitSz(off_opt, icount), 1); + ae2fCL_mAnnSpInit(&v->CL_Sp, icount, w_opt, Act, CalDelta, errret_opt, erronnfound_opt, off_opt); return v; } -static ae2f_err_t Clean(ae2f_AnnSp* a) { +static ae2f_err_t Clean(ae2f_mAnnSp* a) { if(!a) return ae2f_errGlob_PTR_IS_NULL; cl_int er[2]; - er[0] = clReleaseMemObject(*ae2fCL_AnnSpIOCl(a)); - er[1] = clReleaseMemObject(*ae2fCL_AnnSpWCl(a)); + er[0] = clReleaseMemObject(*ae2fCL_mAnnSpIOCl(a)); + er[1] = clReleaseMemObject(*ae2fCL_mAnnSpWCl(a)); if(er[0] || er[1]) return ae2f_errGlob_NFOUND; diff --git a/test/cl-src/MlpTrainXOR.c b/test/cl-src/MlpTrainXOR.c index 5be0b42..47e5422 100644 --- a/test/cl-src/MlpTrainXOR.c +++ b/test/cl-src/MlpTrainXOR.c @@ -71,32 +71,32 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins, goals + 2, gLearningRate ); if(err2) { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 2, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 4, goals, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); if(err2) { @@ -108,8 +108,8 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins, outbuff ); if(err2) { goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -118,8 +118,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -128,8 +128,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -138,8 +138,8 @@ int main() { err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); @@ -151,7 +151,7 @@ int main() { #endif __failure: - ae2f_AnnMlpDel(Mlp); + ae2f_mAnnSlpDel(&Mlp->Slp); ae2fCL_AnnDel(); printf("Something is over, code: %d\n", err_ae2f | err2); return err_ae2f | err2; diff --git a/test/cl-src/PercMk.cc b/test/cl-src/PercMk.cc index 66aee7b..1861ff1 100644 --- a/test/cl-src/PercMk.cc +++ b/test/cl-src/PercMk.cc @@ -34,24 +34,27 @@ int mainc() { Sigmoid, Sub, &err, errcl, 0 ); CHECK_ERR(err, CL_SUCCESS, __failure); - err = Perc->Predict(Buff, &outfloat); + + err = ae2f_mAnnSpPredict( + &Perc->Sp, Buff, &outfloat + ); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(&Perc->Sp), + (*ae2f_mAnnSpB(&Perc->Sp)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *Perc->B(); - out_checksum = Perc->Act(out_checksum); + out_checksum += *ae2f_mAnnSpB(&Perc->Sp); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); @@ -86,25 +89,25 @@ int maincc() { ); CHECK_ERR(err, CL_SUCCESS, __failure); - err = Perc->Predict( + err = Perc->Sp.Predict( Buff, &outfloat ); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *Perc->B(), - (*Perc->B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *Perc->Sp.B(), + (*Perc->Sp.B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *ae2f_AnnSpB(Perc); - out_checksum = Perc->Act(out_checksum); + out_checksum += *Perc->Sp.B(); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); @@ -113,10 +116,10 @@ int maincc() { __failure: + if(Perc) delete (Perc); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); - if(Perc) delete (Perc); return err; } diff --git a/test/cl-src/SlpLeak.c b/test/cl-src/SlpLeak.c new file mode 100644 index 0000000..6f23335 --- /dev/null +++ b/test/cl-src/SlpLeak.c @@ -0,0 +1,19 @@ +#include + +ae2f_float_t a(ae2f_float_t) { return 0; } +ae2f_float_t b(ae2f_float_t, ae2f_float_t) { return 0; } + +int main() { + ae2fCL_AnnSlp* slp; + + slp = ae2fCL_AnnSlpMkB( + 1, 0, 0, a, b, 1, 0, 0, 0 + ); + ae2f_float_t in, out; + + ae2f_mAnnSlpPredict(&slp->Slp, &in, &out); + ae2f_mAnnSlpTrainB(&slp->Slp, &in, &out, 0.1); + ae2fCL_AnnSlpDel(slp); + + return 0; +} \ No newline at end of file diff --git a/test/cl-src/SlpMk.cc b/test/cl-src/SlpMk.cc index 80b8390..fbae68c 100644 --- a/test/cl-src/SlpMk.cc +++ b/test/cl-src/SlpMk.cc @@ -23,8 +23,8 @@ int mainc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc = 0; - ae2f_AnnSlp* SLP; + ae2f_mAnnSp* Perc = 0; + ae2fCL_AnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -36,17 +36,17 @@ int mainc() { &err, errcl ); - Perc = ae2f_AnnSlpPerV(SLP, 0); + Perc = ae2f_mAnnSlpPerV(&SLP->Slp, 0); CHECK_ERR(err, CL_SUCCESS, __failure); - err = SLP->Predict(Buff, &outfloat); + err = ae2f_mAnnSlpPredict(&SLP->Slp, Buff, &outfloat); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -65,7 +65,7 @@ int mainc() { } __failure: - if(SLP) ae2f_AnnSlpDel(SLP); + if(SLP) ae2fCL_AnnSlpDel(SLP); ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); @@ -80,8 +80,8 @@ int maincc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc = 0; - ae2f_AnnSlp* SLP; + ae2f_mAnnSp* Perc = 0; + ae2fCL_AnnSlp* SLP; ae2f_float_t out_checksum = 0; err = ae2fCL_AnnMkEasy(errcl); @@ -93,17 +93,17 @@ int maincc() { &err, errcl ); - Perc = ae2f_AnnSlpPerV(SLP, 0); + Perc = SLP->Slp.Perc(0); CHECK_ERR(err, CL_SUCCESS, __failure); - err = SLP->Predict(Buff, &outfloat); + err = SLP->Slp.Predict(Buff, &outfloat); if(err) goto __failure; printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -122,7 +122,7 @@ int maincc() { } __failure: - if(SLP) ae2f_AnnSlpDel(SLP); + if(SLP) delete SLP; ae2fCL_AnnDel(); if(ae2fCL_Ann.Q) clReleaseCommandQueue(ae2fCL_Ann.Q); if(ae2fCL_Ann.Ctx) clReleaseContext(ae2fCL_Ann.Ctx); diff --git a/test/cl-src/SlpTrainAND.c b/test/cl-src/SlpTrainAND.c index fc82529..fbcbc4e 100644 --- a/test/cl-src/SlpTrainAND.c +++ b/test/cl-src/SlpTrainAND.c @@ -4,7 +4,7 @@ #include #define gLearningRate 0.1 -#define gEpochs 9000 +#define gEpochs 5000 static ae2f_float_t Forward(ae2f_float_t x) { @@ -42,16 +42,16 @@ int main() { err = ae2fCL_AnnMkEasy(&errcl); CHECK_ERR(err, CL_SUCCESS, __failure); - ae2f_AnnSlp* Slp = ae2fCL_AnnSlpMkB( + ae2fCL_AnnSlp* Slp = ae2fCL_AnnSlpMkB( 2, 0, 0, Forward, Backward, 1, 0, &err2, &errcl ); ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSlpPredict(obj, inb + in_idx, out) + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + ae2f_mAnnSlpPredict(&obj->Slp, inb + in_idx, out) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -59,7 +59,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (1, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -67,7 +67,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (0, 0)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -75,7 +75,7 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f (0, 1)\n", outbuff[0]); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -86,12 +86,12 @@ int main() { if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( - Slp, ins, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins, goals + 0, gLearningRate ); if(err2) { @@ -101,24 +101,24 @@ int main() { goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 2/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 2/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 4/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 4/*in_idx*/, goals + 2, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 6/*in_idx*/, + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 6/*in_idx*/, goals + 2, gLearningRate ); if(err2) { @@ -128,7 +128,7 @@ int main() { puts("we are going for predict"); - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -140,7 +140,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -148,14 +148,14 @@ int main() { err = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { - err2 = ae2f_AnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); + err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(&Slp->Slp, 0), ins + 6, outbuff); printf("Checking the value (SP): %f\n", outbuff[0]); printf("AND 0, 0 no good\n"); err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -167,7 +167,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context diff --git a/test/cl-src/SpTrainAND.c b/test/cl-src/SpTrainAND.c index f7b3d8b..ebe3559 100644 --- a/test/cl-src/SpTrainAND.c +++ b/test/cl-src/SpTrainAND.c @@ -48,27 +48,27 @@ int main() { ae2fCL_AnnMkEasy(0); ae2f_err_t err2, err; - ae2f_AnnSp* Slp = ae2fCL_AnnSpMk( + ae2fCL_AnnSp* Perc = ae2fCL_AnnSpMk( 2, 0, Forward, Backward, &err2, 0, 0 ); if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSpTrainB( - Slp, ins, + err2 = ae2f_mAnnSpTrainB( + &Perc->Sp, ins, *goals + 0, gLearningRate ); if(err2) { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + err2 = ae2fCL_mAnnSlpTrain( + &Perc->Sp, ins, 0, 2/*in_idx*/, 0, goals + 2, gLearningRate, 0, (void*)diff_got, 0, @@ -78,8 +78,8 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + err2 = ae2fCL_mAnnSlpTrain( + &Perc->Sp, ins, 0, 4/*in_idx*/, 0, goals + 2, gLearningRate, 0, (void*)diff_got, 0, @@ -89,8 +89,8 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( - Slp, ins, + err2 = ae2fCL_mAnnSlpTrain( + &Perc->Sp, ins, 0, 6/*in_idx*/, 0, goals + 2, gLearningRate, 0, diff_got, 0, @@ -102,11 +102,11 @@ int main() { } ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSpPredict(obj, inb + in_idx, (out)) + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + err2 = ae2fCL_mAnnSlpPredict( + &Perc->Sp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -117,8 +117,8 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + err2 = ae2fCL_mAnnSlpPredict( + &Perc->Sp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -129,8 +129,8 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + err2 = ae2fCL_mAnnSlpPredict( + &Perc->Sp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -141,8 +141,8 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, + err2 = ae2fCL_mAnnSlpPredict( + &Perc->Sp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context ); if(err2) { @@ -154,7 +154,7 @@ int main() { } __failure: - ae2f_AnnSpDel(Slp); + ae2fCL_AnnSpDel(Perc); ae2fCL_AnnDel(); return err | err2; } \ No newline at end of file diff --git a/test/src/MlpTrainAND.c b/test/src/MlpTrainAND.c index a18f3e5..fbe3241 100644 --- a/test/src/MlpTrainAND.c +++ b/test/src/MlpTrainAND.c @@ -61,32 +61,32 @@ int main() { goto __failure; } for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins, goals + 0, gLearningRate ); if(err2) { err_ae2f = err2; goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 2, goals + 2, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 4, goals + 2, gLearningRate ); if(err2) { goto __failure; } - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 6, goals + 2, gLearningRate ); if(err2) { @@ -98,8 +98,8 @@ int main() { #if 1 puts("Predict time"); - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Mlp, ins, outbuff ); if(err2) { goto __failure; } @@ -108,13 +108,13 @@ int main() { puts("AND 1, 1 no good"); ae2f_float_t tmp[3] = {}; - err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + err2 = ae2f_mAnnSlpPredict( + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, tmp ); - err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 1), + err2 = ae2f_mAnnSlpPredict( + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 1), tmp, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -122,16 +122,16 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff ); if(err2) { err2 = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 0, 0 no good\n"); - err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + err2 = ae2f_mAnnSlpPredict( + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -139,30 +139,30 @@ int main() { err2 = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 0, 1 no good\n"); - err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + err2 = ae2f_mAnnSlpPredict( + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff + err2 = ae2f_mAnnSlpPredict( + &Mlp->Mlp, ins + 2, outbuff ); if(err2) { err_ae2f = err2; goto __failure; } printf("Checking the value: %f\n", outbuff[0]); if(outbuff[0] > 0.5) { printf("AND 1, 0 no good\n"); - err2 = ae2f_AnnSlpPredict( - ae2f_AnnMlpLayerV(Mlp, 0), + err2 = ae2f_mAnnSlpPredict( + ae2f_mAnnMlpLayerV(&Mlp->Mlp, 0), ins, outbuff ); printf("Checking the value(SLP): %f\n", outbuff[0]); @@ -172,7 +172,7 @@ int main() { #endif __failure: - ae2f_AnnMlpDel(Mlp); + ae2f_mAnnSlpDel(&Mlp->Slp); printf("Something is over, code: %d\n", err_ae2f); return err_ae2f | err2; } \ No newline at end of file diff --git a/test/src/MlpTrainXOR.c b/test/src/MlpTrainXOR.c deleted file mode 100644 index 0488ebe..0000000 --- a/test/src/MlpTrainXOR.c +++ /dev/null @@ -1,148 +0,0 @@ -#include "../test.h" -#include -#include - -#include - -#define gLearningRate 0.1 -#define gEpochs 5000 - -static ae2f_float_t -Step(ae2f_float_t x) { - return x >= 0; -} - - -static ae2f_float_t -Calculate(ae2f_float_t out, ae2f_float_t goal) { - return goal - out; -} - -static ae2f_float_t -Forward(ae2f_float_t x) { - return 1.0 / (1.0 + exp(-x)); -} - -static ae2f_float_t -ForwardPrime(ae2f_float_t output) { - return output * (1.0 - output); -} - -static ae2f_float_t -Backward(ae2f_float_t output, ae2f_float_t target) { - return (target - output) * ForwardPrime(output); -} - -int main() { - ae2f_err_t err_ae2f = 0; - ae2f_err_t err2 = 0; - ae2f_float_t diff_got[2] = {0, }; - ae2f_AnnMlp* Mlp = 0; - size_t sizes[] = {2, 3, 1}; - ae2f_float_t outbuff[2] = { 0, 0 }; - - // [1, 1], [1, 0], [0, 1], [0, 0] - ae2f_float_t ins[] = { - 1, 1, 1, 0, 0, 1, 0, 0 - }; - - // 1, 1, 0, 0 - const ae2f_float_t goals[] = { - 1, 1, 0, 0 - }; - - Mlp = ae2f_AnnMlpMk( - sizeof(sizes) / sizeof(size_t), 0, sizes, - 0, 0, Forward, Backward, 0, - &err_ae2f - ); - - #if 1 - if(err_ae2f) { - goto __failure; - } - for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnMlpTrainB( - Mlp, ins, - goals + 2, gLearningRate - ); - if(err2) { - err_ae2f = err2; goto __failure; - } - - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 2, - goals, gLearningRate - ); - if(err2) { - goto __failure; - } - - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 4, - goals, gLearningRate - ); - if(err2) { - goto __failure; - } - - err2 = ae2f_AnnMlpTrainB( - Mlp, ins + 6, - goals + 2, gLearningRate - ); - if(err2) { - goto __failure; - } - } - #endif - - #if 1 - puts("Predict time"); - - err2 = ae2f_AnnMlpPredict( - Mlp, ins, outbuff - ); if(err2) { - goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] > 0.5) { - printf("AND 1, 1 no good\n"); - err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 6, outbuff - ); if(err2) { - err2 = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] > 0.5) { - printf("AND 0, 0 no good\n"); - err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 4, outbuff - ); if(err2) { - err_ae2f = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] < 0.5) { - printf("AND 0, 1 no good\n"); - err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2f_AnnMlpPredict( - Mlp, ins + 2, outbuff - ); if(err2) { - err_ae2f = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] < 0.5) { - printf("AND 1, 0 no good\n"); - err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; - } - - #endif - - __failure: - ae2f_AnnMlpDel(Mlp); - printf("Something is over, code: %d\n", err_ae2f | err2); - return err_ae2f | err2; -} \ No newline at end of file diff --git a/test/src/MlpTrainXOR.cc b/test/src/MlpTrainXOR.cc new file mode 100644 index 0000000..b5120eb --- /dev/null +++ b/test/src/MlpTrainXOR.cc @@ -0,0 +1,264 @@ +#include "../test.h" +#include +#include + +#include + +#define gLearningRate 0.1 +#define gEpochs 25000 + +static ae2f_float_t +Step(ae2f_float_t x) { + return x >= 0; +} + + +static ae2f_float_t +Calculate(ae2f_float_t out, ae2f_float_t goal) { + return goal - out; +} + +static ae2f_float_t +Forward(ae2f_float_t x) { + return 1.0 / (1.0 + exp(-x)); +} + +static ae2f_float_t +ForwardPrime(ae2f_float_t output) { + return output * (1.0 - output); +} + +static ae2f_float_t +Backward(ae2f_float_t output, ae2f_float_t target) { + return (target - output) * ForwardPrime(output); +} + +int mainc() { + ae2f_err_t err_ae2f = 0; + ae2f_err_t err2 = 0; + ae2f_float_t diff_got[2] = {0, }; + + ae2f_AnnMlp* Mlp = 0; + size_t sizes[] = {2, 3, 1}; + ae2f_float_t outbuff[2] = { 0, 0 }; + + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + // 1, 1, 0, 0 + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + Mlp = ae2f_AnnMlpMk( + sizeof(sizes) / sizeof(size_t), 0, sizes, + 0, 0, Forward, Backward, 0, + &err_ae2f + ); + + #if 1 + if(err_ae2f) { + goto __failure; + } + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, + ins, goals + 2, gLearningRate + ); + if(err2) { + err_ae2f = err2; goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 2, + goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 4, + goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Mlp->Slp, ins + 6, + goals + 2, gLearningRate + ); + if(err2) { + goto __failure; + } + } + #endif + + #if 1 + puts("Predict time"); + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins, outbuff + ); if(err2) { + goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff + ); if(err2) { + err2 = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 0, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + #endif + + __failure: + ae2f_mAnnSlpDel(&Mlp->Slp); + printf("Something is over, code: %d\n", err_ae2f | err2); + return err_ae2f | err2; +} + +int maincc() { + ae2f_err_t err_ae2f = 0; + ae2f_err_t err2 = 0; + ae2f_float_t diff_got[2] = {0, }; + + ae2f_AnnMlp* Mlp = 0; + size_t sizes[] = {2, 3, 1}; + ae2f_float_t outbuff[2] = { 0, 0 }; + + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + // 1, 1, 0, 0 + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + Mlp = ae2f_AnnMlpMk( + sizeof(sizes) / sizeof(size_t), 0, sizes, + 0, 0, Forward, Backward, 0, + &err_ae2f + ); + + #if 1 + if(err_ae2f) { + goto __failure; + } + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = Mlp->Slp.TrainB( + ins, goals + 2, gLearningRate + ); + if(err2) { + err_ae2f = err2; goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 2, goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 4, goals, gLearningRate + ); + if(err2) { + goto __failure; + } + + err2 = Mlp->Slp.TrainB( + ins + 6, goals + 2, gLearningRate + ); + if(err2) { + goto __failure; + } + } + #endif + + #if 1 + puts("Predict time"); + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins, outbuff + ); if(err2) { + goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 6, outbuff + ); if(err2) { + err2 = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 4, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 0, 1 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2f_mAnnSlpPredict( + &Mlp->Slp, ins + 2, outbuff + ); if(err2) { + err_ae2f = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 0 no good\n"); + err_ae2f = ae2f_errGlob_IMP_NOT_FOUND; + } + + #endif + + __failure: + delete Mlp; + printf("Something is over, code: %d\n", err_ae2f | err2); + return err_ae2f | err2; +} + +int main() { + return mainc() + maincc(); +} \ No newline at end of file diff --git a/test/src/PercMk.cc b/test/src/PercMk.cc index 3bc1f8c..43b2a2a 100644 --- a/test/src/PercMk.cc +++ b/test/src/PercMk.cc @@ -21,16 +21,16 @@ ae2f_extern int mainc() { 0.3, 0.2, 0.4, 0.6, 0.1 }; - ae2f_AnnSp* Perc; + ae2f_mAnnSp* Perc; ae2f_float_t out_checksum = 0; - Perc = ae2f_AnnSpMk( + Perc = (ae2f_mAnnSp*)ae2f_AnnSpMk( sizeof(Buff)/sizeof(ae2f_float_t), Buff, Sigmoid, Sub, &err, 0 ); CHECK_ERR(err, 0, __failure); - err = ae2f_AnnSpPredict( + err = ae2f_mAnnSpPredict( Perc, Buff, &outfloat ); @@ -38,8 +38,8 @@ ae2f_extern int mainc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *ae2f_AnnSpB(Perc), - (*ae2f_AnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *ae2f_mAnnSpB(Perc), + (*ae2f_mAnnSpB(Perc)) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); for(size_t i = 0; i < Perc->inc; i++) @@ -48,7 +48,7 @@ ae2f_extern int mainc() { out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *ae2f_AnnSpB(Perc); + out_checksum += *ae2f_mAnnSpB(Perc); out_checksum = Perc->Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { @@ -57,7 +57,7 @@ ae2f_extern int mainc() { } __failure: - ae2f_AnnSpDel(Perc); + ae2f_mAnnSpDel(Perc); return err; } @@ -77,7 +77,7 @@ int maincc() { ); CHECK_ERR(err, 0, __failure); - err = Perc->Predict( + err = Perc->Sp.Predict( Buff, &outfloat ); @@ -85,18 +85,18 @@ int maincc() { printf("out: %f\n", outfloat); printf( "Bias global: %f, with bias: %f\n", - *Perc->B(), - (*Perc->B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat + *Perc->Sp.B(), + (*Perc->Sp.B()) * sizeof(Buff)/ sizeof(ae2f_float_t) + outfloat ); - for(size_t i = 0; i < Perc->inc; i++) + for(size_t i = 0; i < Perc->Sp.inc; i++) { ae2f_float_t got = Buff[i] * Buff[i]; out_checksum += got; printf("Check-got: %f\n", got); } - out_checksum += *Perc->B(); - out_checksum = Perc->Act(out_checksum); + out_checksum += *Perc->Sp.B(); + out_checksum = Perc->Sp.Act(out_checksum); printf("Checking two values match...: %f %f\n", out_checksum, outfloat); if((out_checksum - outfloat) * (out_checksum - outfloat) > gThreshold) { printf("Check failed\n"); diff --git a/test/src/SlpTrainAND.c b/test/src/SlpTrainAND.c deleted file mode 100644 index 3a14fc1..0000000 --- a/test/src/SlpTrainAND.c +++ /dev/null @@ -1,178 +0,0 @@ -#include "../test.h" -#include -#include - -#define gLearningRate 0.1 -#define gEpochs 1000 - -static ae2f_float_t -Forward(ae2f_float_t x) { - return 1.0 / (1.0 + exp(-x)); -} - -static ae2f_float_t -ForwardPrime(ae2f_float_t output) { - return output * (1.0 - output); -} - -static ae2f_AnnDelta_t Backward; - -static ae2f_float_t -Backward(ae2f_float_t o, ae2f_float_t T) { - - return (T - o) * ForwardPrime(o); -} - -int main() { - // [1, 1], [1, 0], [0, 1], [0, 0] - ae2f_float_t ins[] = { - 1, 1, 1, 0, 0, 1, 0, 0 - }; - - - const ae2f_float_t goals[] = { - 1, 1, 0, 0 - }; - - ae2f_float_t diff_got[2]; - - ae2f_err_t err2, err; - ae2f_AnnSlp* Slp = ae2f_AnnSlpMkB( - 2, 0, 0, Forward, Backward, 1, - 0, &err2 - ); - ae2f_float_t outbuff[2] = { 5 }; - - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSlpPredict(obj, inb + in_idx, out) - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 0/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f (1, 1)\n", outbuff[0]); - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 6/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f (0, 0)\n", outbuff[0]); - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 4/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f (0, 1)\n", outbuff[0]); - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 2/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f (1, 0)\n", outbuff[0]); - - if(err2) { - err = err2; goto __failure; - } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSlpTrainB(obj, ins + in_idx, goal, learnrate) - - if(err) goto __failure; - for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSlpTrainB( - Slp, ins, - goals + 0, gLearningRate - ); - if(err2) { - err = err2; goto __failure; - } - - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 2/*in_idx*/, - goals + 2, gLearningRate - ); - if(err2) { - err = err2; goto __failure; - } - - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 4/*in_idx*/, - goals + 2, gLearningRate - ); - if(err2) { - err = err2; goto __failure; - } - - err2 = ae2f_AnnSlpTrainB( - Slp, ins + 6/*in_idx*/, - goals + 2, gLearningRate - ); - if(err2) { - err = err2; goto __failure; - } - } - - puts("we are going for predict"); - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 0/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] < 0.5) { - printf("AND 1, 1 no good\n"); - err = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 6/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] > 0.5) { - err2 = ae2f_AnnSpPredict(ae2f_AnnSlpPerV(Slp, 0), ins + 6, outbuff); - printf("Checking the value (SP): %f\n", outbuff[0]); - - printf("AND 0, 0 no good\n"); - err = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 4/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] > 0.5) { - printf("AND 0, 1 no good\n"); - err = ae2f_errGlob_IMP_NOT_FOUND; - } - - err2 = ae2fCL_AnnSlpPredict( - Slp, ins, 0, - 2/*in_idx*/, 0, outbuff, - queue, CL_TRUE, 0, 0, 0, context - ); if(err2) { - err = err2; goto __failure; - } printf("Checking the value: %f\n", outbuff[0]); - if(outbuff[0] > 0.5) { - printf("AND 1, 0 no good\n"); - err = ae2f_errGlob_IMP_NOT_FOUND; - } - - __failure: - ae2f_AnnSlpDel(Slp); - printf("ERR: %d\n", err); - return err; -} \ No newline at end of file diff --git a/test/src/SlpTrainAND.cc b/test/src/SlpTrainAND.cc new file mode 100644 index 0000000..c9c6db2 --- /dev/null +++ b/test/src/SlpTrainAND.cc @@ -0,0 +1,341 @@ +#include "../test.h" +#include +#include + +#define gLearningRate 0.1 +#define gEpochs 1000 + +static ae2f_float_t +Forward(ae2f_float_t x) { + return 1.0 / (1.0 + exp(-x)); +} + +static ae2f_float_t +ForwardPrime(ae2f_float_t output) { + return output * (1.0 - output); +} + +static ae2f_AnnDelta_t Backward; + +static ae2f_float_t +Backward(ae2f_float_t o, ae2f_float_t T) { + return (T - o) * ForwardPrime(o); +} +int mainc(), maincc(); +int main() { + return mainc() + maincc(); +} + +int mainc() { + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + ae2f_float_t diff_got[2]; + + ae2f_err_t err2, err; + ae2f_AnnSlp* Slp = ae2f_AnnSlpMkB( + 2, 0, 0, Forward, Backward, 1, + 0, &err2 + ); + ae2f_float_t outbuff[2] = { 5 }; + + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + ae2f_mAnnSlpPredict(&obj->Slp, inb + in_idx, out) + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 1)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 0)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 1)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 0)\n", outbuff[0]); + + if(err2) { + err = err2; goto __failure; + } + + Slp->Slp.vPredict; + + if(err) goto __failure; + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins, + goals + 0, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 2/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 4/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = ae2f_mAnnSlpTrainB( + &Slp->Slp, ins + 6/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + } + + puts("we are going for predict"); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + err2 = ae2f_mAnnSpPredict(ae2f_mAnnSlpPerV(&Slp->Slp, 0), ins + 6, outbuff); + printf("Checking the value (SP): %f\n", outbuff[0]); + + printf("AND 0, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + __failure: + ae2f_AnnSlpDel(Slp); + printf("ERR: %d\n", err); + return err; +} + + + +int maincc() { + // [1, 1], [1, 0], [0, 1], [0, 0] + ae2f_float_t ins[] = { + 1, 1, 1, 0, 0, 1, 0, 0 + }; + + + const ae2f_float_t goals[] = { + 1, 1, 0, 0 + }; + + ae2f_float_t diff_got[2]; + + ae2f_err_t err2, err; + ae2f_AnnSlp* Slp = ae2f_AnnSlpMkB( + 2, 0, 0, Forward, Backward, 1, + 0, &err2 + ); + ae2f_float_t outbuff[2] = { 5 }; + + #undef ae2fCL_mAnnSlpPredict + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + (obj)->Slp.Predict(inb + in_idx, out) // ae2f_mAnnSlpPredict(obj, inb + in_idx, out) + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 1)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 0)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (0, 1)\n", outbuff[0]); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f (1, 0)\n", outbuff[0]); + + if(err2) { + err = err2; goto __failure; + } + + #undef ae2fCL_mAnnSlpTrain + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) \ + (obs)->Slp.TrainB(ins + in_idx, goal, learnrate) // ae2f_mAnnSlpTrainB(obj, ins + in_idx, goal, learnrate) + + if(err) goto __failure; + for(size_t _ = 0; _ < gEpochs; _++) { + err2 = Slp->Slp.TrainB( + ins, goals + 0, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = Slp->Slp.TrainB( + ins + 2/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = Slp->Slp.TrainB( + ins + 4/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + + err2 = Slp->Slp.TrainB( + ins + 6/*in_idx*/, + goals + 2, gLearningRate + ); + if(err2) { + err = err2; goto __failure; + } + } + + puts("we are going for predict"); + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 0/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] < 0.5) { + printf("AND 1, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 6/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + err2 = Slp->Slp.Perc(0)->Predict(ins + 6, outbuff); + printf("Checking the value (SP): %f\n", outbuff[0]); + + printf("AND 0, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 4/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 0, 1 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + err2 = ae2fCL_mAnnSlpPredict( + Slp, ins, 0, + 2/*in_idx*/, 0, outbuff, + queue, CL_TRUE, 0, 0, 0, context + ); if(err2) { + err = err2; goto __failure; + } printf("Checking the value: %f\n", outbuff[0]); + if(outbuff[0] > 0.5) { + printf("AND 1, 0 no good\n"); + err = ae2f_errGlob_IMP_NOT_FOUND; + } + + __failure: + ae2f_AnnSlpDel(Slp); + printf("ERR: %d\n", err); + return err; +} diff --git a/test/src/SpTrainAND.c b/test/src/SpTrainAND.c index f7d5c52..bf99e25 100644 --- a/test/src/SpTrainAND.c +++ b/test/src/SpTrainAND.c @@ -45,18 +45,18 @@ int main() { ae2f_float_t diff_got[2]; ae2f_err_t err2, err; - ae2f_AnnSp* Slp = ae2f_AnnSpMk( + ae2f_mAnnSp* Slp = (ae2f_mAnnSp*)ae2f_AnnSpMk( 2, 0, Forward, Backward, &err2, 0 ); if(err2) { err = err2; goto __failure; } - #define ae2fCL_AnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_AnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) + #define ae2fCL_mAnnSlpTrain(obj, ins, _, in_idx, __, goal, learnrate, ...) ae2f_mAnnSpTrainB(obj, ins + in_idx, *(goal), learnrate) if(err) goto __failure; for(size_t _ = 0; _ < gEpochs; _++) { - err2 = ae2f_AnnSpTrainB( + err2 = ae2f_mAnnSpTrainB( Slp, ins, *goals + 0, gLearningRate ); @@ -64,7 +64,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 2/*in_idx*/, 0, goals + 2, gLearningRate, @@ -75,7 +75,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 4/*in_idx*/, 0, goals + 2, gLearningRate, @@ -86,7 +86,7 @@ int main() { err = err2; goto __failure; } - err2 = ae2fCL_AnnSlpTrain( + err2 = ae2fCL_mAnnSlpTrain( Slp, ins, 0, 6/*in_idx*/, 0, goals + 2, gLearningRate, @@ -99,10 +99,10 @@ int main() { } ae2f_float_t outbuff[2] = { 5 }; - #define ae2fCL_AnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ - ae2f_AnnSpPredict(obj, inb + in_idx, (out)) + #define ae2fCL_mAnnSlpPredict(obj, inb, _, in_idx, __, out, ...) \ + ae2f_mAnnSpPredict(obj, inb + in_idx, (out)) - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 0/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -114,7 +114,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 6/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -126,7 +126,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 4/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -138,7 +138,7 @@ int main() { err = ae2f_errGlob_IMP_NOT_FOUND; } - err2 = ae2fCL_AnnSlpPredict( + err2 = ae2fCL_mAnnSlpPredict( Slp, ins, 0, 2/*in_idx*/, 0, outbuff, queue, CL_TRUE, 0, 0, 0, context @@ -151,6 +151,6 @@ int main() { } __failure: - ae2f_AnnSpDel(Slp); + ae2f_mAnnSpDel(Slp); return err | err2; } \ No newline at end of file