scvi.module.SCANVAE#

class scvi.module.SCANVAE(n_input, n_batch=0, n_labels=0, n_hidden=128, n_latent=10, n_layers=1, n_continuous_cov=0, n_cats_per_cov=None, dropout_rate=0.1, dispersion='gene', log_variational=True, gene_likelihood='zinb', y_prior=None, labels_groups=None, use_labels_groups=False, linear_classifier=False, classifier_parameters=None, use_batch_norm='both', use_layer_norm='none', **vae_kwargs)[source]#

Bases: VAE

Single-cell annotation using variational inference.

This is an implementation of the scANVI model described in [Xu et al., 2021], inspired from M1 + M2 model, as described in (https://arxiv.org/pdf/1406.5298.pdf).

Parameters:
  • n_input (int) – Number of input genes

  • n_batch (int (default: 0)) – Number of batches

  • n_labels (int (default: 0)) – Number of labels

  • n_hidden (int (default: 128)) – Number of nodes per hidden layer

  • n_latent (int (default: 10)) – Dimensionality of the latent space

  • n_layers (int (default: 1)) – Number of hidden layers used for encoder and decoder NNs

  • n_continuous_cov (int (default: 0)) – Number of continuous covarites

  • n_cats_per_cov (Iterable[int] | None (default: None)) – Number of categories for each extra categorical covariate

  • dropout_rate (float (default: 0.1)) – Dropout rate for neural networks

  • dispersion (Literal['gene', 'gene-batch', 'gene-label', 'gene-cell'] (default: 'gene')) –

    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 (bool (default: True)) – Log(data+1) prior to encoding for numerical stability. Not normalization.

  • gene_likelihood (Literal['zinb', 'nb'] (default: 'zinb')) –

    One of:

    • 'nb' - Negative binomial distribution

    • 'zinb' - Zero-inflated negative binomial distribution

  • y_prior (Tensor | None (default: None)) – If None, initialized to uniform probability over cell types

  • labels_groups (Sequence[int] (default: None)) – Label group designations

  • use_labels_groups (bool (default: False)) – Whether to use the label groups

  • linear_classifier (bool (default: False)) – If True, uses a single linear layer for classification instead of a multi-layer perceptron.

  • classifier_parameters (dict | None (default: None)) – Keyword arguments passed into Classifier.

  • use_batch_norm (Literal['encoder', 'decoder', 'none', 'both'] (default: 'both')) – Whether to use batch norm in layers

  • use_layer_norm (Literal['encoder', 'decoder', 'none', 'both'] (default: 'none')) – Whether to use layer norm in layers

  • linear_classifier – If True, uses a single linear layer for classification instead of a multi-layer perceptron.

  • **vae_kwargs – Keyword args for VAE

Attributes table#

training

Methods table#

classification_loss(labelled_dataset)

rtype:

tuple[Tensor, Tensor, Tensor]

classify(x[, batch_index, cont_covs, ...])

Forward pass through the encoder and classifier.

loss(tensors, inference_outputs, ...[, ...])

Compute the loss.

on_load(model)

Callback function run in load().

Attributes#

SCANVAE.training: bool#

Methods#

SCANVAE.classification_loss(labelled_dataset)[source]#
Return type:

tuple[Tensor, Tensor, Tensor]

SCANVAE.classify(x, batch_index=None, cont_covs=None, cat_covs=None, use_posterior_mean=True)[source]#

Forward pass through the encoder and classifier.

Parameters:
  • x (Tensor) – Tensor of shape (n_obs, n_vars).

  • batch_index (Tensor | None (default: None)) – Tensor of shape (n_obs,) denoting batch indices.

  • cont_covs (Tensor | None (default: None)) – Tensor of shape (n_obs, n_continuous_covariates).

  • cat_covs (Tensor | None (default: None)) – Tensor of shape (n_obs, n_categorical_covariates).

  • use_posterior_mean (bool (default: True)) – Whether to use the posterior mean of the latent distribution for classification.

Return type:

Tensor

Returns:

Tensor of shape (n_obs, n_labels) denoting logit scores per label. Before v1.1, this method by default returned probabilities per label, see #2301 for more details.

SCANVAE.loss(tensors, inference_outputs, generative_ouputs, kl_weight=1.0, labelled_tensors=None, classification_ratio=None)[source]#

Compute the loss.

SCANVAE.on_load(model)[source]#

Callback function run in load().