Skip to content

taylor_expansion

Bases: transformation

The taylor's data expansion function.

It performs the taylor's 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 taylor's expansion will be $$ \begin{equation} \kappa (\mathbf{x} | d) = [P_1(\mathbf{x}), P_2(\mathbf{x}), \cdots, P_d(\mathbf{x}) ] \in {R}^D. \end{equation} $$ where \(P_d(\mathbf{x})\) denotes the taylor's expansion of \(\mathbf{x}\) of degree \(d\). The output dimension will then be \(D = \sum_{i=1}^d m^i\).

Specifically, \(P_d(\mathbf{x})\) can be recursively defined as follows: $$ \begin{align} P_0(\mathbf{x}) &= [1] \in {R}^{1},\\ P_1(\mathbf{x}) &= [x_1, x_2, \cdots, x_m] \in {R}^{m},\\ P_d(\mathbf{x}) &= P_1(\mathbf{x}) \otimes P_{d-1}(\mathbf{x}) \text{, for } \forall d \ge 2. \end{align} $$

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

Attributes:

Name Type Description
name str, default = 'taylor_expansion'

Name of the expansion function.

d int, default = 2

Degree of taylor's 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/polynomial_expansion.py
class taylor_expansion(transformation):
    r"""
    The taylor's data expansion function.

    It performs the taylor's 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 taylor's expansion will be
    $$
        \begin{equation}
            \kappa (\mathbf{x} | d) = [P\_1(\mathbf{x}), P\_2(\mathbf{x}), \cdots, P\_d(\mathbf{x}) ] \in {R}^D.
        \end{equation}
    $$
    where $P_d(\mathbf{x})$ denotes the taylor's expansion of $\mathbf{x}$ of degree $d$. The output dimension will then be $D = \sum_{i=1}^d m^i$.

    Specifically, $P_d(\mathbf{x})$ can be recursively defined as follows:
    $$
        \begin{align}
            P\_0(\mathbf{x}) &= [1] \in {R}^{1},\\\\
            P\_1(\mathbf{x}) &= [x\_1, x\_2, \cdots, x\_m] \in {R}^{m},\\\\
            P\_d(\mathbf{x}) &= P\_1(\mathbf{x}) \otimes P\_{d-1}(\mathbf{x}) \text{, for } \forall d \ge 2.
        \end{align}
    $$

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

    Attributes
    ----------
    name: str, default = 'taylor_expansion'
        Name of the expansion function.
    d: int, default = 2
        Degree of taylor's 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='taylor_expansion', d=2, *args, **kwargs):
        r"""
        The initialization method of taylor's expansion function.

        It initializes a taylor's 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 = 'taylor_expansion'
            The name of the taylor's expansion function.
        d: int, default = 2
            The max degree of the taylor's expansion.

        Returns
        ----------
        transformation
            The taylor's 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 taylor's expansion function, the expansion space dimension is determined by both m and d,
        which can be represented as:

        $$ D = \sum_{i=1}^d m^i. $$

        Notes
        ----------
        Taylor's expansion function will increase the expansion dimension exponentially and the degree parameter $d$
        is usually set with a small number. When the expansion dimension $D > 10^7$ (i.e., more than 10 million),
        the function will raise a warning reminder.

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

        Returns
        -------
        int
            The dimension of the expansion space.
        """
        D = sum([m**i for i in range(1, self.d+1)])
        if D > 10**7:
            warnings.warn('You have expanded the input data to a very high-dimensional representation, '
                          'with more than 10M features per instance...', UserWarning)
        return D

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

        It performs the taylor's data expansion of the input data and returns the expansion result
        according to the following equation:
        $$
        \begin{equation}
            \kappa (\mathbf{x} | d) = [P\_1(\mathbf{x}), P\_2(\mathbf{x}), \cdots, P\_d(\mathbf{x}) ] \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)

        x_powers = torch.ones(size=[x.size(0), 1]).to(device)
        expansion = torch.Tensor([]).to(device)

        for i in range(1, self.d+1):
            x_powers = torch.einsum('ba,bc->bac', x_powers.clone(), x).view(x_powers.size(0), x_powers.size(1)*x.size(1))
            expansion = torch.cat((expansion, x_powers), dim=1)

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

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

The initialization method of taylor's expansion function.

It initializes a taylor's 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

The name of the taylor's expansion function.

'taylor_expansion'
d

The max degree of the taylor's expansion.

2

Returns:

Type Description
transformation

The taylor's expansion function.

Source code in tinybig/expansion/polynomial_expansion.py
def __init__(self, name='taylor_expansion', d=2, *args, **kwargs):
    r"""
    The initialization method of taylor's expansion function.

    It initializes a taylor's 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 = 'taylor_expansion'
        The name of the taylor's expansion function.
    d: int, default = 2
        The max degree of the taylor's expansion.

    Returns
    ----------
    transformation
        The taylor's 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 taylor's expansion function, the expansion space dimension is determined by both m and d, which can be represented as:

\[ D = \sum_{i=1}^d m^i. \]
Notes

Taylor's expansion function will increase the expansion dimension exponentially and the degree parameter \(d\) is usually set with a small number. When the expansion dimension \(D > 10^7\) (i.e., more than 10 million), the function will raise a warning reminder.

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/polynomial_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 taylor's expansion function, the expansion space dimension is determined by both m and d,
    which can be represented as:

    $$ D = \sum_{i=1}^d m^i. $$

    Notes
    ----------
    Taylor's expansion function will increase the expansion dimension exponentially and the degree parameter $d$
    is usually set with a small number. When the expansion dimension $D > 10^7$ (i.e., more than 10 million),
    the function will raise a warning reminder.

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

    Returns
    -------
    int
        The dimension of the expansion space.
    """
    D = sum([m**i for i in range(1, self.d+1)])
    if D > 10**7:
        warnings.warn('You have expanded the input data to a very high-dimensional representation, '
                      'with more than 10M features per instance...', UserWarning)
    return D

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

The forward method of the data expansion function.

It performs the taylor's data expansion of the input data and returns the expansion result according to the following equation: $$ \begin{equation} \kappa (\mathbf{x} | d) = [P_1(\mathbf{x}), P_2(\mathbf{x}), \cdots, P_d(\mathbf{x}) ] \in {R}^D. \end{equation} $$

Parameters:

Name Type Description Default
x Tensor

The input data vector.

required
device

The device to perform the data expansion.

'cpu'

Returns:

Type Description
Tensor

The expanded data vector of the input.

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

    It performs the taylor's data expansion of the input data and returns the expansion result
    according to the following equation:
    $$
    \begin{equation}
        \kappa (\mathbf{x} | d) = [P\_1(\mathbf{x}), P\_2(\mathbf{x}), \cdots, P\_d(\mathbf{x}) ] \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)

    x_powers = torch.ones(size=[x.size(0), 1]).to(device)
    expansion = torch.Tensor([]).to(device)

    for i in range(1, self.d+1):
        x_powers = torch.einsum('ba,bc->bac', x_powers.clone(), x).view(x_powers.size(0), x_powers.size(1)*x.size(1))
        expansion = torch.cat((expansion, x_powers), dim=1)

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