Skip to content

fourier_expansion

Bases: transformation

The fourier data expansion function.

It performs the fourier 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\), based on the parameters \(P\) and \(N\), its fourier expansion will be $$ \begin{equation} \kappa (\mathbf{x} | P, N) = \left[ \cos (2\pi \frac{1}{P} \mathbf{x} ), \sin(2\pi \frac{1}{P} \mathbf{x} ), \cdots, \cos(2\pi \frac{N}{P} \mathbf{x} ), \sin(2\pi \frac{N}{P} \mathbf{x} ) \right] \in {R}^D, \end{equation} $$ where the output dimension \(D = 2 m N\).

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

Attributes:

Name Type Description
name str, default = 'fourier_expansion'

Name of the expansion function.

P int, default = 10

The period parameter of the expansion.

N int, default = 5

The harmonic number of the 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 fourier_expansion(transformation):
    r"""
    The fourier data expansion function.

    It performs the fourier 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$, based on the parameters $P$ and $N$, its fourier expansion will be
    $$
        \begin{equation}
            \kappa (\mathbf{x} | P, N) = \left[ \cos (2\pi \frac{1}{P} \mathbf{x} ), \sin(2\pi \frac{1}{P} \mathbf{x} ), \cdots, \cos(2\pi \frac{N}{P} \mathbf{x} ), \sin(2\pi \frac{N}{P} \mathbf{x} ) \right] \in {R}^D,
        \end{equation}
    $$
    where the output dimension $D = 2 m N$.

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

    Attributes
    ----------
    name: str, default = 'fourier_expansion'
        Name of the expansion function.
    P: int, default = 10
        The period parameter of the expansion.
    N: int, default = 5
        The harmonic number of the 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='fourier_expansion', P=10, N=5, *args, **kwargs):
        r"""
        The initialization method of fourier expansion function.

        It initializes a fourier 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 = 'fourier_expansion'
            The name of the fourier expansion function.
        P: int, default = 10
            The period parameter of the expansion.
        N: int, default = 5
            The harmonic number of the expansion.

        Returns
        ----------
        transformation
            The fourier expansion function.
        """
        super().__init__(name=name, *args, **kwargs)
        self.P = P
        self.N = N

    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 fourier expansion function, the expansion space dimension is determined by both m and N,
        which can be represented as:

        $$ D = 2 m N. $$

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

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

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

        It performs the fourier data expansion of the input data and returns the expansion result
        according to the following equation:
        $$
        \begin{equation}
            \kappa (\mathbf{x} | P, N) = \left[ \cos (2\pi \frac{1}{P} \mathbf{x} ), \sin(2\pi \frac{1}{P} \mathbf{x} ), \cdots, \cos(2\pi \frac{N}{P} \mathbf{x} ), \sin(2\pi \frac{N}{P} \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.Tensor([]).to(device)
        for n in range(1, self.N+1):
            cos = torch.cos(2 * np.pi * (n / self.P) * x)
            sin = torch.sin(2 * np.pi * (n / self.P) * x)
            expansion = torch.cat((expansion, cos, sin), dim=1)

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

__init__(name='fourier_expansion', P=10, N=5, *args, **kwargs)

The initialization method of fourier expansion function.

It initializes a fourier 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 fourier expansion function.

'fourier_expansion'
P

The period parameter of the expansion.

10
N

The harmonic number of the expansion.

5

Returns:

Type Description
transformation

The fourier expansion function.

Source code in tinybig/expansion/polynomial_expansion.py
def __init__(self, name='fourier_expansion', P=10, N=5, *args, **kwargs):
    r"""
    The initialization method of fourier expansion function.

    It initializes a fourier 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 = 'fourier_expansion'
        The name of the fourier expansion function.
    P: int, default = 10
        The period parameter of the expansion.
    N: int, default = 5
        The harmonic number of the expansion.

    Returns
    ----------
    transformation
        The fourier expansion function.
    """
    super().__init__(name=name, *args, **kwargs)
    self.P = P
    self.N = N

calculate_D(m)

The expansion dimension calculation method.

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

\[ D = 2 m N. \]

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 fourier expansion function, the expansion space dimension is determined by both m and N,
    which can be represented as:

    $$ D = 2 m N. $$

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

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

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

The forward method of the data expansion function.

It performs the fourier data expansion of the input data and returns the expansion result according to the following equation: $$ \begin{equation} \kappa (\mathbf{x} | P, N) = \left[ \cos (2\pi \frac{1}{P} \mathbf{x} ), \sin(2\pi \frac{1}{P} \mathbf{x} ), \cdots, \cos(2\pi \frac{N}{P} \mathbf{x} ), \sin(2\pi \frac{N}{P} \mathbf{x} ) \right] \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 fourier data expansion of the input data and returns the expansion result
    according to the following equation:
    $$
    \begin{equation}
        \kappa (\mathbf{x} | P, N) = \left[ \cos (2\pi \frac{1}{P} \mathbf{x} ), \sin(2\pi \frac{1}{P} \mathbf{x} ), \cdots, \cos(2\pi \frac{N}{P} \mathbf{x} ), \sin(2\pi \frac{N}{P} \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.Tensor([]).to(device)
    for n in range(1, self.N+1):
        cos = torch.cos(2 * np.pi * (n / self.P) * x)
        sin = torch.sin(2 * np.pi * (n / self.P) * x)
        expansion = torch.cat((expansion, cos, sin), dim=1)

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