https://github.com/raphaelsenn/neural-network-autograd-cpp
A simple feed forward neural network (with autograd capabilities) in C++ (no external libarys).
https://github.com/raphaelsenn/neural-network-autograd-cpp
ai algorithms artificial-intelligence artificial-neural-networks backpropagation cplusplus deep-learning machine-learning machine-learning-algorithms neural-network
Last synced: 4 months ago
JSON representation
A simple feed forward neural network (with autograd capabilities) in C++ (no external libarys).
- Host: GitHub
- URL: https://github.com/raphaelsenn/neural-network-autograd-cpp
- Owner: raphaelsenn
- License: mit
- Created: 2024-07-06T06:34:31.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-23T09:24:07.000Z (almost 2 years ago)
- Last Synced: 2025-07-13T00:42:20.750Z (11 months ago)
- Topics: ai, algorithms, artificial-intelligence, artificial-neural-networks, backpropagation, cplusplus, deep-learning, machine-learning, machine-learning-algorithms, neural-network
- Language: C++
- Homepage:
- Size: 3.97 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# neural-network-autograd-cpp
A simple neural network (with autograd!) in C++ (no external libarys like Eigen).
## Usage
### Creating a NeuralNetwork
#### OR-Gate problem
```cpp
#include "./NeuralNetwork.h"
#include
int main() {
{
// This neural network learns how to solve the OR-Gate problem.
// Training data.
Matrix X_train =
std::vector>({{0, 0}, {0, 1}, {1, 0}, {1, 1}});
Matrix y_train_or =
std::vector>({{0}, {1}, {1}, {1}});
// Create the neural net.
// w1= 2 x 1, activation sigmoid.
NeuralNetwork orGate(std::vector({2, 1}),
std::vector({Activation::sigmoid}),
0.1f, InitState::RANDOM);
orGate.train(X_train, y_train_or, 0.1f, 10000, false);
orGate.act(X_train).print();
}
}
```
##### Output
```shell
matrix([[0.0205522],
[0.989617],
[0.989618],
[0.999998]])
```
#### XOR-Gate problem
```cpp
#include "./NeuralNetwork.h"
#include
int main() {
{
// This neural network learns how to solve the XOR-Gate problem.
// Training data.
Matrix X_train =
std::vector>({{0, 0}, {0, 1}, {1, 0}, {1, 1}});
Matrix y_train =
std::vector>({{0}, {1}, {1}, {0}});
// Create the neural net.
// w1 = 2 x 4 with relu
// w2 = 4 x 1 with sigmoid
NeuralNetwork xorGate(
std::vector({2, 4, 1}),
std::vector({Activation::relu, Activation::sigmoid}),
0.88f, InitState::RANDOM);
xorGate.train(X_train, y_train_xor, 0.1f, 10000, false);
xorGate.act(X_train).print();
}
}
```
###### The neural network would look like this.

##### Output
```shell
matrix([[0.0145205],
[0.979574],
[0.979554],
[0.0177436]])
```