From 653e98e029a0d0f110b0ac599e50406060bb0f87 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 16 Dec 2023 10:21:16 -0800 Subject: Decouple activations from linear layer. --- src/lib/include/neuralnet/matrix.h | 3 +++ src/lib/include/neuralnet/neuralnet.h | 51 ++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 19 deletions(-) (limited to 'src/lib/include') diff --git a/src/lib/include/neuralnet/matrix.h b/src/lib/include/neuralnet/matrix.h index b7281bf..f80b985 100644 --- a/src/lib/include/neuralnet/matrix.h +++ b/src/lib/include/neuralnet/matrix.h @@ -17,6 +17,9 @@ nnMatrix nnMatrixMake(int rows, int cols); /// Delete a matrix and free its internal memory. void nnMatrixDel(nnMatrix*); +/// Construct a matrix from an array of values. +nnMatrix nnMatrixFromArray(int rows, int cols, const R values[]); + /// Move a matrix. /// /// |in| is an empty matrix after the move. diff --git a/src/lib/include/neuralnet/neuralnet.h b/src/lib/include/neuralnet/neuralnet.h index 05c9406..f122c2a 100644 --- a/src/lib/include/neuralnet/neuralnet.h +++ b/src/lib/include/neuralnet/neuralnet.h @@ -1,32 +1,45 @@ #pragma once +#include #include -typedef struct nnMatrix nnMatrix; - typedef struct nnNeuralNetwork nnNeuralNetwork; typedef struct nnQueryObject nnQueryObject; -/// Neuron activation. -typedef enum nnActivation { - nnIdentity, +/// 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, -} nnActivation; +} nnLayerType; + +/// Neural network layer. +typedef struct nnLayer { + nnLayerType type; + union { + nnLinearParams linear; + }; +} nnLayer; /// Create a network. nnNeuralNetwork* nnMakeNet( - int num_layers, const int* layer_sizes, const nnActivation* activations); + const nnLayer* layers, int num_layers, int input_size); /// Delete the network and free its internal memory. void nnDeleteNet(nnNeuralNetwork**); -/// Set the network's weights. -void nnSetWeights(nnNeuralNetwork*, const R* weights); - -/// Set the network's biases. -void nnSetBiases(nnNeuralNetwork*, const R* biases); - /// Query the network. /// /// |input| is a matrix of inputs, one row per input and as many columns as the @@ -42,10 +55,10 @@ void nnQueryArray( /// Create a query object. /// -/// The query object holds all the internal memory required to query a network. -/// Query objects allocate all memory up front so that network queries can run -/// without additional memory allocation. -nnQueryObject* nnMakeQueryObject(const nnNeuralNetwork*, int num_inputs); +/// 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**); @@ -60,7 +73,7 @@ int nnNetInputSize(const nnNeuralNetwork*); int nnNetOutputSize(const nnNeuralNetwork*); /// Return the layer's input size. -int nnLayerInputSize(const nnMatrix* weights); +int nnLayerInputSize(const nnNeuralNetwork*, int layer); /// Return the layer's output size. -int nnLayerOutputSize(const nnMatrix* weights); +int nnLayerOutputSize(const nnNeuralNetwork*, int layer); -- cgit v1.2.3