DRModule#

class torchdr.DRModule(n_components: int = 2, device: str = 'auto', backend: str | FaissConfig | None = None, verbose: bool = False, random_state: float | None = None, compile: bool = False, process_duplicates: bool = True, **kwargs)[source]#

Bases: BaseEstimator, Module, ABC

Base class for dimensionality reduction methods.

Subclasses must implement _fit_transform().

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

  • device (str, optional) – Device for computations. "auto" uses the input tensor’s device. Default is “auto”.

  • backend ({"keops", "faiss", None} or FaissConfig, optional) – Backend for handling sparsity and memory efficiency. Default is None (standard PyTorch).

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

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

  • compile (bool, default=False) – Whether to use torch.compile for faster computation.

  • process_duplicates (bool, default=True) – Whether to handle duplicate data points by default.

clear_memory()[source]#

Clear non-persistent buffers to free memory after training.

fit(X: ArrayLike, y: Any | None = None) DRModule[source]#

Fit the model from the input data.

Parameters:
  • X (ArrayLike of shape (n_samples, n_features)) – Input data (or (n_samples, n_samples) if precomputed).

  • y (None) – Ignored.

Returns:

self – The fitted instance.

Return type:

DRModule

fit_transform(X: ArrayLike, y: Any | None = None) ArrayLike[source]#

Fit the model and return the embedding.

Handles duplicate data points by default: performs DR on unique points and maps results back to the original structure. Controlled by process_duplicates.

Parameters:
  • X (ArrayLike of shape (n_samples, n_features)) – Input data (or (n_samples, n_samples) if precomputed).

  • y (None) – Ignored.

Returns:

embedding_ – The embedding.

Return type:

ArrayLike of shape (n_samples, n_components)

transform(X: ArrayLike | None = None) ArrayLike[source]#

Transform data into the learned embedding space.

If X is None, returns the training embedding. When an encoder is set, new data is transformed via encoder(X).

Parameters:

X (ArrayLike of shape (n_samples, n_features), optional) – Data to transform. If None, returns the training embedding.

Returns:

embedding_ – The embedding.

Return type:

ArrayLike of shape (n_samples, n_components)

Examples using DRModule:#

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

TSNE embedding of the swiss roll dataset

TSNE embedding of the swiss roll dataset

TSNE vs COSNE : Euclidean vs Hyperbolic

TSNE vs COSNE : Euclidean vs Hyperbolic