Module

reduce.py

import goldener.reduce
View source

Dimensionality reduction of vectors.

The reduction tools reduce the dimension of 2D vectors, for instance to speed up the clustering or the selection on high dimensional embeddings:

logger = <Logger goldener.reduce (INFO)>
class GoldReductionTool(abc.ABC):

Reduce the dimensionality of 2D vectors.

Base class for all dimensionality reduction tools.

@abstractmethod
def transform(self, x: torch.Tensor) -> torch.Tensor:

Transform the 2D vectors using the dimensionality reduction tool.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to transform.
Returns:

Transformed tensor with reduced dimensionality.

class GoldReductionToolWithFit(GoldReductionTool):

Reduce the dimensionality of 2D vectors from fittable reduction methods.

Base class for dimensionality reduction tools that require fitting.

@abstractmethod
def fit(self, x: torch.Tensor) -> None:

Fit the dimensionality reduction tool to the data.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to fit the model on.
@abstractmethod
def fit_transform(self, x: torch.Tensor) -> torch.Tensor:

Fit the dimensionality reduction tool to the 2D vectors and transform it.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to fit and transform.
Returns:

Transformed tensor with reduced dimensionality.

Inherited Members
GoldReductionTool
transform
class GoldTorchModuleReductionTool(GoldReductionTool):

Dimensionality reduction of 2D vectors with a torch Module.

Attributes:
  • reducer: The torch Module to use for dimensionality reduction.
GoldTorchModuleReductionTool(reducer: torch.nn.modules.module.Module)

Initialize the GoldTorchModuleReductionTool.

Arguments:
  • reducer: The torch Module to use for dimensionality reduction.
reducer
def transform(self, x: torch.Tensor) -> torch.Tensor:

Transform the 2D vectors using the fitted dimensionality reduction model.

Arguments:
  • x: x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to transform.
Returns:

Transformed tensor with reduced dimensionality.

class GoldSKLearnReductionTool(GoldReductionToolWithFit):

Dimensionality reduction of 2D vectors using UMAP, PCA, TSNE, or GaussianRandomProjection.

Attributes:
  • _reducer: An instance of UMAP, PCA, TSNE, or GaussianRandomProjection.
GoldSKLearnReductionTool( reducer: umap.umap_.UMAP | sklearn.decomposition._pca.PCA | sklearn.manifold._t_sne.TSNE | sklearn.random_projection.GaussianRandomProjection)

Initialize the GoldSKLearnReductionTool.

Arguments:
  • reducer: An instance of UMAP, PCA, TSNE, or GaussianRandomProjection for dimensionality reduction.
def fit(self, x: torch.Tensor) -> None:

Fit the dimensionality reduction model to the 2D vectors.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to fit the model on.
Raises:
  • ValueError: If input is not a 2D tensor.
def fit_transform(self, x: torch.Tensor) -> torch.Tensor:

Fit the dimensionality reduction model to the 2D vectors and transform it.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to fit and transform.
Returns:

Transformed tensor with reduced dimensionality.

Raises:
  • ValueError: If input is not a 2D tensor.
def transform(self, x: torch.Tensor) -> torch.Tensor:

Transform the 2D vectors using the fitted dimensionality reduction model.

Arguments:
  • x: Already vectorized 2D input tensor of shape (num_vectors, feature_dim) to transform.
Returns:

Transformed tensor with reduced dimensionality.

Raises:
  • ValueError: If input is not a 2D tensor.