This repository is created for educational purposes only and is not ready for use in production.
The developed interface for working with neural networks is largely inspired by the Keras framework.
The central data structure of this library is the model. It defines the structure of the layers and the relationship between them.
At the moment, only a Sequential network model is implemented.
Now create an instance of the new network model:
#include "src/network.h"
Model model;
Then add two hidden layers with different activation functions and neurons number:
model.add(Layer(20, relu)); // 20 neurons and ReLU activation
model.add(Layer(10, sigmoid)); // 10 neurons and Sigmoid activation
Note that the model automatically selects the number of neurons on the input layer of the network.
Now the structure of the neural network is ready.
The next step is to train the neural network.
But before you train a model, you must set the training parameters.
This is done quite simply:
TrainParams params;
params.dataset = dataset; // train dataset
params.epochs = 5; // number of training epochs
params.error = 1e-10; // acceptable learning error
params.teach = 0.05; // learning rate
Under dataset should be understood as a set (vector) of data in the format: vector of input values of the network and a vector of output values of the network. More detailed: Dataset structure.
Now that we are ready to train the network, we can start the learning process:
model.fit(params);
During the training program will inform you:
Training started
---- Epoch: 1, average error: 5.74e-04
---- Epoch: 2, average error: 1.62e-05
---- Epoch: 3, average error: 8.75e-07
---- Epoch: 4, average error: 6.41e-08
---- Epoch: 5, average error: 2.32e-09
Training finished
After training the model, we can test the quality on the test data:
Dataset testing = makeTesting(3); // creates 3 test datasets
model.testing(testingSet); // starts testing
The output of the program:
Testing started
---- Testing set: 1, error: 1.12e-02
---- Testing set: 2, error: 1.65e-03
---- Testing set: 3, error: 4.36e-04
Testing finished. Average error: 4.43e-03
Now the model is trained and tested. Then it can be saved and used for other tasks.
You can see a complete example of a program that uses a neural network (autoencoder) to compress a PNG image into a vector and then restores the image from the compressed vector to the original image: main.cpp file.
Build from sources:
./compile.sh
Run application:
./build/nn example.png output.png