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__

backward

Compute the backward pass to calculate the gradient of the loss with respect to the predicted values.

calculate

Calculate the total loss based on model output and ground truth values.

calculate_accumulated

Calculate the accumulated loss over a training pass.

forward

Compute the forward pass of the Categorical Cross-Entropy loss.

regularization_loss

Calculate the regularization loss based on L1 and L2 regularization terms of the trainable layers' weights and biases.

reset

Reset the accumulated loss variables for a new training pass.

set_trainable_layers

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:

None

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:

ndarray | tuple[ndarray, float]

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.

Return type:

ndarray | tuple[ndarray, float]

Parameters:

include_regularization (bool, optional) – Whether to include regularization loss, by default False

Returns:

Accumulated loss value or tuple of data and regularization losses.

Return type:

float or tuple

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.

Return type:

float

Returns:

Regularization loss value.

Return type:

float

reset()#

Reset the accumulated loss variables for a new training pass.

Return type:

None

set_trainable_layers(trainable_layers)#

Set the list of trainable layers for loss calculations.

Return type:

None

Parameters:

trainable_layers (list[_Layer]) – List of trainable layers in the model.