Fourier Feature Embedding#

class FourierFeatureEmbedding(input_dimension, output_dimension, sigma)[source]#

Bases: Module

Fourier Feature Embedding class to encode the input features using random Fourier features.

This class applies a Fourier transformation to the input features, which can help in learning high-frequency variations in data. The class supports multiscale feature embedding, creating embeddings for each scale specified by the sigma parameter.

The Fourier Feature Embedding augments the input features as follows (3.10 of original paper):

\[\mathbf{x} \rightarrow \tilde{\mathbf{x}} = \left[ \cos\left( \mathbf{B} \mathbf{x} \right), \sin\left( \mathbf{B} \mathbf{x} \right)\right],\]

where \(\mathbf{B}_{ij} \sim \mathcal{N}(0, \sigma^2)\).

If multiple sigma are passed, the resulting embeddings are concateneted:

\[\mathbf{x} \rightarrow \tilde{\mathbf{x}} = \left[ \cos\left( \mathbf{B}^1 \mathbf{x} \right), \sin\left( \mathbf{B}^1 \mathbf{x} \right), \cos\left( \mathbf{B}^2 \mathbf{x} \right), \sin\left( \mathbf{B}^3 \mathbf{x} \right), \dots, \cos\left( \mathbf{B}^M \mathbf{x} \right), \sin\left( \mathbf{B}^M \mathbf{x} \right)\right],\]

where \(\mathbf{B}^k_{ij} \sim \mathcal{N}(0, \sigma_k^2) \quad k \in (1, \dots, M)\).

See also

Original reference: Wang, S., Wang, H., and Perdikaris, P. (2021). On the eigenvector bias of Fourier feature networks: From regression to solving multi-scale PDEs with physics-informed neural networks. Computer Methods in Applied Mechanics and Engineering 384 (2021): 113938. DOI: 10.1016/j.cma.2021.113938.

Initialization of the FourierFeatureEmbedding block.

Parameters:
  • input_dimension (int) – The dimension of the input tensor.

  • output_dimension (int) – The dimension of the output tensor. The output is obtained as a concatenation of cosine and sine embeddings.

  • sigma (float | int) – The standard deviation used for the Fourier Embedding. This value must reflect the granularity of the scale in the differential equation solution.

Raises:

RuntimeError – If the output dimension is not an even number.

forward(x)[source]#

Forward pass.

Parameters:

x (torch.Tensor | LabelTensor) – The input tensor.

Returns:

Fourier embedding of the input.

Return type:

torch.Tensor

property sigma#

The standard deviation used for the Fourier Embedding.

Returns:

The standard deviation used for the Fourier Embedding.

Return type:

float | int