JVAETrainer

class scvi.inference.JVAETrainer(model, discriminator, gene_dataset_list, train_size=0.9, use_cuda=True, kappa=1.0, n_epochs_kl_warmup=400, **kwargs)[source]

Bases: scvi.inference.trainer.Trainer

The trainer class for the unsupervised training of JVAE.

Parameters
  • model (ModuleModule) – A model instance from class JVAE

  • discriminator (ModuleModule) – A model instance of a classifier (with logit output)

  • gene_dataset_list (List[GeneExpressionDataset]List[GeneExpressionDataset]) – list of gene_dataset instance like [CortexDataset(), SmfishDataset()]

  • train_size (floatfloat) – Train-test split ratio in (0,1) to split cells

  • kappa (floatfloat) – float to weight the discriminator loss

  • n_epochs_kl_warmup (intint) – Number of epochs for linear warmup of KL(q(z|x)||p(z)) term. After n_epochs_kl_warmup, the training objective is the ELBO. This might be used to prevent inactivity of latent units, and/or to improve clustering of latent space, as a long warmup turns the model into something more of an autoencoder.

  • **kwargs – Other keywords arguments from the general Trainer class.

Attributes Summary

default_metrics_to_monitor

posteriors_loop

Methods Summary

data_loaders_loop()

returns an zipped iterable corresponding to loss signature

get_discriminator_confusion()

A good mixing should lead to a uniform matrix.

get_imputed_values([deterministic, …])

Return imputed values for all genes for each dataset

get_latent([deterministic])

Return the latent space embedding for each dataset

get_loss_magnitude([one_sample])

Return the different losses of the model separately.

loss(tensors[, return_details])

Compute the loss of vae (reconstruction + kl_divergence)

loss_discriminator(latent_tensors[, …])

Compute the loss of the discriminator (either for the true labels or the fool labels)

on_epoch_begin()

on_training_loop(tensors_list)

training_extras_end()

Place to put extra models in eval mode, etc.

training_extras_init([lr_d, eps])

Other necessary models to simultaneously train

Attributes Documentation

default_metrics_to_monitor = ['elbo']
posteriors_loop

Methods Documentation

data_loaders_loop()[source]

returns an zipped iterable corresponding to loss signature

get_discriminator_confusion()[source]

A good mixing should lead to a uniform matrix.

Return type

ndarrayndarray

get_imputed_values(deterministic=True, normalized=True, decode_mode=None)[source]

Return imputed values for all genes for each dataset

Parameters
  • deterministic (boolbool) – If true, use the mean of the encoder instead of a Gaussian sample for the latent vector

  • normalized (boolbool) – Return imputed normalized values or not

  • decode_mode (int, NoneOptional[int]) – If a decode_mode is given, use the encoder specific to each dataset as usual but use the decoder of the dataset of id decode_mode to impute values

Return type

List[ndarray]List[ndarray]

get_latent(deterministic=True)[source]

Return the latent space embedding for each dataset

Parameters

deterministic (boolbool) – If true, use the mean of the encoder instead of a Gaussian sample

Return type

List[ndarray]List[ndarray]

get_loss_magnitude(one_sample=False)[source]

Return the different losses of the model separately. Useful to inspect and compare their magnitude.

Parameters

one_sample (boolbool) – Use only one batch to estimate the loss, can be much faster/less exact on big datasets

Return type

Tuple[ndarray, ndarray, ndarray]Tuple[ndarray, ndarray, ndarray]

loss(tensors, return_details=False)[source]

Compute the loss of vae (reconstruction + kl_divergence)

Parameters
  • tensors (Iterable[Tensor]Iterable[Tensor]) – Tensors of observations for each dataset

  • return_details (boolbool) – Boolean used to inspect the loss values, return detailed loss for each dataset

Return type

Tensor, Tuple[List[Tensor], List[Tensor]]Union[Tensor, Tuple[List[Tensor], List[Tensor]]]

Returns

type scalar loss if return_details is False, else tuple (reconstruction_loss, kl_loss)

loss_discriminator(latent_tensors, predict_true_class=True, return_details=False)[source]

Compute the loss of the discriminator (either for the true labels or the fool labels)

Parameters
  • latent_tensors (List[torch.Tensor]) – Tensors for each dataset of the latent space

  • predict_true_class (boolbool) – Specify if the loss aims at minimizing the accuracy or the mixing

  • return_details (boolbool) – Boolean used to inspect the loss values, return detailed loss for each dataset

  • latent_tensors

Return type

List[Tensor], TensorUnion[List[Tensor], Tensor]

Returns

type scalar loss if return_details is False, else list of scalar losses for each dataset

on_epoch_begin()[source]
on_training_loop(tensors_list)[source]
training_extras_end()[source]

Place to put extra models in eval mode, etc.

training_extras_init(lr_d=0.001, eps=0.01)[source]

Other necessary models to simultaneously train

Parameters

**extras_kwargs

Returns