A small, from-scratch C++ neural network library for demonstrative use. The goal is to keep the API easy to use while staying flexible enough for experimentation and learning. It focuses on simple feed-forward dense networks that you can assemble by hand, train with backpropagation, and inspect at each step.
- Demonstrate how feed-forward neural networks work without heavy dependencies.
- Provide a compact, approachable API for building and training small networks.
- Stay flexible so you can customize activations, layer sizes, and training loops.
- Dense (fully connected) layers with configurable activations.
- Common activations: Linear, ReLU, LeakyReLU, Sigmoid, Logistic, Tanh.
- Xavier and Kaiming weight initialization in
LayerConnection. - Forward propagation and backpropagation for gradient-based learning.
- Lightweight
MatrixandVectortypes for basic linear algebra. - Optional helper utilities on
NeuralNetworksuch astrain,evaluate, andpredict.
Use the umbrella header:
#include "bbdnn/bbdnn.hpp"Or include individual headers such as:
#include "bbdnn/Activations.hpp"All library types live in the bbdnn namespace.
The demo trains a tiny network on XOR and prints predictions:
- Builds a 2-3-1 network: input layer (2), hidden layer (3), output layer (1).
- Uses
Linear,Tanh, andSigmoidactivations. - Trains on XOR samples with
train(features, labels, learningRate, epochs, isStochastic). - Uses
predictto get outputs for each input.
Key snippet from the demo:
NeuralNetwork nn(seed, {
DenseLayer(2, Activation::Linear()),
DenseLayer(8, Activation::Tanh()),
DenseLayer(8, Activation::Tanh()),
DenseLayer(1, Activation::Sigmoid()),
});
std::vector<Vector> features {
Vector{0.0f, 0.0f},
Vector{0.0f, 1.0f},
Vector{1.0f, 0.0f},
Vector{1.0f, 1.0f},
};
std::vector<Vector> labels {
Vector{0.0f},
Vector{1.0f},
Vector{1.0f},
Vector{0.0f},
};
// Performs full-batch training `epochs` times
nn.train(features, labels, 0.05f, 10000, false);Sample output from demo:
Input: 0 0 => Prediction: 0.0049705
Input: 0 1 => Prediction: 0.989448
Input: 1 0 => Prediction: 0.987596
Input: 1 1 => Prediction: 0.0121131
This project uses CMake:
cmake -S . -B build
cmake --build buildThe nn_demo executable will be built from examples/nn_demo.cpp.
There is also a simple Makefile in the project root:
make
./nn_demoTo clean build artifacts:
make clean