A general-purpose, from-scratch C++ implementation of a multi-layer perceptron (a fully connected, feed-forward neural network), with no external machine-learning libraries. The NeuralNetwork class supports configurable network architectures, multiple activation functions, both regression and classification, and is trained with backpropagation and (stochastic) gradient descent. The included console application demonstrates the library by training a network to add two numbers.
- General-purpose
NeuralNetworkclass — define the layer sizes and per-layer activation functions at construction time. - Arbitrary architecture — fully connected feed-forward networks of any depth and width.
- Activation functions — Linear, Sigmoid, Tanh, and ReLU (with their derivatives), provided by the
MathAImath library. - Training — backpropagation with gradient descent / stochastic gradient descent, and configurable learning rate, epoch count, and mini-batch size.
- Loss — Mean Squared Error for regression tasks.
- Inference — both regression (
Predict) and classification (Classify, returning the arg-max class). - Progress reporting — per-epoch loss is written to a CSV log, and live training progress is printed to the console.
- Zero dependencies — pure standard C++ (
<vector>,<random>,<cmath>,<fstream>, …); no third-party frameworks. - Self-contained demo — generates synthetic data and trains a network end-to-end, then drops into an interactive test loop.
Note:
The class also declares enums for Cross-Entropy loss and the Adam optimizer, plusSaveModel/LoadModelhooks. These are placeholders for future work. The implemented paths are MSE loss with gradient descent / SGD.
A multi-layer perceptron is a stack of fully connected layers. Each neuron computes a weighted sum of the previous layer's outputs plus a bias, then passes the result through an activation function. Training adjusts the weights and biases by propagating the output error backwards through the network (backpropagation) and nudging each parameter down the loss gradient.
The demo application (application.cpp) wires the class up into a complete, runnable example that teaches a network to add two numbers:
- A
{ 2, 3, 1 }network — 2 inputs, one hidden layer of 3 ReLU neurons, and a single linear output. - 10,000 rows of synthetic training data: two features
x0,x1drawn uniformly from[0, 1], with targety = (x0 + x1) / 2. - 200 epochs of stochastic gradient descent, batch size 50, learning rate 0.001, Mean Squared Error loss.
The training data is generated to Data\training_data.csv (features and target, one sample per row):
Running the program generates the data, builds and trains the network while reporting loss, and then lets you feed it your own pairs of numbers to see what it predicts:
After building, run the executable from the NeuralNetwork project directory so that the relative Data\ path resolves:
NeuralNetwork\x64\Release\NeuralNetwork.exeThe program will:
- Generate
Data\training_data.csv(10,000 synthetic samples). - Initialise the network and print its configuration.
- Train for 200 epochs, logging each epoch's loss to
Data\training_results.csvand showing live progress. - Drop into an interactive loop — enter two integer values and the network predicts their sum. Type
exitto quit.
Example interaction:
- Enter
x0 = 42andx1 = 150; the trained network predicts a sum of roughly192. - Inputs are scaled into
[0, 1]before inference and the output is scaled back, so any two numbers in range can be tried.
The NeuralNetwork class is the reusable part of the project. Construct it with an architecture and hyperparameters, Train it on your data, then call Predict (regression) or Classify (classification):
#include "neural_network.h"
// Define a 2 -> 3 -> 1 network: a ReLU hidden layer and a linear output.
vector <int> layers = { 2, 3, 1 };
vector <ActivationFunction> activation_functions = { RELU, LINEAR };
NeuralNetwork neural_network
(
layers,
activation_functions,
MEAN_SQUARED_ERROR, // Loss function.
0.001, // Learning rate.
200, // Epoch count.
50, // Batch size.
STOCHASTIC_GRADIENT_DESCENT, // Optimization algorithm.
"Data\\training_results.csv" // Per-epoch loss log.
);
// Train with backpropagation, then run inference.
neural_network.Train ( training_data_x, training_data_y );
MathAI::Vector y = neural_network.Predict ( { 0.042, 0.150 } ); // Regression: continuous output.
int label = neural_network.Classify ( features ); // Classification: arg-max class index.- Visual Studio 2022 with the "Desktop development with C++" workload (MSVC v143 / v145 toolset).
-
Clone the repository:
git clone https://github.com/rohingosling/neural-network-cpp.git
-
Open
NeuralNetwork.slnin Visual Studio. -
Select a configuration and platform from the toolbar —
Release/x64is recommended. -
Build the solution (Build → Build Solution, or
Ctrl+Shift+B). -
Run it (Debug → Start Without Debugging, or
Ctrl+F5). When launched from Visual Studio the working directory is the project folder, so theData\directory is found automatically.
From a Developer Command Prompt for VS 2022, in the repository root:
msbuild NeuralNetwork.sln /p:Configuration=Release /p:Platform=x64Then run the executable from the project directory so the relative Data\ path resolves:
cd NeuralNetwork
x64\Release\NeuralNetwork.exeThe program reads and writes CSV files under
Data\using paths relative to the working directory. Run the executable from theNeuralNetworkproject folder (which containsData\), or copy theData\folder next to the binary.
neural-network-cpp
├─ NeuralNetwork.sln Visual Studio solution
│
└─ NeuralNetwork Project directory
├─ NeuralNetwork.vcxproj MSBuild project file
├─ main.cpp Entry point - constructs and runs the demo Application
├─ application.h / .cpp Demo harness - data generation, training, interactive test loop
├─ neural_network.h / .cpp NeuralNetwork class - MLP, forward/back propagation, training
├─ math_ai.h / .cpp MathAI - activation functions, derivatives, vector math
├─ Data Training data and per-epoch loss logs (CSV)
└─ Image Diagrams and screenshots used in this README
Released under the MIT License — Copyright © 2014 Rohin Gosling.



