Skip to content

aevyy/NeuralNetwork-CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

104 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Neural Network in C++

A neural network library built from scratch in C++17. No external ML dependencies.

Supports configurable network topologies, multiple activation functions, three optimizers (SGD, SGD with momentum, Adam), two loss functions (MSE, cross-entropy), and training utilities like validation splits and early stopping.

Build

make build

Requires CMake 3.15+ and a C++17 compiler.

Usage

./build/main train    # train using config/train.json
./build/main test     # evaluate using config/test.json
./build/main demo     # run XOR demo

Configuration

Training and testing are configured through JSON files in config/.

{
  "topology": [2, 8, 1],
  "learningRate": 0.05,
  "momentum": 0.9,
  "epochs": 5000,
  "trainingData": "data/train.csv",
  "weightsFile": "weights/model.weights",
  "lossExport": "weights/loss.csv",
  "hiddenActivation": "relu",
  "outputActivation": "sigmoid",
  "lossFunction": "mse",
  "optimizer": "sgd_momentum",
  "validationSplit": 0.2,
  "patience": 50
}

Options

Activations: relu, leaky_relu, sigmoid, tanh, linear

Optimizers: sgd, sgd_momentum, adam

Loss functions: mse, cross_entropy

Early stopping: Set validationSplit > 0 and patience > 0. Training stops if validation loss does not improve for patience consecutive epochs.

Data format

CSV files in data/. Columns are split based on the topology: the first N columns are inputs (where N = first layer size), remaining columns are targets.

0,0,0
0,1,1
1,0,1
1,1,0

For XOR with topology [2, 8, 1]: 2 input columns, 1 target column.

Architecture

headers/
  Neuron.h        Activation functions and derivatives
  Layer.h         Neuron container with matrix conversion
  Matrix.h        Linear algebra operations, weight initialization
  NeuralNetwork.h Network core: forward pass, backprop, optimizers
  NetworkTrainer.h
  NetworkTester.h
  NetworkDemo.h
  utils/
    DataLoader.h  CSV loading, normalization, shuffling, train/val split

src/
  NeuralNetwork/
    constructor.cpp
    FeedForward.cpp
    BackPropagation.cpp
    ErrorFunctions.cpp
    train.cpp
    mutators.cpp
    weightsIO.cpp
    IOHelpers.cpp

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages