The purpose of this project is to get a better understanding on how artificial neural networks operate since programs like e.g. stable diffusion or starnet++ sparked my interest in the topic. In addition to this I see it as kind of a challenge to implement it by myself after watching tons of videos and tutorials online and reading equally as many papers about machine learning. So I am not really trying to create the best performing or even the most usable AI framework, though I believe it would be pretty fun experimenting with some usecases if I get it to work properly. That being said, in the end it is nothing more than a recreational programming exercise for me and maybe for others who want to play around with this thing. I used C for this project, because it seemed fun to me, though I wouldn’t be disinclined porting it to Common-LISP if I get it to work here properly.
To build this project there aren’t any dependencies other than libc …duh… A Makefile is included. So just type the following:
$ git clone https://github.com/dw0xaa55/anni
$ cd anni/
### To build:
$ make
### To run:
$ make runI implemented this with the STB-style header-only library philosophy in mind because I like the minimalist and easy to include approach, so everything needed is in one header file including the code for all functions. The anni.c file can be seen as an example So to include anni.h with access to all implementations use the following:
#define ANNI_IMPLEMENTATION
#include "/path/to/anni.h"As for the setup of the neural network you need to specify the topology with a single dimension array where the first element represents the input layer, the following elements the hidden layers and finally the last element stands for the output layer.
The training data is contained by two two dimensional arrays. The first one holds the inputs, while the second one holds the expected outputs. Each line of the respective arrays represents one training sample.
#define SAMPLE_AMOUNT 4
double input[SAMPLE_AMOUNT][2] = {{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}};
double output[SAMPLE_AMOUNT][1] = {{0.0},
{1.0},
{1.0},
{0.0}};Here everything is stored concerning the actual neural network and its architecture derived from the topology to all the layers which are stored in dynamically allocated arrays. Although it seems to be good practice to free the neural net after its usage has expired, I guess it’s not really necessary since it should be in use until the program exits. Anyhow, I added a cleanup function, if you want it.
typedef struct {
// This structure represents the Neural Network
size_t num_layers; // number of layers (input + hidden + output)
size_t *topology; // array that specifies the number of neurons per layer
double **neurons; // array of neuron activations per layer
double **biases; // biases for neurons per layer (except input)
double ***weights; // weight matrices: weights[l][i][j] connects j-th neuron in (l-1)th layer to i-th neuron in layer l
} NeuralNetwork;Here is a list of all the functions used with a short discription of what they are doing:
#define RANDOM_WEIGHT() ((double)rand() / (double)RAND_MAX * 2.0 - 1.0) // macro for initializing weights randomly between -1.0 and 1.0.
double sigmoid(double x); // activation function for feeding neurons forward in the network
double sigmoidDerivative(double output); // derivative of activation function used for training via back propagation
NeuralNetwork* createNeuralNetwork(const size_t *topology, // instantiates the NeuralNetwork struct
size_t num_layers);
void feedForward(NeuralNetwork *nn, // uses the existing network to calculate the output
double *input);
void printOutput(NeuralNetwork *nn); // prints the output neurons
double meanSquareErrorDerivative(double predicted, // helper function to calculate the mean square error later in back propagation
double target,
size_t n);
void backPropagation(NeuralNetwork *nn, // back propagation utilizing gradient descent
double *output,
double learning_rate);
void train(NeuralNetwork *nn, // trains the network for set number of epochs in a loop of feed forward and
double *input, // back propagation. This functions needs to be looped through the training set
double *output,
double learning_rate);
void freeNeuralNetwork(NeuralNetwork *nn); // frees allocated memory for neurons, biases and weights as well as the topology array
void printNetwork(NeuralNetwork *nn); // prints the neurons, biases and weights representing the Neural Net for debug purposes
void printTraining(NeuralNetwork *nn, // prints the NeuralNet and the current epoch
size_t epoch); I am a hobbyist programmer who likes playing around with programming languages and other nerdy stuff in my free time, so do not expect either elegant or efficient code. Also there may be bugs and maybe some swear words here and there (due to their unique semantic appearance in contrast to programming language terminology I occasionally use them to jump between code passages via search function, if the code blocks are far apart from each other; that said, I try to eliminate them before uploads :>). ~Happy Hacking