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 (Optional[Dict[str, Union[torch.Tensor, jax.Array]]]) – Additional metrics can be passed as arrays/tensors or dictionaries of arrays/tensors.
n_obs_minibatch (Optional[int]) – 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#
Attributes#
extra_metrics_keys
kl_global
kl_global_sum
kl_local
kl_local_sum
n_obs_minibatch
reconstruction_loss
reconstruction_loss_sum
loss
extra_metrics
Methods#
dict_sum
from_tuple
replace
to_tuple