Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Neural Network Framework in C

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.


Features

  • 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_weights and network_load_weights) to persist trained models to disk.
  • Zero Dependencies: Relies solely on standard C libraries and standard math linking (-lm).

Project Structure

├── 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


Building and Running

The repository includes a Makefile configured with optimized build targets for each experiment.

Available Make Targets

  • XOR Logic Gate: make xor or make xor-fast (-O3 optimized)
  • Concentric Circles: make concentric-circles or make concentric-circles-fast
  • Two Moons: make two-moons or make two-moons-fast

Example Usage

To build and run the optimized Two Moons experiment:

make two-moons-fast
./app

To clean up compiled binaries:

make clean

Example Code (Building a Network)

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;
}

Validated Datasets

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.

About

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.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages