GraceCU provides a lightweight C/C++ neural network library with a focus on clarity and educational value.
This document describes the public API exposed in NeuralNetwork.h.
Initializes a NNConfig struct with invalid values, ensuring unset parameters can later be detected and validated.
- config → pointer to the config struct to initialize
Postcondition:
- The config is safe to check for missing/invalid values before use.
Holds all user-defined hyperparameters:
typedef struct NNConfig {
float learningRate;
NNInitializationFunction weightInitializerF;
NNActivationFunction hiddenLayersAF;
NNActivationFunction outputLayerAF;
NNLossFunction lossFunction;
} NNConfig;NNStatus createNeuralNetwork(const unsigned int *architecture, const unsigned int layerCount, NNConfig config, NeuralNetwork **nnP)
Creates, allocates, and initializes a new neural network.
- architecture → array of neuron counts per layer (e.g.
{784, 128, 10}) - layerCount → length of the architecture array
- config → hyperparameters (learning rate, activation functions, weight init, etc.)
- nnP → output pointer; on success points to a valid
NeuralNetworkobject
Preconditions:
configmust be initialized viacreateNNConfig()layerCountmust match the length ofarchitecture- User must initialize RNG seed (
srand((unsigned int)time(NULL));)
Postconditions:
*nnPpoints to a heap-allocated network- Weights and biases initialized according to
config
Frees all memory associated with a neural network.
- nn → pointer to a previously created network
Precondition:
nnmust have been created viacreateNeuralNetwork()
Postcondition:
- All memory allocated for the network is released
Trains the neural network using backpropagation and gradient descent.
- trainingData → matrix with input columns + last
nOutputscolumns as expected outputs - batchSize → training mode depends on this value:
1→ stochastic gradient descent1 < batchSize < trainingSetSize→ mini-batch trainingbatchSize == trainingSetSize→ full batch training
Postcondition:
- Weights and biases updated according to gradients
Computes classification accuracy of the network on a dataset.
- dataset → matrix formatted like training data
- accuracy → pointer to float where result (0–100%) is stored
Computes average loss of the network on a dataset.
- trainingData → formatted dataset
- averageLoss → pointer to float where average loss is stored
Runs a forward pass on the input data.
- input → matrix containing input samples
- output → pointer to matrix where activations of the last layer will be stored
Saves the entire network state (weights, biases, gradients, config, etc.) to a file.
- fpOut → file pointer (must already be opened for writing)
- nn → pointer to the network
Loads a previously saved network state from a file.
- fpIn → file pointer (must already be opened for reading)
- nnP → output pointer where the loaded network will be stored
Converts an error/status code into a human-readable string.
- code → status code returned by API functions
- returns → string message (e.g.
"NN_ERROR_INVALID_ARGUMENT")