pyml.neural_network.loss.mean_squarred_error.MeanSquaredError#

class MeanSquaredError[source]#

Bases: _Loss

Mean Squared Error loss function.

This class defines the forward and backward pass computations for calculating the mean squared error 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 mean squared error 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.

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 mean squared error loss.

Return type:

ndarray

Parameters:
  • y_pred (numpy.ndarray) – _description_

  • y_true (numpy.ndarray) – _description_

Returns:

Array of sample-wise mean squared error losses.

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.