From 4bd3064b6571dec04d17d67b8e6fd3128ceb1f09 Mon Sep 17 00:00:00 2001 From: nasr Date: Sun, 19 Apr 2026 18:38:51 +0200 Subject: feature(main): working on the design. i dont know how machine learning works pfff... --- Makefile | 51 +++++++++++++++++++++++++++++---------------- README.md | 33 +++++++++++++++++++++++++----- source/tb_ml/tb_mh.h | 43 ++++++++++++++++++++++++++++++++++++++ source/tb_ml/tb_ml.c | 58 ++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 source/tb_ml/tb_mh.h diff --git a/Makefile b/Makefile index 2f5d478..9846889 100644 --- a/Makefile +++ b/Makefile @@ -1,27 +1,42 @@ -BIN = build/tb_ml -SRC = source/tb_ml/tb_ml.c +# Build configuration +BIN := build/tb_ml +SRC := source/tb_ml/tb_ml.c +OBJ := build/tb_ml.o +DEP := build/tb_ml.d -# CC = gcc -CC = clang +# Compiler flags +CC ?= clang +CFLAGS := -Wall -Wextra -Wpedantic -Wno-unused-function -g --std=c99 -fno-omit-frame-pointer +LDFLAGS := -lm -COMPILER := $(shell $(CC) --version | grep -o "gcc\|clang" | head -1) +# Optimization level +OPT ?= 0 +CFLAGS += -O$(OPT) -# check for compile optimizations per compiler -ifeq ($(COMPILER),gcc) - CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -Werror -O0 -else ifeq ($(COMPILER),clang) - CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0 -else - CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0 -endif +# Build directory +$(shell mkdir -p build) -$(BIN): $(SRC) - mkdir -p build - $(CC) $(CFLAGS) $< -o $@ +# Targets +.PHONY: all run clean help + +all: $(BIN) + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + +$(OBJ): $(SRC) + $(CC) $(CFLAGS) -MT $@ -MMD -MP -MF $(DEP) -c $< -o $@ + +-include $(DEP) run: $(BIN) - $(BIN) + ./$(BIN) -.PHONY: clean clean: rm -rf build + +help: + @echo "make [OPT=0|1|2|3]" + @echo " all - Build (default)" + @echo " run - Build and run" + @echo " clean - Remove artifacts" diff --git a/README.md b/README.md index b059617..c4a4964 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,30 @@ -TB_ML +# TB_ML -Attempt at writing a machine learning library in c using tb_db for the input data. +An attempt at writing a machine learning library in C, using `tb_db` as the data input layer. +[tb_db](https://git.nsrddyn.com/tb_db.git/) -pseudo random number generation -https://www.youtube.com/watch?v=OlSYfj8VZi0 -https://www.pcg-random.org/ +--- + +## References + +- [Computerphile – Random Numbers (YouTube)](https://www.youtube.com/watch?v=OlSYfj8VZi0) +- [PCG Random – O'Neill (pcg-random.org)](https://www.pcg-random.org/) +- [Pattern Recognition and Machine Learning – Bishop (PDF)](https://www.cs.uoi.gr/~arly/courses/ml/tmp/Bishop_book.pdf) +- [Mean Squared Error – Wikipedia](https://en.wikipedia.org/wiki/Mean_squared_error) +- [Lineaire Benadering – Wikipedia (NL)](https://nl.wikipedia.org/wiki/Lineaire_benadering) +- [Partial Differential Equations – Wikipedia](https://en.wikipedia.org/wiki/Partial_differential_equation) + +- [ChatGPT] used for navigating unfamiliar topics and finding relevant material +- [Claude] used for mathematical implementations because I suck + +--- + +## Dependencies + +- [`tb_db`](https://git.nsrddyn.com/tb_db) — custom CSV query engine / database layer + +--- + +## Status + +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 @@ +#ifndef TB_ML_H +#define TB_ML_H + + +//- supervised learning model by abdellah el morabit +//- from +#define M_E 2.7182818284590452354 /* e */ + +typedef struct training_sample trainig_sample; +struct training_sample +{ + +}; + +typedef struct prediction_data predicition_data; +struct prediction_data +{ + f32 w; + f32 y; +}; + + +typedef struct prediction_category prediction_category; +struct category +{ + string8 name; + predicition_data data; +}; + +f(f32 d) { + return d * d; +} + +internal f32 +derivative(f32 x, f32 (*f)(f32)) { + + f32 h = 1e-6; // small interval to differnetiate the scope + return (f(x + h) - f(x - h)) / (2.0 * h); +} + + + +#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 @@ -#define BASE_UNITY +#define BASE_IMPLEMENTATION #include "../base/base_include.h" -#if 0 +#ifdef BTREE_IMPLEMENTATION +internal void +btree_collect(btree_node *node, (void *) *keys, (void *) array *) { -#include "../third_party/btree_impl.h" + if (!n) return; + + for (s32 index = 0; index < node->count; ++index) + { + TODO(nasr): traverse the tree to use the tree as training data + } +} #endif +/** +* calculating a derviate requires a building an algebraic system +* so we do a dump approximation of what the derivative could be +* we want this to be able to normalize the data which is necessary for proper training +**/ +#if 1 +internal f32 +internal training_sample_data +normalize_training_data_btree(btree *btree) { + + + +} + +#endif -//- dataset is an implementation of the btree -int main(int c, char **v) -{ - unused(c); - unused(v); +// using gradient descent +internal void +train(f32 *weights, b_tree, s32 learning_rate) { + + btree_collect(btree_node *node, (void *) *keys, (void *) array *); + + for (s32 epoch = 0; epoch < learning_rate; ++epoch) + { + + } + +} + +internal f32 +predict(f32 b1, f32 b2) { + return b1 + (b2 * log(b1)) + M_E; +} + +//- dataset is an implementation of the btree +int main(void) { + for (s32 index = 0; index < 10; ++index) + { + printf("value [%4.f]\n",predict(10, 10)); + } return 0; } -- cgit v1.3