|
| 1 | +"""Module for the PirateNet model class.""" |
| 2 | + |
| 3 | +import torch |
| 4 | +from .block import FourierFeatureEmbedding, PirateNetBlock |
| 5 | +from ..utils import check_consistency, check_positive_integer |
| 6 | + |
| 7 | + |
| 8 | +class PirateNet(torch.nn.Module): |
| 9 | + """ |
| 10 | + Implementation of Physics-Informed residual adaptive network (PirateNet). |
| 11 | +
|
| 12 | + The model consists of a Fourier feature embedding layer, multiple PirateNet |
| 13 | + blocks, and a final output layer. Each PirateNet block consist of three |
| 14 | + dense layers with dual gating mechanism and an adaptive residual connection, |
| 15 | + whose contribution is controlled by a trainable parameter ``alpha``. |
| 16 | +
|
| 17 | + The PirateNet, augmented with random weight factorization, is designed to |
| 18 | + mitigate spectral bias in deep networks. |
| 19 | +
|
| 20 | + .. seealso:: |
| 21 | +
|
| 22 | + **Original reference**: |
| 23 | + Wang, S., Sankaran, S., Stinis., P., Perdikaris, P. (2025). |
| 24 | + *Simulating Three-dimensional Turbulence with Physics-informed Neural |
| 25 | + Networks*. |
| 26 | + DOI: `arXiv preprint arXiv:2507.08972. |
| 27 | + <https://arxiv.org/abs/2507.08972>`_ |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + input_dimension, |
| 33 | + inner_size, |
| 34 | + output_dimension, |
| 35 | + embedding=None, |
| 36 | + n_layers=3, |
| 37 | + activation=torch.nn.Tanh, |
| 38 | + ): |
| 39 | + """ |
| 40 | + Initialization of the :class:`PirateNet` class. |
| 41 | +
|
| 42 | + :param int input_dimension: The number of input features. |
| 43 | + :param int inner_size: The number of hidden units in the dense layers. |
| 44 | + :param int output_dimension: The number of output features. |
| 45 | + :param torch.nn.Module embedding: The embedding module used to transform |
| 46 | + the input into a higher-dimensional feature space. If ``None``, a |
| 47 | + default :class:`~pina.model.block.FourierFeatureEmbedding` with |
| 48 | + scaling factor of 2 is used. Default is ``None``. |
| 49 | + :param int n_layers: The number of PirateNet blocks in the model. |
| 50 | + Default is 3. |
| 51 | + :param torch.nn.Module activation: The activation function to be used in |
| 52 | + the blocks. Default is :class:`torch.nn.Tanh`. |
| 53 | + """ |
| 54 | + super().__init__() |
| 55 | + |
| 56 | + # Check consistency |
| 57 | + check_consistency(activation, torch.nn.Module, subclass=True) |
| 58 | + check_positive_integer(input_dimension, strict=True) |
| 59 | + check_positive_integer(inner_size, strict=True) |
| 60 | + check_positive_integer(output_dimension, strict=True) |
| 61 | + check_positive_integer(n_layers, strict=True) |
| 62 | + |
| 63 | + # Initialize the activation function |
| 64 | + self.activation = activation() |
| 65 | + |
| 66 | + # Initialize the Fourier embedding |
| 67 | + self.embedding = embedding or FourierFeatureEmbedding( |
| 68 | + input_dimension=input_dimension, |
| 69 | + output_dimension=inner_size, |
| 70 | + sigma=2.0, |
| 71 | + ) |
| 72 | + |
| 73 | + # Initialize the shared dense layers |
| 74 | + self.linear1 = torch.nn.Linear(inner_size, inner_size) |
| 75 | + self.linear2 = torch.nn.Linear(inner_size, inner_size) |
| 76 | + |
| 77 | + # Initialize the PirateNet blocks |
| 78 | + self.blocks = torch.nn.ModuleList( |
| 79 | + [PirateNetBlock(inner_size, activation) for _ in range(n_layers)] |
| 80 | + ) |
| 81 | + |
| 82 | + # Initialize the output layer |
| 83 | + self.output_layer = torch.nn.Linear(inner_size, output_dimension) |
| 84 | + |
| 85 | + def forward(self, input_): |
| 86 | + """ |
| 87 | + Forward pass of the PirateNet model. It applies the Fourier feature |
| 88 | + embedding, computes the shared gating tensors U and V, and passes the |
| 89 | + input through each block in the network. Finally, it applies the output |
| 90 | + layer to produce the final output. |
| 91 | +
|
| 92 | + :param input_: The input tensor for the model. |
| 93 | + :type input_: torch.Tensor | LabelTensor |
| 94 | + :return: The output tensor of the model. |
| 95 | + :rtype: torch.Tensor | LabelTensor |
| 96 | + """ |
| 97 | + # Apply the Fourier feature embedding |
| 98 | + x = self.embedding(input_) |
| 99 | + |
| 100 | + # Compute U and V from the shared dense layers |
| 101 | + U = self.activation(self.linear1(x)) |
| 102 | + V = self.activation(self.linear2(x)) |
| 103 | + |
| 104 | + # Pass through each block in the network |
| 105 | + for block in self.blocks: |
| 106 | + x = block(x, U, V) |
| 107 | + |
| 108 | + return self.output_layer(x) |
| 109 | + |
| 110 | + @property |
| 111 | + def alpha(self): |
| 112 | + """ |
| 113 | + Return the alpha values of all PirateNetBlock layers. |
| 114 | +
|
| 115 | + :return: A list of alpha values from each block. |
| 116 | + :rtype: list |
| 117 | + """ |
| 118 | + return [block.alpha.item() for block in self.blocks] |
0 commit comments