An open API service indexing awesome lists of open source software.

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).

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.

![image](./res/neuralnet.png)

##### Output

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