Skip to content

chebyshev_expansion

Bases: transformation

The chebyshev data expansion function.

It performs the chebyshev expansion of the input vector, and returns the expansion result. The class inherits from the base expansion class (i.e., the transformation class in the module directory).

...

Notes

For input vector \(\mathbf{x} \in R^m\), its chebyshev expansion up to degree \(d\) can be represented as $$ \begin{equation} \kappa(\mathbf{x} | d) = \left[ T_1(\mathbf{x}), T_2(\mathbf{x}) \cdots, T_d(\mathbf{x}) \right] \in {R}^D, \end{equation} $$ where \(T_d(\mathbf{x})\) denotes the chebyshev expansion polynomial of \(\mathbf{x}\) of degree \(d\). The output dimension of chebyshev expansion will be \(D = m d\).

As to the specific representations of chebyshev polynomials, they can be defined recursively based on the lower-degree terms according to the following equations:

(1) Base chebyshev polynomial with degree \(d=0\) and \(d=1\): $$ \begin{equation} T_0(x) = 1 \text{, and } T_1(x) = x. \end{equation} $$

(2) Higher-degree chebyshev polynomial with \(d \ge 2\): $$ \begin{equation} T_d(x) = 2x \cdot T_{d-1}(x) - T_{d-2}(x). \end{equation} $$

By default, the input and output can also be processed with the optional pre- or post-processing functions in the chebyshev expansion function.

Attributes:

Name Type Description
name str, default = 'chebyshev_expansion'

Name of the expansion function.

d int, default = 2

Degree of chebyshev expansion.

Methods:

Name Description
__init__

It performs the initialization of the expansion function.

calculate_D

It calculates the expansion space dimension D based on the input dimension parameter m.

forward

It implements the abstract forward method declared in the base expansion class.

Source code in tinybig/expansion/recursive_expansion.py
class chebyshev_expansion(transformation):
    r"""
    The chebyshev data expansion function.

    It performs the chebyshev expansion of the input vector, and returns the expansion result.
    The class inherits from the base expansion class (i.e., the transformation class in the module directory).

    ...

    Notes
    ----------
    For input vector $\mathbf{x} \in R^m$, its chebyshev expansion up to degree $d$ can be represented as
    $$
        \begin{equation}
            \kappa(\mathbf{x} | d) = \left[ T_1(\mathbf{x}), T_2(\mathbf{x}) \cdots, T_d(\mathbf{x}) \right] \in {R}^D,
        \end{equation}
    $$
    where $T_d(\mathbf{x})$ denotes the chebyshev expansion polynomial of $\mathbf{x}$ of degree $d$.
    The output dimension of chebyshev expansion will be $D = m d$.

    As to the specific representations of chebyshev polynomials, they can be defined recursively based on the
    lower-degree terms according to the following equations:

    (1) **Base chebyshev polynomial with degree $d=0$ and $d=1$:**
    $$
        \begin{equation}
            T_0(x) = 1 \text{, and } T_1(x) = x.
        \end{equation}
    $$

    (2) **Higher-degree chebyshev polynomial with $d \ge 2$:**
    $$
        \begin{equation}
            T_d(x) = 2x \cdot T_{d-1}(x) - T_{d-2}(x).
        \end{equation}
    $$

    By default, the input and output can also be processed with the optional pre- or post-processing functions
    in the chebyshev expansion function.

    Attributes
    ----------
    name: str, default = 'chebyshev_expansion'
        Name of the expansion function.
    d: int, default = 2
        Degree of chebyshev expansion.

    Methods
    ----------
    __init__
        It performs the initialization of the expansion function.

    calculate_D
        It calculates the expansion space dimension D based on the input dimension parameter m.

    forward
        It implements the abstract forward method declared in the base expansion class.

    """
    def __init__(self, name: str = 'chebyshev_polynomial_expansion', d: int = 2, *args, **kwargs):
        r"""
        The initialization method of chebyshev expansion function.

        It initializes a chebyshev expansion object based on the input function name.
        This method will also call the initialization method of the base class as well.

        Parameters
        ----------
        name: str, default = 'chebyshev_polynomial_expansion'
            The name of the chebyshev expansion function.
        d: int, default = 5
            The degree of the chebyshev expansion function.

        Returns
        ----------
        transformation
            The chebyshev expansion function.
        """
        super().__init__(name=name, *args, **kwargs)
        self.d = d

    def calculate_D(self, m: int):
        r"""
        The expansion dimension calculation method.

        It calculates the intermediate expansion space dimension based on the input dimension parameter m.
        For the chebyshev expansion function, the expansion space dimension is determined by m and d,
        which can be represented as:

        $$ D = m d. $$

        Parameters
        ----------
        m: int
            The dimension of the input space.

        Returns
        -------
        int
            The dimension of the expansion space.
        """
        return m * self.d

    def forward(self, x: torch.Tensor, device: str = 'cpu', *args, **kwargs):
        r"""
        The forward method of the data expansion function.

        It performs the chebyshev data expansion of the input data and returns the expansion result
        according to the following equation:
        $$
        \begin{equation}
            \kappa(\mathbf{x} | d) = \left[ T_1(\mathbf{x}), T_2(\mathbf{x}) \cdots, T_d(\mathbf{x}) \right] \in {R}^D.
        \end{equation}
        $$


        Parameters
        ----------
        x: torch.Tensor
            The input data vector.
        device: str, default = 'cpu'
            The device to perform the data expansion.

        Returns
        ----------
        torch.Tensor
            The expanded data vector of the input.
        """
        b, m = x.shape
        x = self.pre_process(x=x, device=device)
        expansion = torch.ones(size=[x.size(0), x.size(1), self.d+1]).to(device)
        if self.d > 0:
            expansion[:,:,1] = x
        for n in range(2, self.d+1):
            expansion[:, :, n] = 2 * x * expansion[:, :, n-1].clone() - expansion[:, :, n-2].clone()
        expansion = expansion[:, :, 1:].contiguous().view(x.size(0), -1)

        assert expansion.shape == (b, self.calculate_D(m=m))
        return self.post_process(x=expansion, device=device)

