diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-19 18:38:51 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-19 18:38:51 +0200 |
| commit | 4bd3064b6571dec04d17d67b8e6fd3128ceb1f09 (patch) | |
| tree | 28cd7e577d553c87956fc41f60100066637d21d7 | |
| parent | 36dc1f859a13e428c441fc8f4f35550fe12ed72f (diff) | |
feature(main): working on the design. i dont know how machine learning works pfff...main
| -rw-r--r-- | Makefile | 51 | ||||
| -rw-r--r-- | README.md | 33 | ||||
| -rw-r--r-- | source/tb_ml/tb_mh.h | 43 | ||||
| -rw-r--r-- | source/tb_ml/tb_ml.c | 58 |
4 files changed, 154 insertions, 31 deletions
| @@ -1,27 +1,42 @@ | |||
| 1 | BIN = build/tb_ml | 1 | # Build configuration |
| 2 | SRC = source/tb_ml/tb_ml.c | 2 | BIN := build/tb_ml |
| 3 | SRC := source/tb_ml/tb_ml.c | ||
| 4 | OBJ := build/tb_ml.o | ||
| 5 | DEP := build/tb_ml.d | ||
| 3 | 6 | ||
| 4 | # CC = gcc | 7 | # Compiler flags |
| 5 | CC = clang | 8 | CC ?= clang |
| 9 | CFLAGS := -Wall -Wextra -Wpedantic -Wno-unused-function -g --std=c99 -fno-omit-frame-pointer | ||
| 10 | LDFLAGS := -lm | ||
| 6 | 11 | ||
| 7 | COMPILER := $(shell $(CC) --version | grep -o "gcc\|clang" | head -1) | 12 | # Optimization level |
| 13 | OPT ?= 0 | ||
| 14 | CFLAGS += -O$(OPT) | ||
| 8 | 15 | ||
| 9 | # check for compile optimizations per compiler | 16 | # Build directory |
| 10 | ifeq ($(COMPILER),gcc) | 17 | $(shell mkdir -p build) |
| 11 | CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -Werror -O0 | ||
| 12 | else ifeq ($(COMPILER),clang) | ||
| 13 | CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0 | ||
| 14 | else | ||
| 15 | CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0 | ||
| 16 | endif | ||
| 17 | 18 | ||
| 18 | $(BIN): $(SRC) | 19 | # Targets |
| 19 | mkdir -p build | 20 | .PHONY: all run clean help |
| 20 | $(CC) $(CFLAGS) $< -o $@ | 21 | |
| 22 | all: $(BIN) | ||
| 23 | |||
| 24 | $(BIN): $(OBJ) | ||
| 25 | $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ | ||
| 26 | |||
| 27 | $(OBJ): $(SRC) | ||
| 28 | $(CC) $(CFLAGS) -MT $@ -MMD -MP -MF $(DEP) -c $< -o $@ | ||
| 29 | |||
| 30 | -include $(DEP) | ||
| 21 | 31 | ||
| 22 | run: $(BIN) | 32 | run: $(BIN) |
| 23 | $(BIN) | 33 | ./$(BIN) |
| 24 | 34 | ||
| 25 | .PHONY: clean | ||
| 26 | clean: | 35 | clean: |
| 27 | rm -rf build | 36 | rm -rf build |
| 37 | |||
| 38 | help: | ||
| 39 | @echo "make [OPT=0|1|2|3]" | ||
| 40 | @echo " all - Build (default)" | ||
| 41 | @echo " run - Build and run" | ||
| 42 | @echo " clean - Remove artifacts" | ||
| @@ -1,7 +1,30 @@ | |||
| 1 | TB_ML | 1 | # TB_ML |
| 2 | 2 | ||
| 3 | Attempt at writing a machine learning library in c using tb_db for the input data. | 3 | An attempt at writing a machine learning library in C, using `tb_db` as the data input layer. |
| 4 | [tb_db](https://git.nsrddyn.com/tb_db.git/) | ||
| 4 | 5 | ||
| 5 | pseudo random number generation | 6 | --- |
| 6 | https://www.youtube.com/watch?v=OlSYfj8VZi0 | 7 | |
| 7 | https://www.pcg-random.org/ | 8 | ## References |
| 9 | |||
| 10 | - [Computerphile – Random Numbers (YouTube)](https://www.youtube.com/watch?v=OlSYfj8VZi0) | ||
| 11 | - [PCG Random – O'Neill (pcg-random.org)](https://www.pcg-random.org/) | ||
| 12 | - [Pattern Recognition and Machine Learning – Bishop (PDF)](https://www.cs.uoi.gr/~arly/courses/ml/tmp/Bishop_book.pdf) | ||
| 13 | - [Mean Squared Error – Wikipedia](https://en.wikipedia.org/wiki/Mean_squared_error) | ||
| 14 | - [Lineaire Benadering – Wikipedia (NL)](https://nl.wikipedia.org/wiki/Lineaire_benadering) | ||
| 15 | - [Partial Differential Equations – Wikipedia](https://en.wikipedia.org/wiki/Partial_differential_equation) | ||
| 16 | |||
| 17 | - [ChatGPT] used for navigating unfamiliar topics and finding relevant material | ||
| 18 | - [Claude] used for mathematical implementations because I suck | ||
| 19 | |||
| 20 | --- | ||
| 21 | |||
| 22 | ## Dependencies | ||
| 23 | |||
| 24 | - [`tb_db`](https://git.nsrddyn.com/tb_db) — custom CSV query engine / database layer | ||
| 25 | |||
| 26 | --- | ||
| 27 | |||
| 28 | ## Status | ||
| 29 | |||
| 30 | Work in progress. | ||
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 | } |
