diff options
Diffstat (limited to 'source/tb_ml')
| -rw-r--r-- | source/tb_ml/tb_mh.h | 43 | ||||
| -rw-r--r-- | source/tb_ml/tb_ml.c | 58 |
2 files changed, 93 insertions, 8 deletions
diff --git a/source/tb_ml/tb_mh.h b/source/tb_ml/tb_mh.h new file mode 100644 index 0000000..91e1e7b --- /dev/null +++ b/source/tb_ml/tb_mh.h | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #ifndef TB_ML_H | ||
| 2 | #define TB_ML_H | ||
| 3 | |||
| 4 | |||
| 5 | //- supervised learning model by abdellah el morabit | ||
| 6 | //- from <math.h> | ||
| 7 | #define M_E 2.7182818284590452354 /* e */ | ||
| 8 | |||
| 9 | typedef struct training_sample trainig_sample; | ||
| 10 | struct training_sample | ||
| 11 | { | ||
| 12 | |||
| 13 | }; | ||
| 14 | |||
| 15 | typedef struct prediction_data predicition_data; | ||
| 16 | struct prediction_data | ||
| 17 | { | ||
| 18 | f32 w; | ||
| 19 | f32 y; | ||
| 20 | }; | ||
| 21 | |||
| 22 | |||
| 23 | typedef struct prediction_category prediction_category; | ||
| 24 | struct category | ||
| 25 | { | ||
| 26 | string8 name; | ||
| 27 | predicition_data data; | ||
| 28 | }; | ||
| 29 | |||
| 30 | f(f32 d) { | ||
| 31 | return d * d; | ||
| 32 | } | ||
| 33 | |||
| 34 | internal f32 | ||
| 35 | derivative(f32 x, f32 (*f)(f32)) { | ||
| 36 | |||
| 37 | f32 h = 1e-6; // small interval to differnetiate the scope | ||
| 38 | return (f(x + h) - f(x - h)) / (2.0 * h); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | #endif | ||
diff --git a/source/tb_ml/tb_ml.c b/source/tb_ml/tb_ml.c index cf9625a..8211a60 100644 --- a/source/tb_ml/tb_ml.c +++ b/source/tb_ml/tb_ml.c | |||
| @@ -1,20 +1,62 @@ | |||
| 1 | #define BASE_UNITY | 1 | #define BASE_IMPLEMENTATION |
| 2 | #include "../base/base_include.h" | 2 | #include "../base/base_include.h" |
| 3 | 3 | ||
| 4 | #if 0 | 4 | #ifdef BTREE_IMPLEMENTATION |
| 5 | internal void | ||
| 6 | btree_collect(btree_node *node, (void *) *keys, (void *) array *) { | ||
| 5 | 7 | ||
| 6 | #include "../third_party/btree_impl.h" | 8 | if (!n) return; |
| 9 | |||
| 10 | for (s32 index = 0; index < node->count; ++index) | ||
| 11 | { | ||
| 12 | TODO(nasr): traverse the tree to use the tree as training data | ||
| 13 | } | ||
| 14 | } | ||
| 7 | #endif | 15 | #endif |
| 8 | 16 | ||
| 9 | 17 | ||
| 18 | /** | ||
| 19 | * calculating a derviate requires a building an algebraic system | ||
| 20 | * so we do a dump approximation of what the derivative could be | ||
| 21 | * we want this to be able to normalize the data which is necessary for proper training | ||
| 22 | **/ | ||
| 23 | #if 1 | ||
| 24 | internal f32 | ||
| 25 | internal training_sample_data | ||
| 26 | normalize_training_data_btree(btree *btree) { | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | } | ||
| 31 | |||
| 32 | #endif | ||
| 10 | 33 | ||
| 11 | //- dataset is an implementation of the btree | ||
| 12 | 34 | ||
| 13 | int main(int c, char **v) | 35 | // using gradient descent |
| 14 | { | 36 | internal void |
| 15 | unused(c); | 37 | train(f32 *weights, b_tree, s32 learning_rate) { |
| 16 | unused(v); | 38 | |
| 39 | btree_collect(btree_node *node, (void *) *keys, (void *) array *); | ||
| 40 | |||
| 41 | for (s32 epoch = 0; epoch < learning_rate; ++epoch) | ||
| 42 | { | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | } | ||
| 47 | |||
| 48 | internal f32 | ||
| 49 | predict(f32 b1, f32 b2) { | ||
| 50 | return b1 + (b2 * log(b1)) + M_E; | ||
| 51 | } | ||
| 52 | |||
| 53 | //- dataset is an implementation of the btree | ||
| 54 | int main(void) { | ||
| 17 | 55 | ||
| 56 | for (s32 index = 0; index < 10; ++index) | ||
| 57 | { | ||
| 58 | printf("value [%4.f]\n",predict(10, 10)); | ||
| 59 | } | ||
| 18 | 60 | ||
| 19 | return 0; | 61 | return 0; |
| 20 | } | 62 | } |
