Affinity#

class torchdr.Affinity(metric: str = 'sqeuclidean', zero_diag: bool = True, device: str = 'auto', backend: str | FaissConfig | None = None, verbose: bool = False, random_state: float | None = None, compile: bool = False, _pre_processed: bool = False)[source]#

Bases: Module, ABC

Base class for affinity matrices.

Parameters:
  • metric (str, optional) – The distance metric to use for computing pairwise distances.

  • zero_diag (bool, optional) – Whether to set the diagonal of the affinity matrix to zero.

  • device (str, optional) – The device to use for computation. Typically “cuda” for GPU or “cpu” for CPU. If “auto”, uses the device of the input data. If None is passed, it is converted to “auto”. Default is “auto”.

  • backend ({"keops", "faiss", None} or FaissConfig, optional) –

    Which backend to use for handling sparsity and memory efficiency. Can be: - “keops”: Use KeOps for memory-efficient symbolic computations - “faiss”: Use FAISS for fast k-NN computations with default settings - None: Use standard PyTorch operations - FaissConfig object: Use FAISS with custom configuration

    (e.g., FaissConfig(temp_memory=2.0))

    Default is None.

  • verbose (bool, optional) – Verbosity. Default is False.

  • compile (bool, optional) – Whether to compile the affinity matrix computation. Default is False.

  • _pre_processed (bool, optional) – If True, assumes inputs are already torch tensors on the correct device and skips the to_torch conversion. Default is False.

clear_memory()[source]#

Clear non-persistent buffers to free memory.

Examples using Affinity:#

Entropic Affinities can adapt to varying noise levels

Entropic Affinities can adapt to varying noise levels

Neighbor Embedding on genomics & equivalent affinity matcher formulation

Neighbor Embedding on genomics & equivalent affinity matcher formulation

PCA via SVD and via AffinityMatcher

PCA via SVD and via AffinityMatcher