Simple Neural Network header written in C
Inspired by TheCodingTrain - Toy-Neural-Network-JS
It has one hidden layer and uses Backpropagation as learning method.
Just copy nn.h into your working directory
For gcc, you have to include these libraries in the linking process:
-lm
NN_LEARNING_RATE
- the learning rate for backpropagation. Default is0.1
. Use#define NN_LEARNING_RATE [double]
in your main file before #include "nn.h" to change this valueNN_ACTIVATION_FUNCTION
- the used activation function Default isNN_SIGMOID
. Use#define NN_ACTIVATION_FUNCTION [ActivationFunctions]
in your main file before #include "nn.h" to change the function
enum ActivationFunctions
- the avaiable activation functionsNN_SIGMOID
- the sigmoid functionNN_TANGENT
- the tangent function
struct NeuralNetwork
- the struct, that inhabits the information of the Neural Networkdouble learning_rate
- the learning rate (same asNN_LEARNING_RATE
)int activation_function
- the activation function (same asNN_ACTIVATION_FUNCTION
)int input_nodes
- the amount of input nodesint hidden_nodes
- the amount of hidden nodesint output_nodes
- the amount of output nodesMatrix* weights_ih
- the weights of the connections between the input nodes and the hidden nodesMatrix* weights_ho
- the weights of the connections between the hidden nodes and the output nodesMatrix* bias_ih
- the bias of the connections between the input nodes and the hidden nodesMatrix* bias_ho
- the bias of the connections between the hidden nodes and the output nodes
NeuralNetwork createNeuralNetwork(int input_nodes, int hidden_nodes, int output_nodes)
- a function, that creates a Neural Networkvoid predict(NeuralNetwork nn, double[] input, double[] output)
- a function, that calculates the output of the Neural Network. The length of the input array has to be the exact same as the amount of input nodes. The length of the output array has also to be exactlay the same as the amount of output nodes. The output values are always between 0 and 1void train(NeuralNetwork nn, double[] training_input, double[] training_output)
- a function, that trains the Neural Network one time with the given training input and the expected output. Input and output have to be the exact same length as the amount of their specific nodesvoid destroyNeuralNetwork(NeuralNetwork nn)
- a function, that destroys the Neural Networkvoid saveNeuralNetwork(NeuralNetwork nn, const char* path)
- a function, that saves the Neural Network in a file. For more details, see fileformatNeuralNetwork loadNeuralNetwork(const char* path)
- a function, that loads a Neural Network from a file. For more details, see fileformat
Here is a simple example on how to use the Neural Network:
#include "nn.h"
int main() {
// Specify the amount of Nodes for the Neural Network
int input_nodes = 2;
int hidden_nodes = 1;
int output_nodes = 1;
// Create the Neural Network
NeuralNetwork nn = createNeuralNetwork(input_nodes, hidden_nodes, output_nodes);
// Specify the training data
double training_input_1[2] = { 1, 1 };
double training_output_1[1] = { 0 };
double training_input_2[2] = { 0, 0 };
double training_output_2[1] = { 1 };
// Train the Neural Network 1000000 times with the training data
for( int i = 0; i < 1000000; i++ ) {
// Switch between the two training sets
if( i % 2 == 0 ) {
train(nn, training_input_1, training_output_1);
} else {
train(nn, training_input_2, training_output_2);
}
}
// Let the trained Neural Network predict the output of training set 1 (should be close to 0)
double output[nn.output_nodes];
predict(nn, training_input_1, output);
printf("Output: %f\n", output[0]);
// Save the trained Neural Network to nn.bin
saveNeuralNetwork(nn, "nn.bin");
// Destroy the Neural Network at the end
destroyNeuralNetwork(nn);
return 0;
}
This project is licensed under the terms of the MIT license, see LICENSE