aboutsummaryrefslogtreecommitdiff
path: root/src/nn.h
blob: 9005364d3caaf2841bdad667d256cbc3700b4e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef __NN__
#define __NN__

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <openblas/cblas.h>

struct Cost {
    double (*func)(double labels[], double net_out[], size_t shape);
    double (*dfunc_out)(double labels, double net_out);
};

struct Activation {
    double (*func)(double);
    double (*dfunc)(double);
};

typedef struct Layer {
    double *weights, *bias;
    struct Activation activation;
    size_t neurons, input_nodes;
} Layer;

void nn_network_init_weights(Layer *network, size_t nmemb, size_t input_cols);
void nn_network_free_weights(Layer *network, size_t nmemb);

void nn_network_predict(
        double *out, size_t out_shape[2],
        double *input, size_t input_shape[2],
        Layer network[], size_t network_size);

void nn_network_train(
        Layer network[], size_t network_size,
        double *input, size_t input_shape[2],
        double *labels, size_t labels_shape[2],
        struct Cost cost, size_t epochs, double alpha);

void nn_layer_map_activation(
        double (*activation)(double),
        double *aout, size_t aout_shape[2],
        double *zout, size_t zout_shape[2]);


double sigmoid(double x);
double relu(double x);
double identity(double x);


void nn_forward(
        double **aout, double **zout,
        double *input, size_t input_shape[2],
        Layer network[], size_t network_size);

void nn_backward(
        double **weights, double **bias,
        double **zout, double **outs,
        double *input, size_t input_shape[2],
        double *labels, size_t labels_shape[2],
        Layer network[], size_t network_size,
        double (cost_derivative)(double, double),
        double alpha);

void nn_layer_forward(
        Layer layer,
        double *out, size_t out_shape[2],
        double *input, size_t input_shape[2]);

void nn_layer_backward(
        double *weights, double *bias, size_t weigths_shape[2],
        double *delta, double *out_prev,
        Layer layer, double alpha);

void nn_layer_out_delta(
        double *delta, double *dcost_out, double *zout, size_t cols,
        double (*activation_derivative)(double));//TODO

void nn_layer_hidden_delta(
        double *delta, double *delta_next, double *zout,
        double *weights_next, size_t weights_next_shape[2],
        double (*activation_derivative)(double));//TODO
#endif
Feel free to download, copy and edit any repo