DenseLayer
A fully connected (dense) neural network layer that performs linear transformation:z = xW + b.
Constructor
Number of input features.
Number of output features (neurons in this layer).
Random number generator for weight initialization. Uses
np.random if not provided.Data type for weights and biases.
Attributes
Weight matrix of shape
(n_in, n_out). Initialized using Xavier/Glorot uniform initialization.Bias vector of shape
(1, n_out). Initialized to zeros.Cached input from the last forward pass, used during backpropagation.
Cached pre-activation output from the last forward pass.
Methods
forward
Performs forward propagation through the layer.Input data of shape
(batch_size, n_in).Optional weight matrix to use instead of
self.weights. Useful for inference with different precision.Optional bias vector to use instead of
self.bias.ndarray - Pre-activation output of shape (batch_size, n_out), computed as x @ weights + bias.
backward
Computes gradients during backpropagation.Gradient of loss with respect to layer output, shape
(batch_size, n_out).L2 regularization strength. Adds regularization term to weight gradients.
tuple[ndarray, ndarray, ndarray]
grad_input: Gradient with respect to input, shape(batch_size, n_in)grad_w: Gradient with respect to weights, shape(n_in, n_out)grad_b: Gradient with respect to bias, shape(1, n_out)
Usage Example
custom_uniform
Xavier/Glorot uniform weight initialization function.Number of input features.
Number of output features.
Random number generator. Uses
np.random if not provided.Data type for the returned array.
ndarray - Weight matrix of shape (n_in, n_out) sampled from uniform distribution U(-limit, limit) where limit = sqrt(6 / (n_in + n_out)).