Fourier Neural Operators¶
spectrans.layers.operators.fno ¶
Fourier Neural Operator (FNO) implementation for spectral transformers.
Provides the Fourier Neural Operator, which learns mappings between infinite-dimensional function spaces by parameterizing integral kernels in the Fourier domain. FNO is useful for learning solution operators for partial differential equations and other continuous transformations.
The FNO architecture combines spectral convolutions in the Fourier domain with pointwise linear transformations, learning global dependencies while maintaining resolution invariance.
Classes:
| Name | Description |
|---|---|
FourierNeuralOperator |
Base FNO layer for learning operators in function spaces. |
FNOBlock |
Complete FNO block with spectral convolution and feedforward network. |
SpectralConv1d |
1D spectral convolution operator. |
SpectralConv2d |
2D spectral convolution operator for image-like data. |
Examples:
Basic FNO layer:
>>> import torch
>>> from spectrans.layers.operators.fno import FourierNeuralOperator
>>> fno = FourierNeuralOperator(hidden_dim=64, modes=16)
>>> x = torch.randn(32, 128, 64) # (batch, sequence, channels)
>>> output = fno(x)
>>> assert output.shape == x.shape
FNO block with residual connection:
>>> from spectrans.layers.operators.fno import FNOBlock
>>> block = FNOBlock(hidden_dim=64, modes=16, mlp_ratio=2.0)
>>> x = torch.randn(32, 128, 64)
>>> output = block(x)
2D spectral convolution for images:
>>> from spectrans.layers.operators.fno import SpectralConv2d
>>> conv2d = SpectralConv2d(in_channels=3, out_channels=64, modes=(32, 32))
>>> image = torch.randn(32, 3, 256, 256)
>>> output = conv2d(image)
Notes
The FNO learns the kernel \(\mathcal{K}\) in the integral operator:
By parameterizing \(k\) in the Fourier domain as \(\mathbf{R}_{\theta}\), the convolution becomes:
This allows computation via FFT and learnable complex weights \(\mathbf{R}_{\theta}\) that are truncated to retain only the lowest frequency modes.
References
Zongyi Li, Nikola Kovachki, Kamyar Azizzadenesheli, Burigede Liu, Kaushik Bhattacharya, Andrew Stuart, and Anima Anandkumar. 2021. Fourier neural operator for parametric partial differential equations. In Proceedings of the International Conference on Learning Representations (ICLR).
John Guibas, Morteza Mardani, Zongyi Li, Andrew Tao, Anima Anandkumar, and Bryan Catanzaro. 2022. Adaptive Fourier neural operators: Efficient token mixers for transformers. In Proceedings of the International Conference on Learning Representations (ICLR).
See Also
spectrans.layers.mixing.afno : Adaptive FNO mixing layer. spectrans.transforms.fourier : Underlying FFT implementations.
Classes¶
SpectralConv1d ¶
Bases: Module
1D Spectral convolution layer.
Performs convolution in the Fourier domain by element-wise multiplication with learnable complex-valued weights on truncated modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels. |
required |
out_channels
|
int
|
Number of output channels. |
required |
modes
|
int
|
Number of Fourier modes to keep (frequency truncation). |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
in_channels |
int
|
Input channel count. |
out_channels |
int
|
Output channel count. |
modes |
int
|
Number of retained Fourier modes. |
weights |
Parameter
|
Complex-valued learnable weights of shape (in_channels, out_channels, modes). |
Examples:
>>> conv = SpectralConv1d(in_channels=64, out_channels=64, modes=16)
>>> x = torch.randn(32, 64, 128) # (batch, channels, sequence)
>>> output = conv(x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
forward |
Apply spectral convolution. |
Source code in spectrans/layers/operators/fno.py
Functions¶
forward ¶
Apply spectral convolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, in_channels, sequence_length). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of shape (batch_size, out_channels, sequence_length). |
Source code in spectrans/layers/operators/fno.py
SpectralConv2d ¶
Bases: Module
2D Spectral convolution layer.
Performs 2D convolution in the Fourier domain for image-like data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels. |
required |
out_channels
|
int
|
Number of output channels. |
required |
modes
|
tuple[int, int]
|
Number of Fourier modes to keep in each dimension (height, width). |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
in_channels |
int
|
Input channel count. |
out_channels |
int
|
Output channel count. |
modes1 |
int
|
Number of retained modes in first spatial dimension. |
modes2 |
int
|
Number of retained modes in second spatial dimension. |
weights |
Parameter
|
Complex weights of shape (in_channels, out_channels, modes1, modes2). |
Examples:
>>> conv2d = SpectralConv2d(in_channels=3, out_channels=64, modes=(32, 32))
>>> x = torch.randn(8, 3, 256, 256)
>>> output = conv2d(x)
>>> assert output.shape == (8, 64, 256, 256)
Methods:
| Name | Description |
|---|---|
forward |
Apply 2D spectral convolution. |
Source code in spectrans/layers/operators/fno.py
Functions¶
forward ¶
Apply 2D spectral convolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, in_channels, height, width). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of shape (batch_size, out_channels, height, width). |
Source code in spectrans/layers/operators/fno.py
FourierNeuralOperator ¶
FourierNeuralOperator(hidden_dim: int, modes: int | tuple[int, ...] = 16, activation: ActivationType = 'gelu', use_spectral_conv: bool = True, use_linear: bool = True)
Bases: SpectralComponent
Fourier Neural Operator layer for learning operators in function spaces.
This layer combines spectral convolution with pointwise linear transformations to learn mappings between function spaces efficiently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension (number of channels). |
required |
modes
|
int | tuple[int, ...]
|
Number of Fourier modes to retain. Can be an integer for 1D or tuple for higher dimensions. Default is 16. |
16
|
activation
|
str
|
Activation function. Options: 'gelu', 'relu', 'tanh'. Default is 'gelu'. |
'gelu'
|
use_spectral_conv
|
bool
|
Whether to use spectral convolution. Default is True. |
True
|
use_linear
|
bool
|
Whether to use pointwise linear transformation. Default is True. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
hidden_dim |
int
|
Hidden dimension size. |
modes |
int | tuple[int, ...]
|
Number of retained Fourier modes. |
spectral_conv |
SpectralConv1d | SpectralConv2d | None
|
Spectral convolution layer if enabled. |
linear |
Conv1d | Conv2d | None
|
Pointwise convolution layer if enabled. |
activation |
Module
|
Activation function. |
Examples:
>>> fno = FourierNeuralOperator(hidden_dim=64, modes=16)
>>> x = torch.randn(32, 128, 64) # (batch, sequence, channels)
>>> output = fno(x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
forward |
Apply Fourier Neural Operator. |
Source code in spectrans/layers/operators/fno.py
Functions¶
forward ¶
Apply Fourier Neural Operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Shape depends on dimensionality: - 1D: (batch_size, sequence_length, hidden_dim) - 2D: (batch_size, height, width, hidden_dim) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of same shape as input. |
Source code in spectrans/layers/operators/fno.py
FNOBlock ¶
FNOBlock(hidden_dim: int, modes: int | tuple[int, ...] = 16, mlp_ratio: float = 2.0, activation: ActivationType = 'gelu', dropout: float = 0.0, norm_type: NormType = 'layernorm')
Bases: SpectralComponent
Complete FNO block with spectral convolution and feedforward network.
This block combines the FNO layer with layer normalization, residual connections, and an optional feedforward network for a complete transformer-like block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. |
required |
modes
|
int | tuple[int, ...]
|
Number of Fourier modes to retain. Default is 16. |
16
|
mlp_ratio
|
float
|
Expansion ratio for feedforward network. Default is 2.0. |
2.0
|
activation
|
str
|
Activation function. Default is 'gelu'. |
'gelu'
|
dropout
|
float
|
Dropout probability. Default is 0.0. |
0.0
|
norm_type
|
str
|
Normalization type: 'layer' or 'batch'. Default is 'layer'. |
'layernorm'
|
Attributes:
| Name | Type | Description |
|---|---|---|
hidden_dim |
int
|
Hidden dimension size. |
fno |
FourierNeuralOperator
|
FNO layer for spectral convolution. |
norm1 |
Module
|
First normalization layer. |
norm2 |
Module | None
|
Second normalization layer (if FFN is used). |
ffn |
Sequential | None
|
Feedforward network. |
dropout |
Dropout
|
Dropout layer. |
Examples:
>>> block = FNOBlock(hidden_dim=64, modes=16, mlp_ratio=2.0)
>>> x = torch.randn(32, 128, 64)
>>> output = block(x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
forward |
Apply FNO block. |
Source code in spectrans/layers/operators/fno.py
Functions¶
forward ¶
Apply FNO block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of same shape as input. |