Skip to content

manifold

Bases: object

Base class for manifold learning.

This class provides a generic interface for manifold learning algorithms, with methods to set the number of neighbors and components and to perform dimensionality reduction using a specified manifold learning technique.

Attributes:

Name Type Description
name str

The name of the manifold learning method.

n_neighbors int

The number of neighbors to consider for the manifold learning algorithm.

n_components int

The number of components for dimensionality reduction.

model object

The manifold learning model to be used.

Methods:

Name Description
get_n_neighbors

Get the number of neighbors used in the manifold learning algorithm.

set_n_neighbors

Set the number of neighbors and reinitialize the model.

get_n_components

Get the number of components for dimensionality reduction.

set_n_components

Set the number of components and reinitialize the model.

forward

Apply the manifold learning algorithm to reduce the dimensionality of the input data.

fit_transform

Perform dimensionality reduction on the input data.

init_model

Abstract method to initialize the manifold learning model.

Source code in tinybig/koala/manifold/manifold.py
class manifold(object):
    """
        Base class for manifold learning.

        This class provides a generic interface for manifold learning algorithms, with methods to set the number of
        neighbors and components and to perform dimensionality reduction using a specified manifold learning technique.

        Attributes
        ----------
        name : str
            The name of the manifold learning method.
        n_neighbors : int
            The number of neighbors to consider for the manifold learning algorithm.
        n_components : int
            The number of components for dimensionality reduction.
        model : object
            The manifold learning model to be used.

        Methods
        -------
        get_n_neighbors()
            Get the number of neighbors used in the manifold learning algorithm.
        set_n_neighbors(n_neighbors)
            Set the number of neighbors and reinitialize the model.
        get_n_components()
            Get the number of components for dimensionality reduction.
        set_n_components(n_components)
            Set the number of components and reinitialize the model.
        forward(X, device='cup', *args, **kwargs)
            Apply the manifold learning algorithm to reduce the dimensionality of the input data.
        fit_transform(X, device='cup', *args, **kwargs)
            Perform dimensionality reduction on the input data.
        init_model()
            Abstract method to initialize the manifold learning model.
    """
    def __init__(self, name: str = 'base_manifold', n_neighbors: int = 5, n_components: int = 2, *args, **kwargs):
        """
            Initialize the manifold learning class.

            Parameters
            ----------
            name : str, optional
                The name of the manifold learning method. Default is 'base_manifold'.
            n_neighbors : int, optional
                The number of neighbors to consider. Default is 5.
            n_components : int, optional
                The number of components for dimensionality reduction. Default is 2.
            *args, **kwargs
                Additional arguments for initialization.
        """
        self.name = name
        self.n_neighbors = n_neighbors
        self.n_components = n_components

        self.model = None
        self.init_model()

    def get_n_neighbors(self):
        """
            Get the number of neighbors used in the manifold learning algorithm.

            Returns
            -------
            int
                The number of neighbors.
        """
        return self.n_neighbors

    def set_n_neighbors(self, n_neighbors):
        """
            Set the number of neighbors and reinitialize the model.

            Parameters
            ----------
            n_neighbors : int
                The new number of neighbors.
        """
        self.n_neighbors = n_neighbors
        self.init_model()

    def get_n_components(self):
        """
        Get the number of components for dimensionality reduction.

        Returns
        -------
        int
            The number of components.
        """
        return self.n_components

    def set_n_components(self, n_components):
        """
        Set the number of components and reinitialize the model.

        Parameters
        ----------
        n_components : int
            The new number of components.
        """
        self.n_components = n_components
        self.init_model()

    def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
        Apply the manifold learning algorithm to the input data.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for dimensionality reduction.
        device : str, optional
            The device to use ('cup' or 'cpu'). Default is 'cup'.
        *args, **kwargs
            Additional arguments.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The transformed data.
        """
        return self.forward(X=X, device=device, *args, **kwargs)

    def forward(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
        Perform dimensionality reduction on the input data.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for dimensionality reduction.
        device : str, optional
            The device to use ('cup' or 'cpu'). Default is 'cup'.
        *args, **kwargs
            Additional arguments.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The transformed data.
        """
        return self.fit_transform(X=X, device=device, *args, **kwargs)

    def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
        Perform dimensionality reduction on the input data.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for dimensionality reduction.
        device : str, optional
            The device to use ('cup' or 'cpu'). Default is 'cup'.
        *args, **kwargs
            Additional arguments.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The transformed data.
        """
        if isinstance(X, torch.Tensor):
            input_X = X.detach().cpu().numpy()  # Convert torch.Tensor to numpy
        else:
            input_X = X

        X_manifold = self.model.fit_transform(X=input_X)

        return torch.tensor(X_manifold) if isinstance(X, torch.Tensor) and not isinstance(X_manifold, torch.Tensor) else X_manifold

    @abstractmethod
    def init_model(self):
        """
        Abstract method to initialize the manifold learning model.
        """
        pass

__call__(X, device='cup', *args, **kwargs)

Apply the manifold learning algorithm to the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for dimensionality reduction.

required
device str

The device to use ('cup' or 'cpu'). Default is 'cup'.

'cup'
*args

Additional arguments.

()
**kwargs

Additional arguments.

()

Returns:

Type Description
Union[ndarray, Tensor]

The transformed data.

Source code in tinybig/koala/manifold/manifold.py
def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
    Apply the manifold learning algorithm to the input data.

    Parameters
    ----------
    X : Union[np.ndarray, torch.Tensor]
        The input data for dimensionality reduction.
    device : str, optional
        The device to use ('cup' or 'cpu'). Default is 'cup'.
    *args, **kwargs
        Additional arguments.

    Returns
    -------
    Union[np.ndarray, torch.Tensor]
        The transformed data.
    """
    return self.forward(X=X, device=device, *args, **kwargs)

