A lightweight, high-performance neural network library written entirely from scratch in C. Built with explicit memory management, a modular matrix math backend, function pointers for custom activations, and zero external dependencies.
- Modular Matrix Backend: Dedicated matrix operations (
matrix.c/matrix.h) handling tensor allocations, dot products, and element-wise transforms. - Flexible Activations & Losses: Clean separation using function pointers for ReLU and Sigmoid activations, their derivatives, and Mean Squared Error (MSE) loss.
- Explicit Backpropagation: Hand-coded forward and backward passes calculating gradients directly across weight and bias buffers.
- Weight Serialization: Robust binary streaming functions (
network_save_weightsandnetwork_load_weights) to persist trained models to disk. - Zero Dependencies: Relies solely on standard C libraries and standard math linking (
-lm).
├── nn.h # Public API, layer/network definitions, and function prototypes
├── nn.c # Core framework logic (forward pass, backprop, SGD, serialization)
├── matrix.c # Matrix math operations and memory handling
├── main.c # General entry point
├── xor.c # Logic gate training script
├── concentric-circles.c # Non-convex radial classification experiment
└── two-moons.c # Interleaving crescent manifold experiment
The repository includes a Makefile configured with optimized build targets for each experiment.
- XOR Logic Gate:
make xorormake xor-fast(-O3optimized) - Concentric Circles:
make concentric-circlesormake concentric-circles-fast - Two Moons:
make two-moonsormake two-moons-fast
To build and run the optimized Two Moons experiment:
make two-moons-fast
./app
To clean up compiled binaries:
make clean
Here is how simple it is to construct a multi-layer perceptron, train it, and save the weights using the framework's API:
#include "nn.h"
#include <stdio.h>
int main() {
// Initialize Network with MSE loss and SGD optimizer
Network *net = network_create(loss_mse, loss_mse_prime, optimizer_sgd);
// Build Architecture:
network_add(net, layer_create(2, 16, activation_relu, activation_relu_prime));
network_add(net, layer_create(16, 16, activation_relu, activation_relu_prime));
network_add(net, layer_create(16, 1, activation_sigmoid, activation_sigmoid_prime));
// Train via network_train_step(...)
// ...
// Save trained weights to disk
network_save_weights(net, "model.weights");
network_free(net);
return 0;
}The framework has been successfully benchmarked on several non-linear spatial classification tasks:
- XOR: Basic truth-table logic separation.
- Concentric Circles: Learning non-convex radial boundaries by utilizing ReLU hidden layers to punch a clean decision hole through the center.
- Two Moons: Carving out an intricate S-shaped manifold to separate interleaving crescent distributions.