Skip to content

lhldev/cneuron

Repository files navigation

C Neural Network 🧠

Prerequisites

Ensure that a BLAS (Basic Linear Algebra Subprograms) library is installed. This project relies on the C interface to BLAS (cblas.h), which is provided by most major BLAS distributions, including:

  • Intel MKL
  • OpenBLAS

For optimal performance, compile the project using the following cmake command:

cmake -S . -B build -DBLA_VENDOR=Intel10_64lp_seq -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTING=OFF

Minimal code example (training on xor)

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

#include "cneuron/cneuron.h"

float sigmoid(float val, bool is_deravative) {
    float result = 1.0f / (1.0f + expf(-val));
    if (is_deravative == 1) {
        return result * (1.0f - result);
    }
    return result;
}

int main(void) {
    // Create XOR dataset
    size_t dataset_length = 4;
    size_t inputs_length = 2;
    dataset *test_dataset = alloc_dataset(dataset_length, inputs_length);

    // XOR gate
    test_dataset->all_inputs[0] = 1.0f;
    test_dataset->all_inputs[1] = 1.0f;
    test_dataset->expected_indices[0] = 0;

    test_dataset->all_inputs[2] = 0.0f;
    test_dataset->all_inputs[3] = 0.0f;
    test_dataset->expected_indices[1] = 0;

    test_dataset->all_inputs[4] = 0.0f;
    test_dataset->all_inputs[5] = 1.0f;
    test_dataset->expected_indices[2] = 1;

    test_dataset->all_inputs[6] = 1.0f;
    test_dataset->all_inputs[7] = 0.0f;
    test_dataset->expected_indices[3] = 1;

    // Create network
    size_t layer_length = 2;
    size_t *layer_lengths = (size_t *)malloc(sizeof(size_t) * layer_length);
    layer_lengths[0] = 4;
    layer_lengths[1] = 2;
    neural_network *nn = get_neural_network(layer_length, layer_lengths, test_dataset->inputs_length, &sigmoid);

    for (size_t i = 0; i < 500000; i++) {
        for (size_t j = 0; j < test_dataset->length; j++) {
            size_t randnum = randnum_u32(test_dataset->length, 0);
            stochastic_gd(nn, 0.001f, &test_dataset->all_inputs[randnum * inputs_length], test_dataset->expected_indices[randnum]);
        }
        if (i % 100000 == 0) {
            printf("Stochastic Multi layer learn cost: %f\n", cost(nn, test_dataset, test_dataset->length));
        }
    }

    free(nn);
    free(layer_lengths);
    free(test_dataset);
    return 0;
}

Benchmark - Highest score recorded for MNIST dataset

  • Intel Core i5 9th Gen: ~150,000 Data/s
  • Intel Core Ultra 5: ~250,000 Data/s

This project utilizes the MNIST dataset

Information regarding its license (Creative Commons Attribution-ShareAlike 3.0) and attribution can be found in the data/mnist/MNIST_Copyright.md file

About

Lightweight neural network library in C.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors