A tiny, single-header, dependency-free Multi-Layer Perceptron library for C.
Drop MLP.h into your project — no build system, no linking, no
external dependencies beyond the standard library.
Version: 0.10.0 · License: MIT
Compile with:
cd examples && gcc mnist.c -O3 -flto -ffast-math -march=native./a
[########################################]
100% Epoch 50/ 50 Loss 1.904e-02
Training completed.
Epochs : 50
Final Loss : 1.90362931e-02
Reason : Maximum epochs reached
Score: 9778/10000
Accuracy: 97.78%
Live Classifier: https://px7nn.github.io/MNIST/
- Zero External Dependencies: Pure, portable C99/C11. No linking required, with optional
<math.h>support (MLP_USE_LIBM). - Flexible Network Configuration: Configure arbitrary topologies, activation functions (
ReLU,Leaky ReLU,Sigmoid,Tanh,Softmax,Linear), weight initializers (He,Xavier, orMLP_AUTO_INITIALIZERS), and loss functions (MSE,BCE,CCE, or automatic inference viaLOSS_AUTO) viaNetworkConfig. - Model Persistence: Easily save and load trained networks to/from disk using compact binary files.
- Built-in CSV Parsing: Streamline dataset preparation with automated CSV loading (
MLP_LoadCSV) or wrap existing memory arrays. - Structured Error Handling: Features a robust global error reporting system with an opt-in fail-fast check (
MLP_EXIT_ON_ERROR) to keep client code completely clean.
#define MLP_IMPLEMENTATION // in exactly one .c file
#include "MLP.h"See docs/getting_started.md for a full
walkthrough.
See examples/ for full training examples:
xor_gate.c— trains a network on XOR and saves it toxor.mlp.load_model.c— loadsxor.mlpand runs inference without retraining.load_csv.c— loadscircle.csvviaMLP_LoadCSV, trains, and predicts.mnist.c— trains on handwritten digits usingSoftmaxandCategorical Cross Entropy(compatible with the Live MNIST Canvas Demo).visual_sin.c— fits a sine wave and saves a visualization plot.
MLP_VERSION_STRING (and the matching _MAJOR/_MINOR/_PATCH macros)
are defined at the top of MLP.h. This project is pre-1.0, so the public
API may still change between minor versions.
