MAGICAffinity#

class torchdr.MAGICAffinity(K: int = 7, metric: str = 'sqeuclidean', zero_diag: bool = True, device: str | None = None, backend: str | None = None, verbose: bool = False)[source]#

Bases: Affinity

Compute the MAGIC affinity introduced in [Van Dijk et al., 2018].

The construction is as follows. First, it computes a Gaussian kernel with sample-wise bandwidth \(\mathbf{\sigma} \in \mathbb{R}^n\).

\[P_{ij} \leftarrow \exp \left( - \frac{C_{ij}}{\sigma_i} \right)\]

In the above, \(\mathbf{C}\) is the pairwise distance matrix and \(\sigma_i\) is the distance from the K’th nearest neighbor of data point \(\mathbf{x}_i\).

Then it averages the affinity matrix with its transpose:

\[P_{ij} \leftarrow \frac{P_{ij} + P_{ji}}{2} \:.\]

Finally, it normalizes the affinity matrix along each row:

\[P_{ij} \leftarrow \frac{P_{ij}}{\sum_{t} P_{it}} \:.\]
Parameters:
  • K (int, optional) – K-th neirest neighbor .

  • metric (str, optional) – Metric to use for pairwise distances computation.

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

  • device (str, optional) – Device to use for computations.

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

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