summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2026-04-19 18:38:51 +0200
committernasr <nsrddyn@gmail.com>2026-04-19 18:38:51 +0200
commit4bd3064b6571dec04d17d67b8e6fd3128ceb1f09 (patch)
tree28cd7e577d553c87956fc41f60100066637d21d7
parent36dc1f859a13e428c441fc8f4f35550fe12ed72f (diff)
feature(main): working on the design. i dont know how machine learning works pfff...main
-rw-r--r--Makefile51
-rw-r--r--README.md33
-rw-r--r--source/tb_ml/tb_mh.h43
-rw-r--r--source/tb_ml/tb_ml.c58
4 files changed, 154 insertions, 31 deletions
diff --git a/Makefile b/Makefile
index 2f5d478..9846889 100644
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,42 @@
1BIN = build/tb_ml 1# Build configuration
2SRC = source/tb_ml/tb_ml.c 2BIN := build/tb_ml
3SRC := source/tb_ml/tb_ml.c
4OBJ := build/tb_ml.o
5DEP := build/tb_ml.d
3 6
4# CC = gcc 7# Compiler flags
5CC = clang 8CC ?= clang
9CFLAGS := -Wall -Wextra -Wpedantic -Wno-unused-function -g --std=c99 -fno-omit-frame-pointer
10LDFLAGS := -lm
6 11
7COMPILER := $(shell $(CC) --version | grep -o "gcc\|clang" | head -1) 12# Optimization level
13OPT ?= 0
14CFLAGS += -O$(OPT)
8 15
9# check for compile optimizations per compiler 16# Build directory
10ifeq ($(COMPILER),gcc) 17$(shell mkdir -p build)
11 CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -Werror -O0
12else ifeq ($(COMPILER),clang)
13 CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0
14else
15 CFLAGS = -Wall -Wextra -Wpedantic -Wno-unused-function -g -O0
16endif
17 18
18$(BIN): $(SRC) 19# Targets
19 mkdir -p build 20.PHONY: all run clean help
20 $(CC) $(CFLAGS) $< -o $@ 21
22all: $(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
22run: $(BIN) 32run: $(BIN)
23 $(BIN) 33 ./$(BIN)
24 34
25.PHONY: clean
26clean: 35clean:
27 rm -rf build 36 rm -rf build
37
38help:
39 @echo "make [OPT=0|1|2|3]"
40 @echo " all - Build (default)"
41 @echo " run - Build and run"
42 @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 @@
1TB_ML 1# TB_ML
2 2
3Attempt at writing a machine learning library in c using tb_db for the input data. 3An 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
5pseudo random number generation 6---
6https://www.youtube.com/watch?v=OlSYfj8VZi0 7
7https://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
30Work 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
9typedef struct training_sample trainig_sample;
10struct training_sample
11{
12
13};
14
15typedef struct prediction_data predicition_data;
16struct prediction_data
17{
18 f32 w;
19 f32 y;
20};
21
22
23typedef struct prediction_category prediction_category;
24struct category
25{
26 string8 name;
27 predicition_data data;
28};
29
30f(f32 d) {
31 return d * d;
32}
33
34internal f32
35derivative(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
5internal void
6btree_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
24internal f32
25internal training_sample_data
26normalize_training_data_btree(btree *btree) {
27
28
29
30}
31
32#endif
10 33
11//- dataset is an implementation of the btree
12 34
13int main(int c, char **v) 35// using gradient descent
14{ 36internal void
15 unused(c); 37train(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
48internal f32
49predict(f32 b1, f32 b2) {
50 return b1 + (b2 * log(b1)) + M_E;
51}
52
53//- dataset is an implementation of the btree
54int 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}