Skip to content

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ML

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.

About

ML is a small machine learning library written in C, inspired by tsoding's videos and project.

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.

Current Features

  1. Matrix structure and operations (alloc, free, mul, transpose, slice, etc.)
  2. Multi-layer neural network with forward propagation
  3. Activation functions: sigmoid, ReLU, Leaky ReLU, tanh, GELU, swish, linear, softmax
  4. Loss functions: SSE, MSE, BCE, CCE, softmax + CCE
  5. Weight initialization: Xavier and uniform random
  6. Backpropagation and finite difference gradient estimation
  7. SGD optimizer with mini-batch training
  8. Model save and load (binary format)
  9. Data loading: CSV and binary (MLDB) formats
  10. Model verification: binary (threshold) and multi-class (argmax) accuracy
  11. Label utilities: one-hot encoding and argmax

Tools

  • tools/img2bin — PNG images → MLDB binary data
  • train_visualizer.c — real-time training visualization (cost curve + decision boundary)
  • check.c — load model + run single-image inference

Data Loading

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

Activation Functions

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

Loss Functions

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

Logging

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 -lm

Without -D, defaults to level 1.

Build

A simple build script is provided:

./build.sh

You can also compile manually:

gcc -Wall -Wextra -O3 ml.c -o ml -lm

Some 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 -lm

The 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 -lm

Project Style

This 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.

Notes

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.

Possible Future Directions

Some possible future directions include:

  1. Cleaner stb-style organization
  2. More flexible layer abstraction
  3. Separate loss and optimizer modules
  4. Better model serialization
  5. GPU backend experiments
  6. A more general ML library architecture

(None of these are guaranteed)

License

This project is licensed under the MIT License.

About

A stb-style (for now) ML library built while learning. Future updates depend on what I want to learn and what I want to change.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages