A stb-style (for now) ML library built while learning. The Git commit history shows my learning path. Future updates depend on what I want to learn and what I want to change.
ML is a small machine learning library written in C, inspired by tsoding's videos and project.
- https://www.youtube.com/playlist?list=PLpM-Dvs8t0VZPZKggcql-MmjaBdZKeDMw
- https://github.com/tsoding/nn.h
This project is not intended to be a production-ready machine learning framework. It is a learning-oriented project where I implement the basic parts of neural networks from scratch.
- Matrix structure and operations (alloc, free, mul, transpose, slice, etc.)
- Multi-layer neural network with forward propagation
- Activation functions: sigmoid, ReLU, Leaky ReLU, tanh, GELU, swish, linear, softmax
- Loss functions: SSE, MSE, BCE, CCE, softmax + CCE
- Weight initialization: Xavier and uniform random
- Backpropagation and finite difference gradient estimation
- SGD optimizer with mini-batch training
- Model save and load (binary format)
- Data loading: CSV and binary (MLDB) formats
- Model verification: binary (threshold) and multi-class (argmax) accuracy
- Label utilities: one-hot encoding and argmax
tools/img2bin— PNG images → MLDB binary datatrain_visualizer.c— real-time training visualization (cost curve + decision boundary)check.c— load model + run single-image inference
| Function | Format | Description |
|---|---|---|
ml_data_load_csv |
.csv |
CSV file (header row + comma-separated floats) |
ml_data_load_bin |
.bin |
MLDB binary ("MLDB" magic + rows/cols + raw floats) |
Both produce a Mat where each row is [input features ... | target values ...].
MLDB files are generated by the preprocessing tool:
./tools/img2bin --max 100 train train_1k.bin 10| Symbol | Function | Derivative |
|---|---|---|
ML_SIGMOID |
sigmoid | dsigmoidf |
ML_RELU |
ReLU | dReLUf |
ML_LRELU |
Leaky ReLU (α=0.01) | dLReLUf |
ML_TANH |
tanh | dtanhf |
ML_GELU |
GELU | dGeLUf |
ML_SWISH |
swish | dswishf |
ML_LINEAR |
identity | dlinearf |
softmaxf() |
row-wise softmax | — |
| Forward | Backward | Use |
|---|---|---|
ml_model_loss_sse |
ml_model_loss_dsse |
Sum of squared errors |
ml_model_loss_mse |
ml_model_loss_dmse |
Mean squared error |
ml_model_loss_bce |
ml_model_loss_dbce |
Binary cross-entropy |
ml_model_loss_cce |
ml_model_loss_dcce |
Categorical cross-entropy (one-hot) |
ml_model_loss_softmax_cce |
ml_model_loss_dsoftmax_cce |
Softmax + CCE combined |
Define ML_LOG_LEVEL before including ml.h to control log output:
| Level | Output | Usage |
|---|---|---|
0 |
none (zero overhead) | Release builds |
1 |
LOG_ERROR |
Production (default) |
2 |
+ LOG_WARN |
Tuning |
3 |
+ LOG_INFO |
Training progress |
4 |
+ LOG_DEBUG |
Debugging |
example:
gcc -DML_LOG_LEVEL=3 ml.c -o ml -lmWithout -D, defaults to level 1.
A simple build script is provided:
./build.shYou can also compile manually:
gcc -Wall -Wextra -O3 ml.c -o ml -lmSome experiments may require external libraries such as raylib.
For example, if you want to build the visualization program, you may need something like:
gcc -Wall -Wextra -O3 train_visualizer.c -o train_visualizer -lraylib -lmThe exact command may depend on your system and installed libraries.
The preprocessing tool (tools/img2bin) requires stb_image.h, which is not tracked in this repository. Download it before building:
wget -O tools/stb_image.h https://raw.githubusercontent.com/nothings/stb/master/stb_image.h
gcc -O3 tools/img2bin.c -o tools/img2bin -lmThis project is currently written in a stb-style direction. This may change in the future. The current structure is not final, and future changes will depend on what I want to learn next.
This repository is experimental. Code may be rewritten, renamed, removed, or reorganized at any time. The main purpose of the project is learning, not API stability. That said, you are welcome to fork this project and experiment with it yourself.
Some possible future directions include:
- Cleaner stb-style organization
- More flexible layer abstraction
- Separate loss and optimizer modules
- Better model serialization
- GPU backend experiments
- A more general ML library architecture
(None of these are guaranteed)
This project is licensed under the MIT License.