scviva.module.JVAE#
- class scviva.module.JVAE(dim_input_list, total_genes, indices_mappings, gene_likelihoods, model_library_bools, library_log_means, library_log_vars, n_latent=10, n_layers_encoder_individual=1, n_layers_encoder_shared=1, dim_hidden_encoder=64, n_layers_decoder_individual=0, n_layers_decoder_shared=0, dim_hidden_decoder_individual=64, dim_hidden_decoder_shared=64, dropout_rate_encoder=0.2, dropout_rate_decoder=0.2, n_batch=0, n_labels=0, dispersion='gene-batch', log_variational=True)[source]#
Joint variational auto-encoder for imputing missing genes in spatial data.
Implementation of gimVI [Lopez et al., 2019].
- Parameters:
- dim_input_list
list[int] List of number of input genes for each dataset.
- total_genes
int Total number of different genes.
- indices_mappings
list[ndarray|slice] List of mappings from model inputs to model output locations.
- gene_likelihoods
list[str] List of distributions: ‘zinb’, ‘nb’, or ‘poisson’.
- model_library_bools
list[bool] Whether to model library size with a latent variable per dataset.
- library_log_means
list[ndarray|None] List of 1 x n_batch arrays of log library size means.
- library_log_vars
list[ndarray|None] List of 1 x n_batch arrays of log library size variances.
- n_latent
int Dimension of latent space.
- n_layers_encoder_individual
int Number of individual encoder layers.
- n_layers_encoder_shared
int Number of shared encoder layers.
- dim_hidden_encoder
int Hidden layer dimension for encoder.
- n_layers_decoder_individual
int Number of individual decoder layers.
- n_layers_decoder_shared
int Number of shared decoder layers.
- dim_hidden_decoder_individual
int Hidden layer dimension for individual decoder.
- dim_hidden_decoder_shared
int Hidden layer dimension for shared decoder.
- dropout_rate_encoder
float Dropout rate for encoder.
- dropout_rate_decoder
float Dropout rate for decoder.
- n_batch
int Total number of batches.
- n_labels
int Total number of labels.
- dispersion
str Dispersion parameterization: ‘gene’, ‘gene-batch’, ‘gene-label’, or ‘gene-cell’.
- log_variational
bool Log(data+1) prior to encoding for numerical stability.
- dim_input_list
Attributes table
Methods table
|
Forward pass through the network. |
|
Run the generative model. |
|
Get the sample rate for the model. |
|
Run the inference model. |
|
Return the reconstruction loss and the Kullback divergences. |
|
Callback function run in |
|
Compute the reconstruction loss. |
|
Generate samples from the learned model. |
|
Sample the tensor of library sizes from the posterior. |
|
Sample tensor of latent values from the posterior. |
|
Return the tensor of scaled frequencies of expression. |
|
Return the tensor of predicted frequencies of expression. |
Attributes
- JVAE.device#
Methods
- JVAE.forward(tensors, get_inference_input_kwargs=None, get_generative_input_kwargs=None, inference_kwargs=None, generative_kwargs=None, loss_kwargs=None, compute_loss=True)[source]#
Forward pass through the network.
- Parameters:
- tensors
tensors to pass through
- get_inference_input_kwargs
dict|None Keyword args for
_get_inference_input()- get_generative_input_kwargs
dict|None Keyword args for
_get_generative_input()- inference_kwargs
dict|None Keyword args for
inference()- generative_kwargs
dict|None Keyword args for
generative()- loss_kwargs
dict|None Keyword args for
loss()- compute_loss
Whether to compute loss on forward pass. This adds another return value.
- Return type:
- JVAE.generative(z, library, batch_index=None, y=None, mode=0, transform_batch=None)[source]#
Run the generative model.
- Parameters:
- z
Tensor Latent values with shape
(n_cells, n_latent).- library
Tensor Log library size with shape
(n_cells, 1).- batch_index
Tensor|None Batch index of each cell, with shape
(n_cells, 1). Used by the decoder and, whendispersion="gene-batch", to select the dispersion.- y
Tensor|None Label of each cell, with shape
(n_cells, 1). Used whendispersion="gene-label"to select the dispersion.- mode
int|None Index of the dataset whose decoder head is used.
px_scaleis normalized over the genes of this dataset.- transform_batch
Tensor|None If not
None, the"batch_index"entry of the returned dictionary is replaced by this value. It does not affect the decoded quantities.
- z
- Return type:
- Returns:
Dictionary with keys
"px_scale","px","px_r","px_rate","px_dropout"and"batch_index".
- JVAE.get_sample_rate(x, batch_index, *_, **__)[source]#
Get the sample rate for the model.
Encodes and decodes
xwith dataset0(seesample_rate()).- Parameters:
- x
Tensor of counts for dataset
0with shape(n_cells, n_genes_0).- batch_index
Batch index of each cell, with shape
(n_cells, 1).- *_
Ignored; accepted for interface compatibility.
- **__
Ignored; accepted for interface compatibility.
- JVAE.inference(x, mode=0, n_samples=1, batch_index=None)[source]#
Run the inference model.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- mode
int|None Index of the dataset (encoder head)
xcomes from.- n_samples
int|None Number of samples of
zto draw from the posterior. If greater than1,zgains a leading sample dimension.- batch_index
Tensor|None Batch index of each cell. Unused; accepted for interface compatibility.
- x
- Return type:
- Returns:
Dictionary with keys
"qz"(posterior overz),"z","ql"(posterior over the log library size, orNoneif the library is not modeled) and"library"(sampled or observed log library size).
- JVAE.loss(tensors, inference_outputs, generative_outputs, mode=None, kl_weight=1.0)[source]#
Return the reconstruction loss and the Kullback divergences.
- Parameters:
- tensors
Dictionary of minibatch tensors; uses the registered counts and batch index.
- inference_outputs
Output of
inference().- generative_outputs
Output of
generative().- mode
int|None Index of the dataset the minibatch comes from. May be
Noneonly when the model has a single dataset, in which case0is used.- kl_weight
float Weight of the KL divergence terms (
zand, if modeled, library size).
- Return type:
- Returns:
LossOutputwith the total loss, per-cell reconstruction loss and per-cell local KL divergence.
- JVAE.reconstruction_loss(x, px_rate, px_r, px_dropout, mode)[source]#
Compute the reconstruction loss.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- px_rate
Tensor Mean of the count distribution, restricted to the genes of dataset
mode.- px_r
Tensor Inverse dispersion of the count distribution, restricted to the genes of dataset
mode.- px_dropout
Tensor Logits of the zero-inflation probability, restricted to the genes of dataset
mode. Only used when the gene likelihood is"zinb".- mode
int Index of the dataset; selects the gene likelihood from
gene_likelihoods.
- x
- Return type:
- Returns:
Negative log-likelihood per cell with shape
(n_cells,).
- JVAE.sample_from_posterior_l(x, mode=None, deterministic=False)[source]#
Sample the tensor of library sizes from the posterior.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- mode
int Index of the dataset (encoder head)
xcomes from.- deterministic
bool If
Trueand the library size is modeled for this dataset, return the posterior mean of the log library size instead of a sample. If the library size is not modeled, the observed log library size is returned regardless.
- x
- Return type:
- Returns:
Tensor of log library sizes with shape
(n_cells, 1).
- JVAE.sample_from_posterior_z(x, mode=None, deterministic=False)[source]#
Sample tensor of latent values from the posterior.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- mode
int Index of the dataset (encoder head)
xcomes from. May beNoneonly when the model has a single dataset, in which case0is used.- deterministic
bool If
True, return the posterior mean instead of a sample.
- x
- Return type:
- Returns:
Tensor of latent values with shape
(n_cells, n_latent).
- JVAE.sample_rate(x, mode, batch_index, y=None, deterministic=False, decode_mode=None)[source]#
Return the tensor of scaled frequencies of expression.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- mode
int Index of the dataset (encoder head)
xcomes from.- batch_index
Tensor Batch index of each cell, with shape
(n_cells, 1).- y
Tensor|None Label of each cell, with shape
(n_cells, 1). Required whendispersion="gene-label".- deterministic
bool If
True, use the posterior means ofzand of the library size (when the library is modeled) instead of samples.- decode_mode
int Index of the dataset whose decoder head is used. If
None, defaults tomode.
- x
- Return type:
- Returns:
Tensor of expected counts (
px_scaletimes library size) with shape(n_cells, total_genes).
- JVAE.sample_scale(x, mode, batch_index, y=None, deterministic=False, decode_mode=None)[source]#
Return the tensor of predicted frequencies of expression.
- Parameters:
- x
Tensor Tensor of counts with shape
(n_cells, n_genes_mode), wheren_genes_modeis the number of genes of datasetmode.- mode
int Index of the dataset (encoder head)
xcomes from.- batch_index
Tensor Batch index of each cell, with shape
(n_cells, 1).- y
Tensor|None Label of each cell, with shape
(n_cells, 1). Required whendispersion="gene-label".- deterministic
bool If
True, use the posterior means ofzand of the library size (when the library is modeled) instead of samples.- decode_mode
int|None Index of the dataset whose decoder head is used. If
None, defaults tomode.
- x
- Return type:
- Returns:
Tensor of normalized expression frequencies with shape
(n_cells, total_genes).