#Feedforward neural network implemented for my AI framework library.

3 messages · Page 1 of 1 (latest)

lone orbit
#

https://github.com/matth3wmajf/alpha
I thought the feedforward neural network would be a good start, so I got that working, would like to know what others think about the code (if it can be improved, etc.).

#

I was able to train it on the XOR operation:

#

And this is the sample code for it:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <math.h>

#include <alpha/feedforward.h>

int main(int argc, const char *argv[])
{
    feedforward_t l_feedforward;

    feedforward_create(&l_feedforward);

    feedforward_resize(&l_feedforward, 2, (uintmax_t[]){4, 2}, 2, 1);

    feedforward_random(&l_feedforward);

    double_t l_learning_rate = 0.25;

    double_t l_input_buffer[4][2] = {
        {0.0, 0.0},
        {0.0, 1.0},
        {1.0, 0.0},
        {1.0, 1.0}
    };

    double_t l_target_buffer[4] = {0.0, 1.0, 1.0, 0.0};

    for(uintmax_t l_epoch = 0; l_epoch < 1000000; l_epoch++)
    {
        for(uintmax_t l_i = 0; l_i < 4; l_i++)
        {
            feedforward_backward(&l_feedforward, l_input_buffer[l_i], 2, &l_target_buffer[l_i], 1, l_learning_rate);
        }
    }

    double_t l_output;
    for(uintmax_t l_i = 0; l_i < 4; l_i++)
    {
        feedforward_forward(&l_feedforward, l_input_buffer[l_i], 2, &l_output, 1);
        printf("Input: %f, %f -> Output: %f\n", l_input_buffer[l_i][0], l_input_buffer[l_i][1], l_output);
    }

    feedforward_delete(&l_feedforward);

    return 0;
}