NeuralNetworkModel
The base neural network model class that supports flexible architecture configuration, multiple precision modes, and regularization techniques.Constructor
List of layer sizes including input and output dimensions. Must have at least 2 elements.
Example:
[784, 128, 64, 10] for input size 784, two hidden layers (128 and 64 units), and output size 10.List of activation function names for each layer. Length must equal
len(layer_sizes) - 1.
Supported values: "sigmoid", "relu", "softmax".L2 regularization strength. Higher values increase regularization penalty.
Dropout probability during training. Must be between 0 and 1.
Configuration object for training and inference precision. Uses
DEFAULT_CONFIG if not provided.Methods
forward
Performs forward propagation through the network.Input data of shape
(batch_size, n_features).Whether to apply dropout during forward pass.
Precision mode for inference:
"float32", "float16", or "int8". Uses infer_precision from config if not specified.ndarray - Output predictions of shape (batch_size, n_classes).
fit
Trains the model using mini-batch gradient descent.Training data of shape
(n_samples, n_features).Training labels. Can be class indices or one-hot encoded.
Number of training epochs.
Learning rate for gradient descent.
Mini-batch size for training.
Path to save model weights after training.
Whether to shuffle training data each epoch.
Random seed for reproducibility.
Validation data for early stopping.
Validation labels for early stopping.
Number of epochs with no improvement before early stopping.
Minimum change in validation loss to qualify as improvement.
Whether to restore best weights after early stopping.
dict - Training history with keys "loss", "accuracy", and "val_loss".
predict
Generates class predictions for input data.Input data of shape
(batch_size, n_features).Precision mode:
"float32", "float16", or "int8".ndarray - Predicted class indices of shape (batch_size,).
save_weights
Saves model weights to a file.Path to save weights file.
load_weights
Loads model weights from a file.Path to weights file.
backprop
Performs backpropagation and updates weights.Input batch of shape
(batch_size, n_features).Target labels (one-hot encoded).
Learning rate.
gradient_check
Verifies gradient computation using numerical approximation.Input data for gradient checking.
Target labels.
Small value for numerical gradient approximation.
Number of random parameters to check.
dict - Maximum gradient errors for each parameter.
Properties
List of weight matrices for all layers.
List of bias vectors for all layers.
Usage Example
NeuralNetwork
A convenience class that inherits fromNeuralNetworkModel with identical functionality. Defined in student.py.
Constructor
NeuralNetworkModel.