Loss

class lightning_pose.losses.losses.Loss[source]

Bases: object

Parent class for all losses.

Attributes Summary

weight

Scalar loss weight computed as 1 / (2 * exp(log_weight)).

Methods Summary

__call__(*args,Β **kwargs)

Execute the full loss pipeline and return a scalar loss plus logging dicts.

compute_loss(**kwargs)

Compute the element-wise loss between targets and predictions.

log_loss(loss,Β stage)

Build a list of logging dicts for the scalar loss and its weight.

rectify_epsilon(loss)

Zero out loss values below the epsilon threshold (epsilon-insensitive loss).

reduce_loss(loss[,Β method])

Reduce an element-wise loss tensor to a scalar.

remove_nans(**kwargs)

Remove NaN entries from inputs before computing the loss.

Attributes Documentation

weight

Scalar loss weight computed as 1 / (2 * exp(log_weight)).

Returns:

Positive scalar weight tensor.

Methods Documentation

__call__(*args: Any, **kwargs: Any) tuple[Float[Tensor, ''], list[dict]][source]

Execute the full loss pipeline and return a scalar loss plus logging dicts.

The standard pipeline is: remove_nans β†’ compute_loss β†’ rectify_epsilon β†’ reduce_loss β†’ log_loss. Subclasses must override this method to supply the correct arguments to each step.

Raises:

NotImplementedError – always, unless overridden by a subclass.

compute_loss(**kwargs: Any) Tensor[source]

Compute the element-wise loss between targets and predictions.

Subclasses must override this method.

Raises:

NotImplementedError – always, unless overridden by a subclass.

log_loss(loss: Tensor, stage: Literal['train', 'val', 'test'] | None) list[dict][source]

Build a list of logging dicts for the scalar loss and its weight.

Parameters:
  • loss – scalar loss value to log.

  • stage – training stage prefix for the log key, or None to skip stage prefixing.

Returns:

List of dicts with "name" and "value" keys, one for the loss and one for the weight.

rectify_epsilon(loss: Tensor) Tensor[source]

Zero out loss values below the epsilon threshold (epsilon-insensitive loss).

Parameters:

loss – element-wise loss tensor.

Returns:

Loss tensor with values below self.epsilon set to zero via ReLU.

reduce_loss(loss: Tensor, method: str = 'mean') Float[Tensor, ''][source]

Reduce an element-wise loss tensor to a scalar.

Parameters:
  • loss – element-wise loss tensor.

  • method – reduction method; currently "mean" or "sum".

Returns:

Scalar loss tensor.

remove_nans(**kwargs: Any) Any[source]

Remove NaN entries from inputs before computing the loss.

Subclasses must override this method to implement the appropriate NaN-masking strategy.

Raises:

NotImplementedError – always, unless overridden by a subclass.

__init__(data_module: BaseDataModule | UnlabeledDataModule | None = None, epsilon: float | list[float] = 0.0, log_weight: float = 0.0, **kwargs: Any) None[source]
Parameters:
  • data_module – give losses access to data for computing data-specific loss params

  • epsilon – loss values below epsilon will be zeroed out

  • log_weight – final weight in front of the loss term in the objective function is computed as 1.0 / (2.0 * exp(log_weight)).

__new__(**kwargs)