SelfAdaptivePINN#
- class SelfAdaptivePINN(problem, model, weight_function=Sigmoid(), optimizer_model=None, optimizer_weights=None, scheduler_model=None, scheduler_weights=None, weighting=None, loss=None)[source]#
Bases:
PINNInterface
,MultiSolverInterface
Self-Adaptive Physics-Informed Neural Network (SelfAdaptivePINN) solver class. This class implements the Self-Adaptive Physics-Informed Neural Network solver, using a user specified
model
to solve a specificproblem
. It can be used to solve both forward and inverse problems.The Self-Adapive Physics-Informed Neural Network solver aims to find the solution \(\mathbf{u}:\Omega\rightarrow\mathbb{R}^m\) of a differential problem:
\[\begin{split}\begin{cases} \mathcal{A}[\mathbf{u}](\mathbf{x})=0\quad,\mathbf{x}\in\Omega\\ \mathcal{B}[\mathbf{u}](\mathbf{x})=0\quad, \mathbf{x}\in\partial\Omega \end{cases}\end{split}\]integrating pointwise loss evaluation using a mask :math:m and self-adaptive weights, which allow the model to focus on regions of the domain where the residual is higher.
The loss function to solve the problem is
\[\mathcal{L}_{\rm{problem}} = \frac{1}{N} \sum_{i=1}^{N_\Omega} m \left( \lambda_{\Omega}^{i} \right) \mathcal{L} \left( \mathcal{A} [\mathbf{u}](\mathbf{x}) \right) + \frac{1}{N} \sum_{i=1}^{N_{\partial\Omega}} m \left( \lambda_{\partial\Omega}^{i} \right) \mathcal{L} \left( \mathcal{B}[\mathbf{u}](\mathbf{x}) \right),\]denoting the self adaptive weights as \(\lambda_{\Omega}^1, \dots, \lambda_{\Omega}^{N_\Omega}\) and \(\lambda_{\partial \Omega}^1, \dots, \lambda_{\Omega}^{N_\partial \Omega}\) for \(\Omega\) and \(\partial \Omega\), respectively.
The Self-Adaptive Physics-Informed Neural Network solver identifies the solution and appropriate self adaptive weights by solving the following optimization problem:
\[\min_{w} \max_{\lambda_{\Omega}^k, \lambda_{\partial \Omega}^s} \mathcal{L} ,\]where \(w\) denotes the network parameters, and \(\mathcal{L}\) is a specific loss function, , typically the MSE:
\[\mathcal{L}(v) = \| v \|^2_2.\]See also
Original reference: McClenny, Levi D., and Ulisses M. Braga-Neto. Self-adaptive physics-informed neural networks. Journal of Computational Physics 474 (2023): 111722. DOI: 10.1016/j.jcp.2022.111722.
Initialization of the
SelfAdaptivePINN
class.- Parameters:
problem (AbstractProblem) – The problem to be solved.
model (torch.nn.Module) – The model to be used.
weight_function (torch.nn.Module) – The Self-Adaptive mask model. Default is
torch.nn.Sigmoid()
.optimizer_model (Optimizer) – The optimizer of the
model
. IfNone
, thetorch.optim.Adam
optimizer is used. Default isNone
.optimizer_weights (Optimizer) – The optimizer of the
weight_function
. IfNone
, thetorch.optim.Adam
optimizer is used. Default isNone
.scheduler_model (Scheduler) – Learning rate scheduler for the
model
. IfNone
, thetorch.optim.lr_scheduler.ConstantLR
scheduler is used. Default isNone
.scheduler_weights (Scheduler) – Learning rate scheduler for the
weight_function
. IfNone
, thetorch.optim.lr_scheduler.ConstantLR
scheduler is used. Default isNone
.weighting (WeightingInterface) – The weighting schema to be used. If
None
, no weighting schema is used. Default isNone
.loss (torch.nn.Module) – The loss function to be minimized. If
None
, thetorch.nn.MSELoss
loss is used. Default isNone
.
- forward(x)[source]#
Forward pass.
- Parameters:
x (LabelTensor) – Input tensor.
- Returns:
The output of the neural network.
- Return type:
- training_step(batch)[source]#
Solver training step, overridden to perform manual optimization.
- on_train_batch_end(outputs, batch, batch_idx)[source]#
This method is called at the end of each training batch and overrides the PyTorch Lightning implementation to log checkpoints.
- on_train_start()[source]#
This method is called at the start of the training process to set the self-adaptive weights as parameters of the mask model.
- Raises:
NotImplementedError – If the batch size is not
None
.
- on_load_checkpoint(checkpoint)[source]#
Override of the Pytorch Lightning
on_load_checkpoint
method to handle checkpoints for Self-Adaptive Weights. This method should not be overridden, if not intentionally.- Parameters:
checkpoint (dict) – Pytorch Lightning checkpoint dict.
- loss_phys(samples, equation)[source]#
Computes the physics loss for the physics-informed solver based on the provided samples and equation.
- Parameters:
samples (LabelTensor) – The samples to evaluate the physics loss.
equation (EquationInterface) – The governing equation.
- Returns:
The computed physics loss.
- Return type:
- property model#
The model.
- Returns:
The model.
- Return type:
- property weights_dict#
The self-adaptive weights.
- Returns:
The self-adaptive weights.
- Return type:
- property scheduler_model#
The scheduler associated to the model.
- Returns:
The scheduler for the model.
- Return type:
- property scheduler_weights#
The scheduler associated to the mask model.
- Returns:
The scheduler for the mask model.
- Return type:
- property optimizer_model#
Returns the optimizer associated to the model.
- Returns:
The optimizer for the model.
- Return type: