aboutsummaryrefslogtreecommitdiff
path: root/src/lib/src/neuralnet_impl.h
blob: 935c5ea262fe2f9bebf19e49e0a7f8b12b543169 (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
#pragma once

#include <neuralnet/matrix.h>

#include <stdbool.h>

/// Linear layer parameters.
typedef struct nnLinearImpl {
  nnMatrix weights;
  nnMatrix biases;
  bool     owned; /// Whether the library owns the weights and biases.
} nnLinearImpl;

/// Neural network layer.
typedef struct nnLayerImpl {
  nnLayerType type;
  int         input_size;
  int         output_size;
  union {
    nnLinearImpl linear;
  };
} nnLayerImpl;

/// Neural network object.
typedef struct nnNeuralNetwork {
  int          num_layers; // Number of non-input layers (hidden + output).
  nnLayerImpl* layers;     // One per non-input layer.
} nnNeuralNetwork;

/// A query object that holds all the memory necessary to query a network.
///
/// |layer_outputs| is an array of matrices of intermediate layer outputs. There
/// is one matrix per intermediate layer. Each matrix holds the layer's output,
/// with one row per input, and as many columns as the layer's output size (the
/// output vector is transposed.)
///
/// |network_outputs| points to the last output matrix in |layer_outputs| for
/// convenience.
typedef struct nnQueryObject {
  int       num_layers;      // Same as nnNeuralNetwork::num_layers.
  nnMatrix* layer_outputs;   // Output matrices, one output per layer.
  nnMatrix* network_outputs; // Points to the last output matrix.
} nnTrainingQueryObject;