VAE

class scvi.models.VAE(n_input, n_batch=0, n_labels=0, n_hidden=128, n_latent=10, n_layers=1, dropout_rate=0.1, dispersion='gene', log_variational=True, reconstruction_loss='zinb', latent_distribution='normal')[source]

Bases: torch.nn.modules.module.Module

Variational auto-encoder model.

This is an implementation of the scVI model descibed in [Lopez18]

Parameters
  • n_input (intint) – Number of input genes

  • n_batch (intint) – Number of batches, if 0, no batch correction is performed.

  • n_labels (intint) – Number of labels

  • n_hidden (intint) – Number of nodes per hidden layer

  • n_latent (intint) – Dimensionality of the latent space

  • n_layers (intint) – Number of hidden layers used for encoder and decoder NNs

  • dropout_rate (floatfloat) – Dropout rate for neural networks

  • dispersion (strstr) –

    One of the following

    • 'gene' - dispersion parameter of NB is constant per gene across cells

    • 'gene-batch' - dispersion can differ between different batches

    • 'gene-label' - dispersion can differ between different labels

    • 'gene-cell' - dispersion can differ for every gene in every cell

  • log_variational (boolbool) – Log(data+1) prior to encoding for numerical stability. Not normalization.

  • reconstruction_loss (strstr) –

    One of

    • 'nb' - Negative binomial distribution

    • 'zinb' - Zero-inflated negative binomial distribution

    • 'poisson' - Poisson distribution

Examples

>>> gene_dataset = CortexDataset()
>>> vae = VAE(gene_dataset.nb_genes, n_batch=gene_dataset.n_batches * False,
... n_labels=gene_dataset.n_labels)

Methods Summary

forward(x, local_l_mean, local_l_var[, …])

Returns the reconstruction loss and the KL divergences

get_latents(x[, y])

Returns the result of sample_from_posterior_z inside a list

get_reconstruction_loss(x, px_rate, px_r, …)

rtype

TensorTensor

get_sample_rate(x[, batch_index, y, …])

Returns the tensor of means of the negative binomial distribution

get_sample_scale(x[, batch_index, y, …])

Returns the tensor of predicted frequencies of expression

inference(x[, batch_index, y, n_samples, …])

Helper function used in forward pass

sample_from_posterior_l(x)

Samples the tensor of library sizes from the posterior

sample_from_posterior_z(x[, y, give_mean, …])

Samples the tensor of latent values from the posterior

Methods Documentation

forward(x, local_l_mean, local_l_var, batch_index=None, y=None)[source]

Returns the reconstruction loss and the KL divergences

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • local_l_mean – tensor of means of the prior distribution of latent variable l with shape (batch_size, 1)

  • local_l_var – tensor of variancess of the prior distribution of latent variable l with shape (batch_size, 1)

  • batch_index – array that indicates which batch the cells belong to with shape batch_size (Default value = None)

  • y – tensor of cell-types labels with shape (batch_size, n_labels) (Default value = None)

Return type

Tuple[Tensor, Tensor]Tuple[Tensor, Tensor]

Returns

type the reconstruction loss and the Kullback divergences

get_latents(x, y=None)[source]

Returns the result of sample_from_posterior_z inside a list

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • y – tensor of cell-types labels with shape (batch_size, n_labels) (Default value = None)

Return type

TensorTensor

Returns

type one element list of tensor

get_reconstruction_loss(x, px_rate, px_r, px_dropout, **kwargs)[source]
Return type

TensorTensor

get_sample_rate(x, batch_index=None, y=None, n_samples=1, transform_batch=None)[source]

Returns the tensor of means of the negative binomial distribution

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • y – tensor of cell-types labels with shape (batch_size, n_labels) (Default value = None)

  • batch_index – array that indicates which batch the cells belong to with shape batch_size (Default value = None)

  • n_samples – number of samples (Default value = 1)

  • transform_batch – int of batch to transform samples into (Default value = None)

Return type

TensorTensor

Returns

type tensor of means of the negative binomial distribution with shape (batch_size, n_input)

get_sample_scale(x, batch_index=None, y=None, n_samples=1, transform_batch=None)[source]

Returns the tensor of predicted frequencies of expression

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • batch_index – array that indicates which batch the cells belong to with shape batch_size (Default value = None)

  • y – tensor of cell-types labels with shape (batch_size, n_labels) (Default value = None)

  • n_samples – number of samples (Default value = 1)

  • transform_batch – int of batch to transform samples into (Default value = None)

Return type

TensorTensor

Returns

type tensor of predicted frequencies of expression with shape (batch_size, n_input)

inference(x, batch_index=None, y=None, n_samples=1, transform_batch=None)[source]

Helper function used in forward pass

Return type

{str: Tensor}Dict[str, Tensor]

sample_from_posterior_l(x)[source]

Samples the tensor of library sizes from the posterior

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • y – tensor of cell-types labels with shape (batch_size, n_labels)

Return type

TensorTensor

Returns

type tensor of shape (batch_size, 1)

sample_from_posterior_z(x, y=None, give_mean=False, n_samples=5000)[source]

Samples the tensor of latent values from the posterior

Parameters
  • x – tensor of values with shape (batch_size, n_input)

  • y – tensor of cell-types labels with shape (batch_size, n_labels) (Default value = None)

  • give_mean – is True when we want the mean of the posterior distribution rather than sampling (Default value = False)

  • n_samples – how many MC samples to average over for transformed mean (Default value = 5000)

Return type

TensorTensor

Returns

type tensor of shape (batch_size, n_latent)