__init__(name='base_manifold', n_neighbors=5, n_components=2, *args, **kwargs)

Initialize the manifold learning class.

Parameters:

Name Type Description Default
name str

The name of the manifold learning method. Default is 'base_manifold'.

'base_manifold'
n_neighbors int

The number of neighbors to consider. Default is 5.

5
n_components int

The number of components for dimensionality reduction. Default is 2.

2
*args

Additional arguments for initialization.

()
**kwargs

Additional arguments for initialization.

()
Source code in tinybig/koala/manifold/manifold.py
def __init__(self, name: str = 'base_manifold', n_neighbors: int = 5, n_components: int = 2, *args, **kwargs):
    """
        Initialize the manifold learning class.

        Parameters
        ----------
        name : str, optional
            The name of the manifold learning method. Default is 'base_manifold'.
        n_neighbors : int, optional
            The number of neighbors to consider. Default is 5.
        n_components : int, optional
            The number of components for dimensionality reduction. Default is 2.
        *args, **kwargs
            Additional arguments for initialization.
    """
    self.name = name
    self.n_neighbors = n_neighbors
    self.n_components = n_components

    self.model = None
    self.init_model()

fit_transform(X, device='cup', *args, **kwargs)

Perform dimensionality reduction on the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for dimensionality reduction.

required
device str

The device to use ('cup' or 'cpu'). Default is 'cup'.

'cup'
*args

Additional arguments.

()
**kwargs

Additional arguments.

()

Returns:

Type Description
Union[ndarray, Tensor]

The transformed data.

Source code in tinybig/koala/manifold/manifold.py
def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
    Perform dimensionality reduction on the input data.

    Parameters
    ----------
    X : Union[np.ndarray, torch.Tensor]
        The input data for dimensionality reduction.
    device : str, optional
        The device to use ('cup' or 'cpu'). Default is 'cup'.
    *args, **kwargs
        Additional arguments.

    Returns
    -------
    Union[np.ndarray, torch.Tensor]
        The transformed data.
    """
    if isinstance(X, torch.Tensor):
        input_X = X.detach().cpu().numpy()  # Convert torch.Tensor to numpy
    else:
        input_X = X

    X_manifold = self.model.fit_transform(X=input_X)

    return torch.tensor(X_manifold) if isinstance(X, torch.Tensor) and not isinstance(X_manifold, torch.Tensor) else X_manifold

forward(X, device='cup', *args, **kwargs)

Perform dimensionality reduction on the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for dimensionality reduction.

required
device str

The device to use ('cup' or 'cpu'). Default is 'cup'.

'cup'
*args

Additional arguments.

()
**kwargs

Additional arguments.

()

Returns:

Type Description
Union[ndarray, Tensor]

The transformed data.

Source code in tinybig/koala/manifold/manifold.py
def forward(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
    Perform dimensionality reduction on the input data.

    Parameters
    ----------
    X : Union[np.ndarray, torch.Tensor]
        The input data for dimensionality reduction.
    device : str, optional
        The device to use ('cup' or 'cpu'). Default is 'cup'.
    *args, **kwargs
        Additional arguments.

    Returns
    -------
    Union[np.ndarray, torch.Tensor]
        The transformed data.
    """
    return self.fit_transform(X=X, device=device, *args, **kwargs)

get_n_components()

Get the number of components for dimensionality reduction.

Returns:

Type Description
int

The number of components.

Source code in tinybig/koala/manifold/manifold.py
def get_n_components(self):
    """
    Get the number of components for dimensionality reduction.

    Returns
    -------
    int
        The number of components.
    """
    return self.n_components

get_n_neighbors()

Get the number of neighbors used in the manifold learning algorithm.

Returns:

Type Description
int

The number of neighbors.

Source code in tinybig/koala/manifold/manifold.py
def get_n_neighbors(self):
    """
        Get the number of neighbors used in the manifold learning algorithm.

        Returns
        -------
        int
            The number of neighbors.
    """
    return self.n_neighbors

init_model() abstractmethod

Abstract method to initialize the manifold learning model.

Source code in tinybig/koala/manifold/manifold.py
@abstractmethod
def init_model(self):
    """
    Abstract method to initialize the manifold learning model.
    """
    pass

set_n_components(n_components)

Set the number of components and reinitialize the model.

Parameters:

Name Type Description Default
n_components int

The new number of components.

required
Source code in tinybig/koala/manifold/manifold.py
def set_n_components(self, n_components):
    """
    Set the number of components and reinitialize the model.

    Parameters
    ----------
    n_components : int
        The new number of components.
    """
    self.n_components = n_components
    self.init_model()

set_n_neighbors(n_neighbors)

Set the number of neighbors and reinitialize the model.

Parameters:

Name Type Description Default
n_neighbors int

The new number of neighbors.

required
Source code in tinybig/koala/manifold/manifold.py
def set_n_neighbors(self, n_neighbors):
    """
        Set the number of neighbors and reinitialize the model.

        Parameters
        ----------
        n_neighbors : int
            The new number of neighbors.
    """
    self.n_neighbors = n_neighbors
    self.init_model()