AffinityMatcher#

class torchdr.AffinityMatcher(affinity_in: Affinity, affinity_out: Affinity, kwargs_affinity_out: dict = {'log': True}, n_components: int = 2, loss_fn: str = 'square_loss', kwargs_loss: dict = {'log': True}, optimizer: str = 'Adam', optimizer_kwargs: dict | None = None, lr: float | str = 1.0, scheduler: str = 'constant', scheduler_kwargs: dict | None = None, min_grad_norm: float = 1e-07, max_iter: int = 1000, init: str | Tensor | ndarray = 'pca', init_scaling: float = 0.0001, device: str = 'auto', backend: str | None = None, verbose: bool = False, random_state: float | None = None, n_iter_check: int = 50)[source]#

Bases: DRModule

Perform dimensionality reduction by matching two affinity matrices.

It amounts to solving a problem of the form:

\[\min_{\mathbf{Z}} \: \mathcal{L}( \mathbf{P}, \mathbf{Q})\]

where \(\mathcal{L}\) is a loss function, \(\mathbf{P}\) is the input affinity matrix and \(\mathbf{Q}\) is the affinity matrix of the embedding.

The embedding optimization is performed using a first-order optimization method, with gradients calculated via PyTorch’s automatic differentiation.

Parameters:
  • affinity_in (Affinity) – The affinity object for the input space.

  • affinity_out (Affinity) – The affinity object for the output embedding space.

  • kwargs_affinity_out (dict, optional) – Additional keyword arguments for the affinity_out method.

  • n_components (int, optional) – Number of dimensions for the embedding. Default is 2.

  • optimizer (str, optional) – Optimizer to use for the optimization. Default is “Adam”.

  • optimizer_kwargs (dict, optional) – Additional keyword arguments for the optimizer.

  • lr (float or 'auto', optional) – Learning rate for the optimizer. Default is 1e0.

  • scheduler (str, optional) – Learning rate scheduler. Default is “constant”.

  • scheduler_kwargs (dict, optional) – Additional keyword arguments for the scheduler.

  • min_grad_norm (float, optional) – Tolerance for stopping criterion. Default is 1e-7.

  • max_iter (int, optional) – Maximum number of iterations. Default is 1000.

  • init (str | torch.Tensor | np.ndarray, optional) – Initialization method for the embedding. Default is “pca”.

  • init_scaling (float, optional) – Scaling factor for the initial embedding. Default is 1e-4.

  • device (str, optional) – Device to use for computations. Default is “auto”.

  • backend ({"keops", "faiss", None}, optional) – Which backend to use for handling sparsity and memory efficiency. Default is None.

  • verbose (bool, optional) – Verbosity of the optimization process. Default is False.

  • random_state (float, optional) – Random seed for reproducibility. Default is None.

  • n_iter_check (int, optional) – Number of iterations between two checks for convergence. Default is 50.

fit(X: Tensor | ndarray, y=None)[source]#

Fit the model to the provided data.

Parameters:
  • X (torch.Tensor or np.ndarray of shape (n_samples, n_features)) – or (n_samples, n_samples) if precomputed is True Input data.

  • y (None) – Ignored.

Returns:

self – The fitted AffinityMatcher instance.

Return type:

AffinityMatcher

fit_transform(X: Tensor | ndarray, y=None)[source]#

Fit the model to the provided data and returns the transformed data.

Parameters:
  • X (torch.Tensor or np.ndarray of shape (n_samples, n_features)) – or (n_samples, n_samples) if precomputed is True Input data.

  • y (None) – Ignored.

Returns:

embedding_ – The embedding of the input data.

Return type:

torch.Tensor

Examples using AffinityMatcher:#

PCA via SVD and via AffinityMatcher

PCA via SVD and via AffinityMatcher

TSNE embedding of the swiss roll dataset

TSNE embedding of the swiss roll dataset

Neighbor Embedding on genomics & equivalent affinity matcher formulation

Neighbor Embedding on genomics & equivalent affinity matcher formulation