Skip to content

incremental_dimension_reduction

Bases: object

A base class for incremental dimension reduction methods.

This class provides a framework for implementing incremental dimension reduction techniques, with methods for setting the number of features, fitting, transforming, and reducing data.

Attributes:

Name Type Description
name str

The name of the dimension reduction method.

n_feature (int, optional)

The number of features to retain after reduction.

incremental (bool, optional)

Whether the dimension reduction is performed incrementally.

Methods:

Name Description
get_n_feature

Retrieve the number of features to retain.

set_n_feature

Set the number of features to retain.

__call__

Forward the input data through the dimension reduction process.

forward

Perform dimension reduction on the input data.

fit_transform

Fit the model to the input data and reduce its dimensionality.

fit

Abstract method for fitting the model to the input data.

transform

Abstract method for transforming input data based on the fitted model.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
class incremental_dimension_reduction(object):
    """
        A base class for incremental dimension reduction methods.

        This class provides a framework for implementing incremental dimension reduction techniques,
        with methods for setting the number of features, fitting, transforming, and reducing data.

        Attributes
        ----------
        name : str
            The name of the dimension reduction method.
        n_feature : int, optional
            The number of features to retain after reduction.
        incremental : bool, optional
            Whether the dimension reduction is performed incrementally.

        Methods
        -------
        get_n_feature()
            Retrieve the number of features to retain.
        set_n_feature(n_feature)
            Set the number of features to retain.
        __call__(X, device='cup', *args, **kwargs)
            Forward the input data through the dimension reduction process.
        forward(X, device='cup', *args, **kwargs)
            Perform dimension reduction on the input data.
        fit_transform(X, device='cup', *args, **kwargs)
            Fit the model to the input data and reduce its dimensionality.
        fit(X, device='cup', *args, **kwargs)
            Abstract method for fitting the model to the input data.
        transform(X, device='cup', *args, **kwargs)
            Abstract method for transforming input data based on the fitted model.
    """
    def __init__(self, name: str = 'incremental_dimension_reduction', n_feature: int = None, incremental: bool = True, *args, **kwargs):
        """
            Initialize the incremental dimension reduction class.

            Parameters
            ----------
            name : str, optional
                The name of the dimension reduction method. Default is 'incremental_dimension_reduction'.
            n_feature : int, optional
                The number of features to retain after reduction. Default is None.
            incremental : bool, optional
                Whether the dimension reduction is performed incrementally. Default is True.
            *args, **kwargs
                Additional arguments for subclass initialization.
        """
        super().__init__(*args, **kwargs)
        self.name = name
        self.incremental = incremental
        self.n_feature = n_feature

    def get_n_feature(self):
        """
            Retrieve the number of features to retain.

            Returns
            -------
            int
                The number of features to retain.
        """
        return self.n_feature

    def set_n_feature(self, n_feature):
        """
            Set the number of features to retain.

            Parameters
            ----------
            n_feature : int
                The number of features to retain.
        """
        self.n_feature = n_feature

    def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Forward the input data through the dimension reduction process.

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

            Returns
            -------
            Union[np.ndarray, torch.Tensor]
                The reduced input 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 dimension reduction on the input data.

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

            Returns
            -------
            Union[np.ndarray, torch.Tensor]
                The reduced input data.
        """
        if isinstance(X, torch.Tensor):
            input_X = X.detach().cpu().numpy()  # Convert torch.Tensor to numpy
        else:
            input_X = X
        X_reduced = self.fit_transform(X=input_X, device=device, *args, **kwargs)
        return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

    def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Fit the model to the input data and reduce its dimensionality.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data to reduce.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cup'.
            *args, **kwargs
                Additional arguments for the fit and transform processes.

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

        self.fit(X=input_X, device=device, *args, **kwargs)
        X_reduced = self.transform(X=input_X, device=device, *args, **kwargs)

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


    @abstractmethod
    def fit(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Abstract method for fitting the model to the input data.

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

            Raises
            ------
            NotImplementedError
                This method must be implemented in subclasses.
        """
        pass

    @abstractmethod
    def transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Abstract method for transforming input data based on the fitted model.

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

            Raises
            ------
            NotImplementedError
                This method must be implemented in subclasses.
        """
        pass

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

Forward the input data through the dimension reduction process.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to reduce.

required
device str

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

'cup'
*args

Additional arguments for the forward process.

()
**kwargs

Additional arguments for the forward process.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced input data.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Forward the input data through the dimension reduction process.

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

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

__init__(name='incremental_dimension_reduction', n_feature=None, incremental=True, *args, **kwargs)

Initialize the incremental dimension reduction class.

Parameters:

Name Type Description Default
name str

The name of the dimension reduction method. Default is 'incremental_dimension_reduction'.

'incremental_dimension_reduction'
n_feature int

The number of features to retain after reduction. Default is None.

None
incremental bool

Whether the dimension reduction is performed incrementally. Default is True.

True
*args

Additional arguments for subclass initialization.

()
**kwargs

Additional arguments for subclass initialization.

()
Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
def __init__(self, name: str = 'incremental_dimension_reduction', n_feature: int = None, incremental: bool = True, *args, **kwargs):
    """
        Initialize the incremental dimension reduction class.

        Parameters
        ----------
        name : str, optional
            The name of the dimension reduction method. Default is 'incremental_dimension_reduction'.
        n_feature : int, optional
            The number of features to retain after reduction. Default is None.
        incremental : bool, optional
            Whether the dimension reduction is performed incrementally. Default is True.
        *args, **kwargs
            Additional arguments for subclass initialization.
    """
    super().__init__(*args, **kwargs)
    self.name = name
    self.incremental = incremental
    self.n_feature = n_feature

fit(X, device='cup', *args, **kwargs) abstractmethod

Abstract method for fitting the model to the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to fit.

required
device str

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

'cup'
*args

Additional arguments for the fitting process.

()
**kwargs

Additional arguments for the fitting process.

()

Raises:

Type Description
NotImplementedError

This method must be implemented in subclasses.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
@abstractmethod
def fit(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Abstract method for fitting the model to the input data.

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

        Raises
        ------
        NotImplementedError
            This method must be implemented in subclasses.
    """
    pass

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

