scvi.module.base.LossOutput#

class scvi.module.base.LossOutput(loss, reconstruction_loss=None, kl_local=None, kl_global=None, extra_metrics=<factory>, n_obs_minibatch=None)[source]#

Bases: Mapping

Loss signature for models.

This class provides an organized way to record the model loss, as well as the components of the ELBO. This may also be used in MLE, MAP, EM methods. The loss is used for backpropagation during inference. The other parameters are used for logging/early stopping during inference.

Parameters:
  • loss (Union[Dict[str, Union[torch.Tensor, jax.Array]], torch.Tensor, jax.Array]) – Tensor with loss for minibatch. Should be one dimensional with one value. Note that loss should be in an array/tensor and not a float.

  • reconstruction_loss (Optional[Union[Dict[str, Union[torch.Tensor, jax.Array]], torch.Tensor, jax.Array]]) – Reconstruction loss for each observation in the minibatch. If a tensor, converted to a dictionary with key “reconstruction_loss” and value as tensor.

  • kl_local (Optional[Union[Dict[str, Union[torch.Tensor, jax.Array]], torch.Tensor, jax.Array]]) – KL divergence associated with each observation in the minibatch. If a tensor, converted to a dictionary with key “kl_local” and value as tensor.

  • kl_global (Optional[Union[Dict[str, Union[torch.Tensor, jax.Array]], torch.Tensor, jax.Array]]) – Global KL divergence term. Should be one dimensional with one value. If a tensor, converted to a dictionary with key “kl_global” and value as tensor.

  • extra_metrics (dict[str, Union[torch.Tensor, jax.Array]] | None) – Additional metrics can be passed as arrays/tensors or dictionaries of arrays/tensors.

  • n_obs_minibatch (int | None) – Number of observations in the minibatch. If None, will be inferred from the shape of the reconstruction_loss tensor.

Examples

>>> loss_output = LossOutput(
...     loss=loss,
...     reconstruction_loss=reconstruction_loss,
...     kl_local=kl_local,
...     extra_metrics={"x": scalar_tensor_x, "y": scalar_tensor_y},
... )

Attributes table#

Methods table#

dict_sum(dictionary)

Sum over elements of a dictionary.

from_tuple()

replace(**kwargs)

to_tuple()

Attributes#

extra_metrics_keys

LossOutput.extra_metrics_keys[source]#

Keys for extra metrics.

kl_global

LossOutput.kl_global: Optional[Union[Dict[str, Union[Tensor, Array]], Tensor, Array]] = None#

kl_global_sum

LossOutput.kl_global_sum: Union[Tensor, Array] = None#

kl_local

LossOutput.kl_local: Optional[Union[Dict[str, Union[Tensor, Array]], Tensor, Array]] = None#

kl_local_sum

LossOutput.kl_local_sum: Union[Tensor, Array] = None#

n_obs_minibatch

LossOutput.n_obs_minibatch: int | None = None#

reconstruction_loss

LossOutput.reconstruction_loss: Optional[Union[Dict[str, Union[Tensor, Array]], Tensor, Array]] = None#

reconstruction_loss_sum

LossOutput.reconstruction_loss_sum: Union[Tensor, Array] = None#

loss

LossOutput.loss: Union[Dict[str, Union[Tensor, Array]], Tensor, Array]#

extra_metrics

LossOutput.extra_metrics: dict[str, Union[torch.Tensor, jax.Array]] | None#

Methods#

dict_sum

static LossOutput.dict_sum(dictionary)[source]#

Sum over elements of a dictionary.

Parameters:

dictionary (Union[dict[str, Union[torch.Tensor, jax.Array]], Tensor, Array]) –

from_tuple

LossOutput.from_tuple()[source]#

replace

LossOutput.replace(**kwargs)[source]#

to_tuple

LossOutput.to_tuple()[source]#