(LLVM on macOS, GCC on Linux, MSVC on Windows)
This is a very basic neural network implementation written with simple C.
I really just created this to learn more about the internals of neural networks. I'm making it public in case anyone else finds it helpful or useful.
- Allows abitrary number of layers, and nodes in each layer (feed-forward only)
- Processes multiple training examples at a time
- Good unit test coverage
- Load and save weight values to file
- Include bias nodes
- Paralellisation across CPU and/or GPU to increase speed
- Choice of activation function at different layers
-
Create/allocate a new neural network with any number of layers, and any number of nodes in each layer, e.g.
nn_Network *network = nn_Network_alloc("2, 3, 1");
-
Randomise weights within the network
nn_Network_randomiseWeightsBetweenMinAndMax(network, -3.0, 3.0);
-
Set up training input and output data, e.g.
nn_Matrix *trainingDataInputs = nn_Matrix_allocWithValues(4, 2, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0 ); nn_Matrix *trainingDataOutputs = nn_Matrix_allocWithValues(4, 1, 1.0, 0.0, 0.0, 1.0 );
-
Complete a training pass (forward, then backward pass with weight updates),
double error = nn_Network_train(network, trainingDataInputs, trainingDataOutputs, 0.3);
Complete this step until
error
is at an acceptable level. -
Clean up memory,
nn_Matrix_free(trainingDataInputs); nn_Matrix_free(trainingDataOutputs); nn_Network_free(network);
-
Link in the C
math
library when building, e.g.cc -o example example.c nn_Network.c nn_Matrix.c -lm