__init__(name='chebyshev_polynomial_expansion', d=2, *args, **kwargs)

The initialization method of chebyshev expansion function.

It initializes a chebyshev expansion object based on the input function name. This method will also call the initialization method of the base class as well.

Parameters:

Name Type Description Default
name str

The name of the chebyshev expansion function.

'chebyshev_polynomial_expansion'
d int

The degree of the chebyshev expansion function.

2

Returns:

Type Description
transformation

The chebyshev expansion function.

Source code in tinybig/expansion/recursive_expansion.py
def __init__(self, name: str = 'chebyshev_polynomial_expansion', d: int = 2, *args, **kwargs):
    r"""
    The initialization method of chebyshev expansion function.

    It initializes a chebyshev expansion object based on the input function name.
    This method will also call the initialization method of the base class as well.

    Parameters
    ----------
    name: str, default = 'chebyshev_polynomial_expansion'
        The name of the chebyshev expansion function.
    d: int, default = 5
        The degree of the chebyshev expansion function.

    Returns
    ----------
    transformation
        The chebyshev expansion function.
    """
    super().__init__(name=name, *args, **kwargs)
    self.d = d

calculate_D(m)

The expansion dimension calculation method.

It calculates the intermediate expansion space dimension based on the input dimension parameter m. For the chebyshev expansion function, the expansion space dimension is determined by m and d, which can be represented as:

\[ D = m d. \]

Parameters:

Name Type Description Default
m int

The dimension of the input space.

required

Returns:

Type Description
int

The dimension of the expansion space.

Source code in tinybig/expansion/recursive_expansion.py
def calculate_D(self, m: int):
    r"""
    The expansion dimension calculation method.

    It calculates the intermediate expansion space dimension based on the input dimension parameter m.
    For the chebyshev expansion function, the expansion space dimension is determined by m and d,
    which can be represented as:

    $$ D = m d. $$

    Parameters
    ----------
    m: int
        The dimension of the input space.

    Returns
    -------
    int
        The dimension of the expansion space.
    """
    return m * self.d

forward(x, device='cpu', *args, **kwargs)

The forward method of the data expansion function.

It performs the chebyshev data expansion of the input data and returns the expansion result according to the following equation: $$ \begin{equation} \kappa(\mathbf{x} | d) = \left[ T_1(\mathbf{x}), T_2(\mathbf{x}) \cdots, T_d(\mathbf{x}) \right] \in {R}^D. \end{equation} $$

Parameters:

Name Type Description Default
x Tensor

The input data vector.

required
device str

The device to perform the data expansion.

'cpu'

Returns:

Type Description
Tensor

The expanded data vector of the input.

Source code in tinybig/expansion/recursive_expansion.py
def forward(self, x: torch.Tensor, device: str = 'cpu', *args, **kwargs):
    r"""
    The forward method of the data expansion function.

    It performs the chebyshev data expansion of the input data and returns the expansion result
    according to the following equation:
    $$
    \begin{equation}
        \kappa(\mathbf{x} | d) = \left[ T_1(\mathbf{x}), T_2(\mathbf{x}) \cdots, T_d(\mathbf{x}) \right] \in {R}^D.
    \end{equation}
    $$


    Parameters
    ----------
    x: torch.Tensor
        The input data vector.
    device: str, default = 'cpu'
        The device to perform the data expansion.

    Returns
    ----------
    torch.Tensor
        The expanded data vector of the input.
    """
    b, m = x.shape
    x = self.pre_process(x=x, device=device)
    expansion = torch.ones(size=[x.size(0), x.size(1), self.d+1]).to(device)
    if self.d > 0:
        expansion[:,:,1] = x
    for n in range(2, self.d+1):
        expansion[:, :, n] = 2 * x * expansion[:, :, n-1].clone() - expansion[:, :, n-2].clone()
    expansion = expansion[:, :, 1:].contiguous().view(x.size(0), -1)

    assert expansion.shape == (b, self.calculate_D(m=m))
    return self.post_process(x=expansion, device=device)