aboutsummaryrefslogtreecommitdiff
path: root/src/lib/include/neuralnet/neuralnet.h
blob: f122c2a46e3887a924caa7f1a284a32e7074b6a3 (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
#pragma once

#include <neuralnet/matrix.h>
#include <neuralnet/types.h>

typedef struct nnNeuralNetwork nnNeuralNetwork;
typedef struct nnQueryObject   nnQueryObject;

/// Linear layer parameters.
///
/// Either one of the following must be set:
///   a) Training:  input and output sizes.
///   b) Inference: weights + biases.
typedef struct nnLinearParams {
  int      input_size;
  int      output_size;
  nnMatrix weights;
  nnMatrix biases;
} nnLinearParams;

/// Layer type.
typedef enum nnLayerType {
  nnLinear,
  nnSigmoid,
  nnRelu,
} nnLayerType;

/// Neural network layer.
typedef struct nnLayer {
  nnLayerType type;
  union {
    nnLinearParams linear;
  };
} nnLayer;

/// Create a network.
nnNeuralNetwork* nnMakeNet(
    const nnLayer* layers, int num_layers, int input_size);

/// Delete the network and free its internal memory.
void nnDeleteNet(nnNeuralNetwork**);

/// Query the network.
///
/// |input| is a matrix of inputs, one row per input and as many columns as the
/// input's dimension.
///
/// The query object's output matrix (see nnQueryOutputs()) is a matrix of
/// outputs, one row per output and as many columns as the output's dimension.
void nnQuery(const nnNeuralNetwork*, nnQueryObject*, const nnMatrix* input);

/// Query the network, array version.
void nnQueryArray(
    const nnNeuralNetwork*, nnQueryObject*, const R* input, R* output);

/// Create a query object.
///
/// The query object holds all the internal memory required to query a network
/// with batches of the given size. Memory is allocated up front so that network
/// queries can run without additional memory allocation.
nnQueryObject* nnMakeQueryObject(const nnNeuralNetwork*, int batch_size);

/// Delete the query object and free its internal memory.
void nnDeleteQueryObject(nnQueryObject**);

/// Return the outputs of the query.
const nnMatrix* nnNetOutputs(const nnQueryObject*);

/// Return the network's input size.
int nnNetInputSize(const nnNeuralNetwork*);

/// Return the network's output size.
int nnNetOutputSize(const nnNeuralNetwork*);

/// Return the layer's input size.
int nnLayerInputSize(const nnNeuralNetwork*, int layer);

/// Return the layer's output size.
int nnLayerOutputSize(const nnNeuralNetwork*, int layer);