Neural Networks¶
Model Factory¶
PINNModel ¶
Bases: BaseNetwork
Physics-Informed Neural Network model factory.
Builds the network named by config.model.architecture (fourier,
resnet, siren, attention, autoencoder, fno; anything
else gives feedforward) and stores it as self.model.
Initialize the PINN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
Configuration object with |
required |
device
|
Optional device to override config device |
None
|
|
**kwargs
|
Ignored. |
{}
|
Source code in pinnrl/neural_networks/__init__.py
forward ¶
Forward pass of the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input tensor of shape (batch_size, input_dim) |
required |
Returns:
| Type | Description |
|---|---|
|
Output tensor of shape (batch_size, output_dim) |
Base¶
BaseNetwork ¶
Bases: Module
Base class for all neural network architectures.
Initialize the base network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration mapping with |
required |
Source code in pinnrl/neural_networks/base_network.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
save_state ¶
Save model state with architecture information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to save the model |
required |
Source code in pinnrl/neural_networks/base_network.py
load_state ¶
Load model state with architecture information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to load the model from |
required |
Source code in pinnrl/neural_networks/base_network.py
FeedForward¶
FeedForwardNetwork ¶
Bases: BaseNetwork
Simple feed-forward neural network with configurable hidden layers.
Initialize the feed-forward network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing: - input_dim: Input dimension - hidden_dims: List of hidden layer dimensions - output_dim: Output dimension - activation: Activation function name ('relu', 'tanh', etc.; default 'relu') - dropout: Dropout rate (default 0.1) - layer_norm: Whether to use layer normalization (default True) - device: Device to place the model on |
required |
Source code in pinnrl/neural_networks/feedforward.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
Source code in pinnrl/neural_networks/feedforward.py
ResNet¶
ResNet ¶
Bases: BaseNetwork
Residual Neural Network architecture built from multiple ResNetBlock components.
Initialize the ResNet architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing:
- input_dim: Input dimension
- hidden_dim: Hidden layer dimension (else |
required |
Source code in pinnrl/neural_networks/resnet.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
Source code in pinnrl/neural_networks/resnet.py
ResNetBlock ¶
Bases: Module
Residual block act(x + F(x)).
F is Linear -> LayerNorm -> act -> Dropout -> Linear -> LayerNorm -> Dropout.
Initialize a single residual block component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim
|
int
|
Input dimension |
required |
hidden_dim
|
int
|
Hidden dimension |
required |
activation
|
str
|
Activation function name |
'relu'
|
dropout
|
float
|
Dropout rate |
0.1
|
Source code in pinnrl/neural_networks/resnet.py
forward ¶
Forward pass with residual connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor |
SIREN¶
SIREN ¶
Bases: BaseNetwork
Sinusoidal Representation Network: SIRENLayer stack and a linear output layer.
Initialize the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing:
- input_dim: Input dimension
- hidden_dims: List of hidden layer dimensions
- output_dim: Output dimension
- omega_0: Frequency parameter (default 30.0); |
required |
Source code in pinnrl/neural_networks/siren.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
Source code in pinnrl/neural_networks/siren.py
SIRENLayer ¶
Bases: Module
SIREN layer sin(omega_0 * (W x + b)).
Initialize the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Input dimension |
required |
out_features
|
int
|
Output dimension |
required |
omega_0
|
float
|
Frequency parameter |
30.0
|
Source code in pinnrl/neural_networks/siren.py
initialize_weights ¶
Draw W ~ U(-sqrt(6 / in) / omega_0, sqrt(6 / in) / omega_0).
This is the SIREN hidden-layer rule; it is used for every layer,
including the first (the paper's first-layer U(-1/in, 1/in) is not
applied). Biases keep PyTorch's default nn.Linear initialization.
Source code in pinnrl/neural_networks/siren.py
forward ¶
Forward pass of the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor |
Fourier¶
FourierNetwork ¶
Bases: BaseNetwork
MLP on random Fourier features: [sin(xB), cos(xB)] -> hidden layers -> output.
Initialize the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing:
- input_dim: Input dimension
- mapping_size: Size of the Fourier feature mapping (default 32)
- hidden_dims: Width of each hidden layer (one layer per entry,
like the feedforward network); falls back to
|
required |
Source code in pinnrl/neural_networks/fourier.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
Source code in pinnrl/neural_networks/fourier.py
FourierFeatures ¶
Bases: Module
Random Fourier feature map [sin(xB), cos(xB)] with fixed B ~ N(0, scale^2).
B has shape (input_dim, mapping_size) and is a buffer, not trained;
there is no 2Ï€ factor.
Initialize Fourier features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Input dimension |
required |
mapping_size
|
int
|
Number of frequencies (output has |
required |
scale
|
float
|
Standard deviation of the entries of |
10.0
|
device
|
device | None
|
Device to place the features on (default: MPS if available, else CPU) |
None
|
Source code in pinnrl/neural_networks/fourier.py
forward ¶
Forward pass with Fourier feature mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in pinnrl/neural_networks/fourier.py
Fourier Neural Operator (FNO)¶
FNONetwork ¶
Bases: BaseNetwork
Fourier Neural Operator network for point-wise PINN training.
Architecture
Input(input_dim) -> Lift(Linear-GELU-Linear, hidden_dim) -> [FNOBlock(hidden_dim)] x num_blocks -> Project(Linear-GELU-Linear, output_dim)
Config keys: input_dim, output_dim, hidden_dim (default 128),
num_blocks (default 4), modes (default 16) and activation of
the blocks (default gelu).
Source code in pinnrl/neural_networks/fno.py
forward ¶
Map (x, t) points to the solution through the spectral blocks.
Source code in pinnrl/neural_networks/fno.py
FNOBlock ¶
Bases: Module
Single FNO layer: act(LayerNorm(spectral(x) + linear(x) + x)).
Unknown activation names fall back to GELU.
Source code in pinnrl/neural_networks/fno.py
forward ¶
Forward: spectral path + linear bypass with residual connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
(batch, channels) tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
(batch, channels) tensor |
Source code in pinnrl/neural_networks/fno.py
SpectralConv1d ¶
Bases: Module
1D spectral convolution layer operating in Fourier space.
Treats the feature/channel dimension as a 1D signal::
X = rfft(x) # (batch, channels // 2 + 1)
Y[:, n] = sum_{m < M} X[:, m] * W[m, n] # n < M; Y = 0 for n >= M
y = irfft(Y, n=channels)
with M = min(modes, channels // 2 + 1) and W a learnable complex
(M, M) matrix (initialised N(0, 1) / (channels * M) per real and
imaginary part), so the kept modes are mixed, not scaled one by one.
Source code in pinnrl/neural_networks/fno.py
forward ¶
Apply spectral convolution along the feature dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
(batch, channels) tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
(batch, channels) tensor |
Source code in pinnrl/neural_networks/fno.py
Attention¶
AttentionNetwork ¶
Bases: BaseNetwork
Neural network with self-attention layers.
Initialize the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing: - input_dim: Input dimension - hidden_dim: Hidden dimension - output_dim: Output dimension - num_layers: Number of SelfAttention + FeedForwardBlock pairs - num_heads: Number of attention heads (must divide hidden_dim) - dropout: Dropout rate - activation: Activation after the input projection only |
required |
Source code in pinnrl/neural_networks/attention.py
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Output tensor or array |
Source code in pinnrl/neural_networks/attention.py
SelfAttention ¶
Bases: Module
Multi-head self-attention over a length-1 sequence, plus residual and LayerNorm.
Each point is its own sequence, so the softmax weight is always 1 and,
without attention dropout, the layer computes LayerNorm(x + proj(value(x))).
Initialize the attention layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Input dimension |
required |
heads
|
int
|
Number of attention heads |
4
|
dropout
|
float
|
Dropout rate |
0.1
|
Source code in pinnrl/neural_networks/attention.py
forward ¶
Forward pass of the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, dim) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of shape (batch_size, dim) |
Source code in pinnrl/neural_networks/attention.py
AutoEncoder¶
AutoEncoder ¶
Bases: BaseNetwork
Encoder-decoder MLP: input -> hidden_dims -> latent -> reversed(hidden_dims) -> output.
It maps coordinates to the solution; it does not reconstruct its input.
Initialize the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
NetworkConfig
|
Network configuration dictionary containing: - input_dim: Input dimension - latent_dim: Latent space dimension - hidden_dims: Encoder hidden widths; the decoder uses them reversed - output_dim: Output dimension (default 1) - activation: Activation function name - dropout: Dropout rate (default 0.0) - layer_norm: LayerNorm after each hidden linear layer (default True) |
required |
Source code in pinnrl/neural_networks/autoencoder.py
encode ¶
Encode input to latent space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Latent space representation |
Source code in pinnrl/neural_networks/autoencoder.py
decode ¶
Decode latent space to output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Tensor
|
Latent space tensor |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Decoded output |
forward ¶
Forward pass of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
InputType
|
Input tensor or array |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Predicted solution, shape |