A lightweight C++ neural network implementation with SFML-based visualisation. Users can interactively train a simple feedforward network to classify points by clicking within a 2D graph interface.
This project was made using SFML's CMake project template. For more information see Cmake SFML GitHub repo
To visualise the costs run cost_plot.py
Depends on numpy and matplotlib.
Uses a header only json library
to dump number of epochs and output frequency after each run, which is read
the python script.
- C++14 or higher
- SFML 3.0 (fetched automatically via CMake)
- Armadillo for linear algebra
- CMake 3.14+
- nlohmann/json to dump config data for post processing in python.
git clone https://github.com/yourname/neuralnet-visualiser.git
cd neuralnet-visualiser
cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
./build/bin/main- Left-click to add a red class point
- Right-click to add a blue class point
- The network will train to classify these points using backpropagation
- Live prediction updates are displayed on the grid as colour shading
All neural network structure is defined at compile time in config.h:
constexpr std::array<int, 3> LAYERS = {2, 3, 2}; // input → hidden → output
constexpr int NGRID = 20; // resolution of visualisation grid
// Change this to something like {2, 4, 3, 2} for a deeper network.Note that the first and last layers need a size of 2 as the code is currently set up. This is may can be easily changed but is currently enforced in main.cpp.
| File | Purpose |
|---|---|
| main.cpp | Entry point, event loop setup |
| graph.h/cpp | Handles graph display and user input |
| NeuralNet.h/cpp | Neural network implementation: forward + backprop |
| nodes.h/cpp | Activation functions, visual node logic |
| updateLoop.h/cpp | Training loop and visual update logic |
| config.h | Central place for defining layer sizes, grid resolution, etc. |
- Armadillo cubes (arma::cube) are used to handle batched matrix operations efficiently across training samples.
- Compile-time fixed network layout using std::array ensures consistent sizing and avoids heap allocations.
- Backpropagation is implemented manually, with helpers like batched_matmul, transpose_cube, and contract_sum.
- SFML provides the interactive GUI, mapping screen coordinates to training inputs.
Use -DCMAKE_BUILD_TYPE=Debug in CLion or CMake to enable step-through debugging. The code is designed to be simple and modifiable — great for educational purposes or prototyping small neural nets. Ideal for learning or demonstrating neural network principles interactively.
MIT License — free to use, modify, and distribute.
