Losses#

Loss functions (MSE, CrossEntropy, etc.)

Submodule Overview#

nabla.nn.losses.mean_squared_error(predictions, targets)[source]#

Compute mean squared error loss.

Parameters:
  • predictions (Array) – Predicted values of shape (batch_size, …)

  • targets (Array) – Target values of shape (batch_size, …)

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.mean_absolute_error(predictions, targets)[source]#

Compute mean absolute error loss.

Parameters:
  • predictions (Array) – Predicted values of shape (batch_size, …)

  • targets (Array) – Target values of shape (batch_size, …)

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.huber_loss(predictions, targets, delta=1.0)[source]#

Compute Huber loss (smooth L1 loss).

Parameters:
  • predictions (Array) – Predicted values of shape (batch_size, …)

  • targets (Array) – Target values of shape (batch_size, …)

  • delta (float) – Threshold for switching between L1 and L2 loss

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.cross_entropy_loss(logits, targets, axis=-1)[source]#

Compute cross-entropy loss between logits and targets.

Parameters:
  • logits (Array) – Raw model outputs (before softmax) [batch_size, num_classes]

  • targets (Array) – One-hot encoded targets [batch_size, num_classes]

  • axis (int) – Axis along which to compute softmax

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.sparse_cross_entropy_loss(logits, targets, axis=-1)[source]#

Compute cross-entropy loss with integer targets.

Parameters:
  • logits (Array) – Raw model outputs [batch_size, num_classes]

  • targets (Array) – Integer class indices [batch_size]

  • axis (int) – Axis along which to compute softmax

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.binary_cross_entropy_loss(predictions, targets, eps=1e-07)[source]#

Compute binary cross-entropy loss.

Parameters:
  • predictions (Array) – Model predictions (after sigmoid) [batch_size]

  • targets (Array) – Binary targets (0 or 1) [batch_size]

  • eps (float) – Small constant for numerical stability

Returns:

Scalar loss value

Return type:

Array

nabla.nn.losses.softmax_cross_entropy_loss(logits, targets, axis=-1)[source]#

Compute softmax cross-entropy loss (numerically stable).

This is equivalent to cross_entropy_loss but more numerically stable by combining softmax and cross-entropy computations.

Parameters:
  • logits (Array) – Raw model outputs [batch_size, num_classes]

  • targets (Array) – One-hot encoded targets [batch_size, num_classes]

  • axis (int) – Axis along which to compute softmax

Returns:

Scalar loss value

Return type:

Array