Fit the model to the input data and reduce its dimensionality.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to reduce.

required
device str

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

'cup'
*args

Additional arguments for the fit and transform processes.

()
**kwargs

Additional arguments for the fit and transform processes.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced input data.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Fit the model to the input data and reduce its dimensionality.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data to reduce.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cup'.
        *args, **kwargs
            Additional arguments for the fit and transform processes.

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

    self.fit(X=input_X, device=device, *args, **kwargs)
    X_reduced = self.transform(X=input_X, device=device, *args, **kwargs)

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

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

Perform dimension reduction on the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to reduce.

required
device str

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

'cup'
*args

Additional arguments for the forward process.

()
**kwargs

Additional arguments for the forward process.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced input data.

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

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

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The reduced input data.
    """
    if isinstance(X, torch.Tensor):
        input_X = X.detach().cpu().numpy()  # Convert torch.Tensor to numpy
    else:
        input_X = X
    X_reduced = self.fit_transform(X=input_X, device=device, *args, **kwargs)
    return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

get_n_feature()

Retrieve the number of features to retain.

Returns:

Type Description
int

The number of features to retain.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
def get_n_feature(self):
    """
        Retrieve the number of features to retain.

        Returns
        -------
        int
            The number of features to retain.
    """
    return self.n_feature

set_n_feature(n_feature)

Set the number of features to retain.

Parameters:

Name Type Description Default
n_feature int

The number of features to retain.

required
Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
def set_n_feature(self, n_feature):
    """
        Set the number of features to retain.

        Parameters
        ----------
        n_feature : int
            The number of features to retain.
    """
    self.n_feature = n_feature

transform(X, device='cup', *args, **kwargs) abstractmethod

Abstract method for transforming input data based on the fitted model.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to transform.

required
device str

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

'cup'
*args

Additional arguments for the transformation process.

()
**kwargs

Additional arguments for the transformation process.

()

Raises:

Type Description
NotImplementedError

This method must be implemented in subclasses.

Source code in tinybig/koala/machine_learning/dimension_reduction/incremental_dimension_reduction.py
@abstractmethod
def transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Abstract method for transforming input data based on the fitted model.

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

        Raises
        ------
        NotImplementedError
            This method must be implemented in subclasses.
    """
    pass