scvi.module.AmortizedLDAPyroModule#

class scvi.module.AmortizedLDAPyroModule(n_input, n_topics, n_hidden, cell_topic_prior=None, topic_feature_prior=None)[source]#

Bases: PyroBaseModuleClass

An amortized implementation of Latent Dirichlet Allocation [Blei03] implemented in Pyro.

This module uses auto encoding variational Bayes to optimize the latent variables in the model. In particular, a fully-connected neural network is used as an encoder, which takes in feature counts as input and outputs the parameters of cell topic distribution. To employ the reparametrization trick stably, the Dirichlet priors are approximated by a Logistic-Normal distribution. The input feature counts tensor is a cell by features Bag-of-Words(BoW) representation of the counts. I.e. the model treats each cell’s feature vector as ordered, not as unordered as in a Multinomial distribution.

Parameters:
n_input : int

Number of input features.

n_topics : int

Number of topics/topics to model.

n_hidden : int

Number of nodes in the hidden layer of the encoder.

cell_topic_prior : float | Sequence[float] | NoneUnion[float, Sequence[float], None] (default: None)

Prior of cell topic distribution. If None, defaults to 1 / n_topics.

topic_feature_prior : float | Sequence[float] | NoneUnion[float, Sequence[float], None] (default: None)

Prior of topic feature distribution. If None, defaults to 1 / n_topics.

Attributes table#

Methods table#

get_elbo(x, library, n_obs)

Computes ELBO.

get_topic_distribution(x, n_samples)

Converts x to its inferred topic distribution.

topic_by_feature(n_samples)

Gets a Monte-Carlo estimate of the expectation of the topic by feature matrix.

Attributes#

T_destination#

AmortizedLDAPyroModule.T_destination#

alias of TypeVar(‘T_destination’, bound=Dict[str, Any])

alias of TypeVar(‘T_destination’, bound=Dict[str, Any]) .. autoattribute:: AmortizedLDAPyroModule.T_destination dump_patches ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

AmortizedLDAPyroModule.dump_patches: bool = False#

guide#

AmortizedLDAPyroModule.guide[source]#

list_obs_plate_vars#

AmortizedLDAPyroModule.list_obs_plate_vars[source]#

Model annotation for minibatch training with pyro plate.

A dictionary with: 1. “name” - the name of observation/minibatch plate; 2. “in” - indexes of model args to provide to encoder network when using amortised inference; 3. “sites” - dictionary with

keys - names of variables that belong to the observation plate (used to recognise

and merge posterior samples for minibatch variables)

values - the dimensions in non-plate axis of each variable (used to construct output

layer of encoder network when using amortised inference)

model#

AmortizedLDAPyroModule.model[source]#

training#

AmortizedLDAPyroModule.training: bool#

Methods#

get_elbo#

AmortizedLDAPyroModule.get_elbo(x, library, n_obs)[source]#

Computes ELBO.

Parameters:
x : Tensor

Counts tensor.

library : Tensor

Library sizes for each cell.

n_obs : int

Size of full batch. If n_obs < x.shape[0], ELBO is scaled by (n_obs / x.shape[0]).

Return type:

float

Returns:

The positive ELBO.

get_topic_distribution#

AmortizedLDAPyroModule.get_topic_distribution(x, n_samples)[source]#

Converts x to its inferred topic distribution.

Parameters:
x : Tensor

Counts tensor.

n_samples : int

Number of samples to take for the Monte-Carlo estimate of the mean.

Return type:

Tensor

Returns:

A x.shape[0] x n_topics tensor containing the normalized topic distribution.

topic_by_feature#

AmortizedLDAPyroModule.topic_by_feature(n_samples)[source]#

Gets a Monte-Carlo estimate of the expectation of the topic by feature matrix.

Assumes the module has already been trained.

Parameters:
n_samples : int

Number of samples to take for the Monte-Carlo estimate of the mean.

Return type:

Tensor

Returns:

A n_topics x n_input tensor containing the topic by feature matrix.