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_batch (
int
int
) – Number of batches, if 0, no batch correction is performed.n_layers (
int
int
) – Number of hidden layers used for encoder and decoder NNsdropout_rate (
float
float
) – Dropout rate for neural networksOne 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 (
bool
bool
) – Log(data+1) prior to encoding for numerical stability. Not normalization.reconstruction_loss (
str
str
) –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 listget_reconstruction_loss
(x, px_rate, px_r, …)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
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
- Returns
type the reconstruction loss and the Kullback divergences
-
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
- 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
- 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
-
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
- Returns
type tensor of shape
(batch_size, n_latent)