smoother.models.reduction._spae =============================== .. py:module:: smoother.models.reduction._spae Classes ------- .. autoapisummary:: smoother.models.reduction._spae.AutoEncoderClass smoother.models.reduction._spae.SpatialAutoEncoder Module Contents --------------- .. py:class:: AutoEncoderClass(n_feature: int, n_latent: int = 10, spatial_loss: Optional[smoother.losses.SpatialLoss] = None, lambda_spatial_loss=0.1, lambda_orth_loss=0.1) Bases: :py:obj:`torch.nn.Module` Abstract AutoEncoder class. To mimic the behavior of PCA, the overall objective contains three losses: 1. Reconstruction loss: reconstruction error of the observed data. 2. Orthogonality loss: the loss of orthogonality on the hidden embeddings. 3. Spatial loss: the spatial loss on the hidden embeddings. .. py:attribute:: n_latent :value: 10 .. py:attribute:: n_feature .. py:attribute:: _data :value: None .. py:attribute:: spatial_loss :value: None .. py:attribute:: l_sp_loss :value: 0.1 .. py:attribute:: l_orth_loss :value: 0.1 .. py:attribute:: dr_configs .. py:attribute:: dr_logs .. py:method:: load_adata(adata: anndata.AnnData, layer: Optional[str] = None) Load data to project. :param adata: data to project. :type adata: AnnData :param layer: layer to project. If None, use adata.X. :type layer: str .. py:method:: forward(x) .. py:method:: encode(x) .. py:method:: decode(x_enc) .. py:method:: get_latent_representation() Get the latent representation of the loaded data. .. py:method:: get_recon_loss() :abstractmethod: Get the reconstruction loss. .. py:method:: get_orth_loss() :abstractmethod: Get the orthogonality loss (weighted by lambda_orth_loss). .. py:method:: get_sp_loss() :abstractmethod: Get the spatial loss (weighted by lambda_spatial_loss). .. py:method:: reduce(lr=0.01, max_epochs=1000, patience=10, tol=1e-05, optimizer='SGD', verbose=True, quite=False, clear_logs=True) .. py:method:: _reduce_dim_ae(ae_model, lr=0.01, max_epochs=1000, patience=10, tol=1e-05, optimizer='SGD', verbose=True, quite=False, clear_logs=False, return_model=False) :classmethod: Dimension reduction using auto-encoders. Two additional losses are added in addition to the reconstruction loss: 1. Orthogonality loss: the loss of the orthogonality of the hidden embedding. 2. Spatial loss: the spatial loss on the hidden embeddings. :param ae_model: The autoencoder model. :type ae_model: AutoEncoder :param lr: The learning rate. :type lr: float :param max_epochs: The maximum number of epochs. :type max_epochs: int :param patience: The patience for early stopping. :type patience: int :param tol: The tolerated convergence error. :type tol: float :param optimizer: The optimizer to be used. Can be 'SGD' or 'Adam'. :type optimizer: str :param verbose: If True, print out loss while training. :type verbose: bool :param quite: If True, no output printed. :type quite: bool :param clear_logs: If True, clear the logs in the autoencoder model. :type clear_logs: bool :param return_model: If True, return the trained autoencoder model. :type return_model: bool :returns: The trained autoencoder model. :rtype: ae_model (AutoEncoder) .. py:class:: SpatialAutoEncoder(adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.losses.SpatialLoss] = None, lambda_spatial_loss=0.1, lambda_orth_loss=0.1, recon_loss_mode: Literal['mse', 'poisson'] = 'poisson', n_layers: int = 1, n_hidden: int = 128, n_latent: int = 10, dropout_rate: float = 0.0, use_batch_norm: bool = True, use_activation: bool = True, activation_fn: Optional[torch.nn.Module] = nn.ReLU, use_bias=True) Bases: :py:obj:`AutoEncoderClass` Generic spatial auto-encoder. .. py:attribute:: setup_args .. py:attribute:: lib_size .. py:attribute:: encoder .. py:attribute:: decoder .. py:method:: encode(x) Project the input to the hidden space. :param x: The input matrix, num_gene x num_spot. :type x: 2D tensor :returns: The hidden embedding, num_spot x num_hidden. :rtype: x_enc (2D tensor) .. py:method:: decode(x_enc) Project the hidden embedding back to the input space. :param x_enc: The hidden embedding, num_spot x num_hidden. :type x_enc: 2D tensor :returns: The decoded matrix, num_gene x num_spot. :rtype: x_dec (2D tensor) .. py:method:: get_recon_loss() Get the reconstruction loss. .. py:method:: get_orth_loss() Get the orthogonality loss (weighted by lambda_orth_loss). .. py:method:: get_sp_loss() Get the spatial loss (weighted by lambda_spatial_loss). .. py:method:: from_rna_model(rna_model, st_adata: anndata.AnnData, layer: Optional[str] = None, spatial_loss: Optional[smoother.losses.SpatialLoss] = None, lambda_spatial_loss=0.1) :classmethod: Initialize a spatial model from a pre-trained RNA model.