Layers
BaseLayer
Bases: ABC
Template for neural network layers.
Layers have to define a trainable property that returns a
list of (name_param, param, grad) tuples.
Layers also have to define a name attribute which specifies
the base name of the layer. The name can be arbitrary but it has
to be unique for each of the layer types.
Source code in src/nnfs/layers.py
name
property
Returns the layer's name.
It is used by the model to summarize layer architecture, as well as to cache layer-specific gradients (for example, to implement momentum).
Returns:
| Type | Description |
|---|---|
str
|
A layer_id of the form layer.name_layer.index. |
Dense
Bases: BaseLayer
A fully connected neural network layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Dimensionality of the input to the layer. |
required |
output_size
|
int
|
Dimensionality of the output of the layer. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
W |
ndarray
|
weight parameters. |
b |
ndarray
|
bias parameters. |
dW |
ndarray
|
gradient of loss w.r.t weight parameters. |
db |
ndarray
|
gradient of loss w.r.t bias parameters. |
X_input |
ndarray
|
cached input to the layer. |
layer_name |
str
|
Short name for the layer type. |
index |
int
|
Position of the layer within the full model (initializes at 0). |
Source code in src/nnfs/layers.py
trainable
property
Returns the layer's trainable parameters and their gradients.
Each element is a tuple (name, param, grad) representing a parameter name, value, and corresponding gradient. These are used by the optimizer during training.
Returns:
| Type | Description |
|---|---|
list[tuple[str, ndarray, ndarray]]
|
A list of tuples containing |
forward(X_input)
Computes the forward pass for the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_input
|
ndarray
|
Input data to be transformed by the layer. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Output of the layer. |
Source code in src/nnfs/layers.py
backward(grad_next)
Computes the backward pass for the layer.
This function updates the dW and db attributes of the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad_next
|
ndarray
|
Gradients fed back from the next layer during backpropagation. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Gradient of the loss w.r.t the input to the layer. |