pyml.neural_network.loss.categorical_cross_entropy.CategoricalCrossentropy#
- class CategoricalCrossentropy[source]#
Bases:
_Loss
Categorical Cross-Entropy loss function for multi-class classification tasks.
This class defines the forward and backward pass computations for calculating the Categorical Cross-Entropy loss and its gradient with respect to the predicted values.
- Variables:
dinputs (numpy.ndarray) – Gradient of the loss with respect to the predicted values.
Methods
__init__
Compute the backward pass to calculate the gradient of the loss with respect to the predicted values.
Calculate the total loss based on model output and ground truth values.
Calculate the accumulated loss over a training pass.
Compute the forward pass of the Categorical Cross-Entropy loss.
Calculate the regularization loss based on L1 and L2 regularization terms of the trainable layers' weights and biases.
Reset the accumulated loss variables for a new training pass.
Set the list of trainable layers for loss calculations.
- backward(dvalues, y_true)[source]#
Compute the backward pass to calculate the gradient of the loss with respect to the predicted values.
- Return type:
- Parameters:
dvalues (numpy.ndarray) – Gradient of the loss with respect to the predicted values.
y_true (numpy.ndarray) – Ground truth values, containing integer class labels or one-hot encoded vectors.
- calculate(output, y, *, include_regularization=False)#
Calculate the total loss based on model output and ground truth values.
- Return type:
- Parameters:
output (numpy.ndarray) – Predicted output from the model.
y (numpy.ndarray) – Ground truth values.
include_regularization (bool, optional) – Whether to include regularization loss, by default False.
- Returns:
Total loss value if include_regularization is False, or a tuple of data and regularization loss values if include_regularization is True.
- Return type:
numpy.ndarray or tuple
- calculate_accumulated(*, include_regularization=False)#
Calculate the accumulated loss over a training pass.
- forward(y_pred, y_true)[source]#
Compute the forward pass of the Categorical Cross-Entropy loss.
- Return type:
ndarray
- Parameters:
y_pred (numpy.ndarray) – Predicted output from the model, representing class probabilities.
y_true (numpy.ndarray) – Ground truth values, containing integer class labels or one-hot encoded vectors.
- Returns:
Array of negative log-likelihoods for each sample.
- Return type:
numpy.ndarray
- regularization_loss()#
Calculate the regularization loss based on L1 and L2 regularization terms of the trainable layers’ weights and biases.