Loss functions
Loss
Bases: ABC
Template for loss functions.
Loss functions are conceptually equivalent to layers, in that they implement forward and backward passes. However, only one loss object per model is allowed, which is directly provided to the model, instead of to the list of layers.
Loss objects have no trainable parameters
Source code in src/nnfs/losses.py
MSE
Bases: Loss
Mean Squared Error loss function
\(\mathcal{L}_{\text{MSE}} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i)^2\)
Source code in src/nnfs/losses.py
forward(y_pred, y_true)
Computes the forward pass of the loss function.
The method stores y_pred and y_true as instance attributes for
later use by the backward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
ndarray
|
Predictions produced by the model. |
required |
y_true
|
ndarray
|
Ground-truth values for the prediction task. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The computed loss value. |
Source code in src/nnfs/losses.py
backward()
Computes the backward pass of the loss function.
Requires the cached values stored by the forward() method.
Returns:
| Type | Description |
|---|---|
ndarray
|
The computed dL_d_ypred gradients. |
Source code in src/nnfs/losses.py
BCE
Bases: Loss
Binary Cross Entropy loss function
\(\mathcal{L}_{\text{BCE}} = - \frac{1}{n} \sum_{i=1}^{n} \big[ y_i \log(\hat{y}_i) + (1 - y_i) \log(1 - \hat{y}_i) \big]\)
Source code in src/nnfs/losses.py
forward(y_pred, y_true)
Computes the forward pass of the loss function.
The method stores y_pred and y_true as instance attributes for
later use by the backward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
ndarray
|
Predictions produced by the model. |
required |
y_true
|
ndarray
|
Ground-truth values for the prediction task. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The computed loss value. |
Source code in src/nnfs/losses.py
backward()
Computes the backward pass of the loss function.
Requires the cached values stored by the forward() method.
Returns:
| Type | Description |
|---|---|
ndarray
|
The computed dL_d_ypred gradients. |
Source code in src/nnfs/losses.py
CCE
Bases: Loss
Categorical Cross Entropy loss function
\(\mathcal{L}_{\text{CCE}} = - \frac{1}{n} \sum_{i=1}^{n} \sum_{k=1}^{K} y_{ik} \log(\hat{y}_{ik})\)
Note: CCE is typically used to compute the loss between a softmax activation layer and one-hot encoded labels.
Source code in src/nnfs/losses.py
forward(y_pred, y_true)
Computes the forward pass of the loss function.
The method stores y_pred and y_true as instance attributes for
later use by the backward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
ndarray
|
Predictions produced by the model. |
required |
y_true
|
ndarray
|
Ground-truth values for the prediction task. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The computed loss value. |
Source code in src/nnfs/losses.py
backward()
Computes the backward pass of the loss function.
Requires the cached values stored by the forward() method.
Returns:
| Type | Description |
|---|---|
ndarray
|
The computed dL_d_ypred gradients. |