Neural Operators¶
spectrans.layers.operators ¶
Neural operator implementations for function space mappings.
Provides neural operators that learn mappings between infinite-dimensional function spaces rather than between finite-dimensional vectors. These operators are useful for learning solution operators for partial differential equations and other continuous transformations.
Neural operators parameterize integral kernels in the Fourier domain, computing global dependencies while maintaining resolution-invariant properties. Functions can be discretized at different resolutions during training and evaluation without retraining.
Modules:
| Name | Description |
|---|---|
fno |
Fourier Neural Operator implementations. |
Classes:
| Name | Description |
|---|---|
FourierNeuralOperator |
Base FNO layer implementing kernel learning in Fourier space. |
SpectralConv1d |
1D spectral convolution with learnable complex weights. |
SpectralConv2d |
2D spectral convolution for spatial data processing. |
FNOBlock |
FNO block with normalization and feedforward components. |
Examples:
Basic Fourier neural operator:
>>> import torch
>>> from spectrans.layers.operators import FourierNeuralOperator
>>> fno = FourierNeuralOperator(hidden_dim=64, modes=16)
>>> x = torch.randn(32, 128, 64)
>>> output = fno(x)
Spectral convolution for 2D problems:
>>> from spectrans.layers.operators import SpectralConv2d
>>> conv2d = SpectralConv2d(in_channels=3, out_channels=64, modes=(32, 32))
>>> spatial_data = torch.randn(32, 3, 256, 256)
>>> features = conv2d(spatial_data)
FNO block with residual connections:
>>> from spectrans.layers.operators import FNOBlock
>>> block = FNOBlock(hidden_dim=64, modes=16, mlp_ratio=2.0)
>>> processed = block(x)
Notes
Mathematical Foundation:
The Fourier Neural Operator learns to approximate the solution operator \(\mathcal{G}: \mathcal{A} \rightarrow \mathcal{U}\) that maps from input function space \(\mathcal{A}\) to output function space \(\mathcal{U}\).
For input function \(\mathbf{v}: \Omega \rightarrow \mathbb{R}^{d_v}\), the FNO layer computes:
The kernel operator \(\mathcal{K}_l\) is parameterized in Fourier space:
where \(\mathbf{R}_l(k) \in \mathbb{C}^{d \times d}\) are learnable complex weights and \(\mathcal{F}\) denotes the Fourier transform.
Spectral convolution applies this kernel by transforming input to Fourier domain \(\hat{\mathbf{v}} = \mathcal{F}[\mathbf{v}]\), applying learned kernel \(\hat{\mathbf{u}} = \mathbf{R} \cdot \hat{\mathbf{v}}\), and transforming back to spatial domain \(\mathbf{u} = \mathcal{F}^{-1}[\hat{\mathbf{u}}]\).
Time complexity is \(O(N d \log N + k d^2)\) where \(k\) is number of retained modes. Space complexity is \(O(k d^2)\) for learnable parameters. Resolution invariance allows the same weights to work for different discretizations.
Mode truncation keeping only low-frequency modes reduces computational cost from \(O(N^2)\) to \(O(N \log N)\), filters out high-frequency noise for generalization, and avoids overfitting to discretization artifacts for stability.
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).
See Also
spectrans.transforms.fourier : Underlying FFT implementations
spectrans.layers.mixing.afno : AFNO layers using similar principles
spectrans.utils.complex : Complex tensor operations
Classes¶
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. |
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
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). |