fix rendering part 2

This commit is contained in:
giovanni
2025-03-14 00:10:18 +01:00
committed by Nicola Demo
parent e0ad4dc8a0
commit d2e3f458ab
17 changed files with 217 additions and 147 deletions

View File

@@ -53,14 +53,16 @@ class CausalPINN(PINN):
.. seealso::
**Original reference**: Wang, Sifan, Shyam Sankaran, and Paris
Perdikaris. "Respecting causality for training physics-informed
neural networks." Computer Methods in Applied Mechanics
and Engineering 421 (2024): 116813.
DOI `10.1016 <https://doi.org/10.1016/j.cma.2024.116813>`_.
Perdikaris.
*Respecting causality for training physics-informed
neural networks.*
Computer Methods in Applied Mechanics and Engineering 421 (2024):116813.
DOI: `10.1016 <https://doi.org/10.1016/j.cma.2024.116813>`_.
.. note::
This class is only compatible with problems that inherit from the
:class:`~pina.problem.TimeDependentProblem` class.
:class:`~pina.problem.time_dependent_problem.TimeDependentProblem`
class.
"""
def __init__(
@@ -77,17 +79,19 @@ class CausalPINN(PINN):
Initialization of the :class:`CausalPINN` class.
:param AbstractProblem problem: The problem to be solved. It must
inherit from at least :class:`~pina.problem.TimeDependentProblem`.
inherit from at least
:class:`~pina.problem.time_dependent_problem.TimeDependentProblem`.
:param torch.nn.Module model: The neural network model to be used.
:param torch.optim.Optimizer optimizer: The optimizer to be used
If `None`, the Adam optimizer is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler: Learning rate scheduler.
If `None`, the constant learning rate scheduler is used.
:param Optimizer optimizer: The optimizer to be used.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param torch.optim.LRScheduler scheduler: Learning rate scheduler.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
:param float eps: The exponential decay parameter. Default is ``100``.
:raises ValueError: If the problem is not a TimeDependentProblem.

View File

@@ -46,8 +46,8 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
.. seealso::
**Original reference**: Zeng, Qi, et al.
"Competitive physics informed networks." International Conference on
Learning Representations, ICLR 2022
*Competitive physics informed networks.*
International Conference on Learning Representations, ICLR 2022
`OpenReview Preprint <https://openreview.net/forum?id=z9SIj-IM7tn>`_.
"""
@@ -72,21 +72,23 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
If `None`, the discriminator is a deepcopy of the ``model``.
Default is ``None``.
:param torch.optim.Optimizer optimizer_model: The optimizer of the
``model``. If `None`, the Adam optimizer is used.
Default is ``None``.
``model``. If `None`, the :class:`torch.optim.Adam` optimizer is
used. Default is ``None``.
:param torch.optim.Optimizer optimizer_discriminator: The optimizer of
the ``discriminator``. If `None`, the Adam optimizer is used.
Default is ``None``.
:param torch.optim.LRScheduler scheduler_model: Learning rate scheduler
for the ``model``. If `None`, the constant learning rate scheduler
is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler_discriminator: Learning rate
scheduler for the ``discriminator``. If `None`, the constant
learning rate scheduler is used. Default is ``None``.
the ``discriminator``. If `None`, the :class:`torch.optim.Adam`
optimizer is used. Default is ``None``.
:param Scheduler scheduler_model: Learning rate scheduler for the
``model``.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param Scheduler scheduler_discriminator: Learning rate scheduler for
the ``discriminator``.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
"""
if discriminator is None:
@@ -118,7 +120,8 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
"""
Solver training step, overridden to perform manual optimization.
:param dict batch: The batch element in the dataloader.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:return: The aggregated loss.
:rtype: LabelTensor
"""
@@ -163,7 +166,7 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
Optimizer configuration.
:return: The optimizers and the schedulers
:rtype: tuple(list, list)
:rtype: tuple[list[Optimizer], list[Scheduler]]
"""
# If the problem is an InverseProblem, add the unknown parameters
# to the parameters to be optimized
@@ -198,7 +201,8 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
:param torch.Tensor outputs: The ``model``'s output for the current
batch.
:param dict batch: The current batch of data.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:param int batch_idx: The index of the current batch.
"""
# increase by one the counter of optimization to save loggers
@@ -234,7 +238,7 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
The optimizer associated to the model.
:return: The optimizer for the model.
:rtype: torch.optim.Optimizer
:rtype: Optimizer
"""
return self.optimizers[0]
@@ -244,7 +248,7 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
The optimizer associated to the discriminator.
:return: The optimizer for the discriminator.
:rtype: torch.optim.Optimizer
:rtype: Optimizer
"""
return self.optimizers[1]
@@ -254,7 +258,7 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
The scheduler associated to the model.
:return: The scheduler for the model.
:rtype: torch.optim.lr_scheduler._LRScheduler
:rtype: Scheduler
"""
return self.schedulers[0]
@@ -264,6 +268,6 @@ class CompetitivePINN(PINNInterface, MultiSolverInterface):
The scheduler associated to the discriminator.
:return: The scheduler for the discriminator.
:rtype: torch.optim.lr_scheduler._LRScheduler
:rtype: Scheduler
"""
return self.schedulers[1]

View File

@@ -46,15 +46,15 @@ class GradientPINN(PINN):
.. seealso::
**Original reference**: Yu, Jeremy, et al. "Gradient-enhanced
physics-informed neural networks for forward and inverse
PDE problems." Computer Methods in Applied Mechanics
and Engineering 393 (2022): 114823.
**Original reference**: Yu, Jeremy, et al.
*Gradient-enhanced physics-informed neural networks for forward and
inverse PDE problems.*
Computer Methods in Applied Mechanics and Engineering 393 (2022):114823.
DOI: `10.1016 <https://doi.org/10.1016/j.cma.2022.114823>`_.
.. note::
This class is only compatible with problems that inherit from the
:class:`~pina.problem.SpatialProblem` class.
:class:`~pina.problem.spatial_problem.SpatialProblem` class.
"""
def __init__(
@@ -70,18 +70,20 @@ class GradientPINN(PINN):
Initialization of the :class:`GradientPINN` class.
:param AbstractProblem problem: The problem to be solved.
It must inherit from at least :class:`~pina.problem.SpatialProblem`
to compute the gradient of the loss.
It must inherit from at least
:class:`~pina.problem.spatial_problem.SpatialProblem` to compute the
gradient of the loss.
:param torch.nn.Module model: The neural network model to be used.
:param torch.optim.Optimizer optimizer: The optimizer to be used.
If `None`, the Adam optimizer is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler: Learning rate scheduler.
If `None`, the constant learning rate scheduler is used.
:param Optimizer optimizer: The optimizer to be used.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param Scheduler scheduler: Learning rate scheduler.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
:raises ValueError: If the problem is not a SpatialProblem.
"""

View File

@@ -42,7 +42,8 @@ class PINN(PINNInterface, SingleSolverInterface):
**Original reference**: Karniadakis, G. E., Kevrekidis, I. G., Lu, L.,
Perdikaris, P., Wang, S., & Yang, L. (2021).
Physics-informed machine learning. Nature Reviews Physics, 3, 422-440.
*Physics-informed machine learning.*
Nature Reviews Physics, 3, 422-440.
DOI: `10.1038 <https://doi.org/10.1038/s42254-021-00314-5>`_.
"""
@@ -60,15 +61,16 @@ class PINN(PINNInterface, SingleSolverInterface):
:param AbstractProblem problem: The problem to be solved.
:param torch.nn.Module model: The neural network model to be used.
:param torch.optim.Optimizer optimizer: The optimizer to be used.
If `None`, the Adam optimizer is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler: Learning rate scheduler.
If `None`, the constant learning rate scheduler is used.
:param Optimizer optimizer: The optimizer to be used.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param Scheduler scheduler: Learning rate scheduler.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
"""
super().__init__(
@@ -101,7 +103,7 @@ class PINN(PINNInterface, SingleSolverInterface):
Optimizer configuration for the PINN solver.
:return: The optimizers and the schedulers
:rtype: tuple(list, list)
:rtype: tuple[list[Optimizer], list[Scheduler]]
"""
# If the problem is an InverseProblem, add the unknown parameters
# to the parameters to be optimized.

View File

@@ -18,12 +18,12 @@ from ...condition import (
class PINNInterface(SolverInterface, metaclass=ABCMeta):
"""
Base class for Physics-Informed Neural Network (PINN) solvers, implementing
the :class:`~pina.solver.SolverInterface` class.
the :class:`~pina.solver.solver.SolverInterface` class.
The `PINNInterface` class can be used to define PINNs that work with one or
multiple optimizers and/or models. By default, it is compatible with
problems defined by :class:`~pina.problem.AbstractProblem`, and users can
choose the problem type the solver is meant to address.
problems defined by :class:`~pina.problem.abstract_problem.AbstractProblem`,
and users can choose the problem type the solver is meant to address.
"""
accepted_conditions_types = (
@@ -38,10 +38,10 @@ class PINNInterface(SolverInterface, metaclass=ABCMeta):
:param AbstractProblem problem: The problem to be solved.
:param torch.nn.Module loss: The loss function to be minimized.
If ``None``, the Mean Squared Error (MSE) loss is used.
Default is ``None``.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
:param kwargs: Additional keyword arguments to be passed to the
:class:`~pina.solver.SolverInterface` class.
:class:`~pina.solver.solver.SolverInterface` class.
"""
if loss is None:
@@ -73,9 +73,12 @@ class PINNInterface(SolverInterface, metaclass=ABCMeta):
loss as argument, thus distinguishing the training step from the
validation and test steps.
:param dict batch: The batch of data to use in the optimization cycle.
:return: The loss of the optimization cycle.
:rtype: torch.Tensor
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:return: The losses computed for all conditions in the batch, casted
to a subclass of :class:`torch.Tensor`. It should return a dict
containing the condition name and the associated scalar loss.
:rtype: dict
"""
return self._run_optimization_cycle(batch, self.loss_phys)
@@ -84,7 +87,8 @@ class PINNInterface(SolverInterface, metaclass=ABCMeta):
"""
The validation step for the PINN solver.
:param dict batch: The batch of data to use in the validation step.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:return: The loss of the validation step.
:rtype: torch.Tensor
"""
@@ -98,7 +102,8 @@ class PINNInterface(SolverInterface, metaclass=ABCMeta):
"""
The test step for the PINN solver.
:param dict batch: The batch of data to use in the test step.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:return: The loss of the test step.
:rtype: torch.Tensor
"""
@@ -169,10 +174,13 @@ class PINNInterface(SolverInterface, metaclass=ABCMeta):
Compute, given a batch, the loss for each condition and return a
dictionary with the condition name as key and the loss as value.
:param dict batch: The batch of data to use in the optimization cycle.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:param function loss_residuals: The loss function to be minimized.
:return: The loss for each condition.
:rtype dict
:return: The losses computed for all conditions in the batch, casted
to a subclass of :class:`torch.Tensor`. It should return a dict
containing the condition name and the associated scalar loss.
:rtype: dict
"""
condition_loss = {}
for condition_name, points in batch:

View File

@@ -59,11 +59,11 @@ class RBAPINN(PINN):
.. seealso::
**Original reference**: Sokratis J. Anagnostopoulos, Juan D. Toscano,
Nikolaos Stergiopulos, and George E. Karniadakis.
"Residual-based attention and connection to information
bottleneck theory in PINNs".
*Residual-based attention and connection to information
bottleneck theory in PINNs.*
Computer Methods in Applied Mechanics and Engineering 421 (2024): 116805
DOI: `10.1016/
j.cma.2024.116805 <https://doi.org/10.1016/j.cma.2024.116805>`_.
DOI: `10.1016/j.cma.2024.116805
<https://doi.org/10.1016/j.cma.2024.116805>`_.
"""
def __init__(
@@ -82,15 +82,16 @@ class RBAPINN(PINN):
:param AbstractProblem problem: The problem to be solved.
:param torch.nn.Module model: The neural network model to be used.
:param torch.optim.Optimizer optimizer: The optimizer to be used.
If `None`, the Adam optimizer is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler: Learning rate scheduler.
If `None`, the constant learning rate scheduler is used.
param Optimizer optimizer: The optimizer to be used.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param Scheduler scheduler: Learning rate scheduler.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
:param float | int eta: The learning rate for the weights of the
residuals. Default is ``0.001``.
@@ -147,7 +148,7 @@ class RBAPINN(PINN):
:param LabelTensor loss_value: the tensor of pointwise losses.
:raises RuntimeError: If the loss reduction is not ``mean`` or ``sum``.
:return: The computed scalar loss.
:rtype LabelTensor
:rtype: LabelTensor
"""
if self.loss.reduction == "mean":
ret = torch.mean(loss_value)

View File

@@ -94,10 +94,10 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
.. seealso::
**Original reference**: McClenny, Levi D., and Ulisses M. Braga-Neto.
"Self-adaptive physics-informed neural networks."
*Self-adaptive physics-informed neural networks.*
Journal of Computational Physics 474 (2023): 111722.
DOI: `10.1016/
j.jcp.2022.111722 <https://doi.org/10.1016/j.jcp.2022.111722>`_.
DOI: `10.1016/j.jcp.2022.111722
<https://doi.org/10.1016/j.jcp.2022.111722>`_.
"""
def __init__(
@@ -119,22 +119,25 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
:param torch.nn.Module model: The model to be used.
:param torch.nn.Module weight_function: The Self-Adaptive mask model.
Default is ``torch.nn.Sigmoid()``.
:param torch.optim.Optimizer optimizer_model: The optimizer of the
``model``. If `None`, the Adam optimizer is used.
:param Optimizer optimizer_model: The optimizer of the ``model``.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param torch.optim.Optimizer optimizer_weights: The optimizer of the
``weight_function``. If `None`, the Adam optimizer is used.
:param Optimizer optimizer_weights: The optimizer of the
``weight_function``.
If `None`, the :class:`torch.optim.Adam` optimizer is used.
Default is ``None``.
:param torch.optim.LRScheduler scheduler_model: Learning rate scheduler
for the ``model``. If `None`, the constant learning rate scheduler
is used. Default is ``None``.
:param torch.optim.LRScheduler scheduler_weights: Learning rate
scheduler for the ``weight_function``. If `None`, the constant
learning rate scheduler is used. Default is ``None``.
:param Scheduler scheduler_model: Learning rate scheduler for the
``model``.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param Scheduler scheduler_weights: Learning rate scheduler for the
``weight_function``.
If `None`, the :class:`torch.optim.lr_scheduler.ConstantLR`
scheduler is used. Default is ``None``.
:param WeightingInterface weighting: The weighting schema to be used.
If `None`, no weighting schema is used. Default is ``None``.
:param torch.nn.Module loss: The loss function to be minimized.
If `None`, the Mean Squared Error (MSE) loss is used.
If `None`, the :class:`torch.nn.MSELoss` loss is used.
Default is `None`.
"""
# check consistency weitghs_function
@@ -175,7 +178,8 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
"""
Solver training step, overridden to perform manual optimization.
:param dict batch: The batch element in the dataloader.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:return: The aggregated loss.
:rtype: LabelTensor
"""
@@ -198,7 +202,7 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
Optimizer configuration.
:return: The optimizers and the schedulers
:rtype: tuple(list, list)
:rtype: tuple[list[Optimizer], list[Scheduler]]
"""
# If the problem is an InverseProblem, add the unknown parameters
# to the parameters to be optimized
@@ -227,7 +231,8 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
:param torch.Tensor outputs: The ``model``'s output for the current
batch.
:param dict batch: The current batch of data.
:param list[tuple[str, dict]] batch: A batch of data. Each element is a
tuple containing a condition name and a dictionary of points.
:param int batch_idx: The index of the current batch.
"""
# increase by one the counter of optimization to save loggers
@@ -307,7 +312,7 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
:param LabelTensor loss_value: the tensor of pointwise losses.
:raises RuntimeError: If the loss reduction is not ``mean`` or ``sum``.
:return: The computed scalar loss.
:rtype LabelTensor
:rtype: LabelTensor
"""
if self.loss.reduction == "mean":
ret = torch.mean(loss_value)
@@ -346,7 +351,7 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
The scheduler associated to the model.
:return: The scheduler for the model.
:rtype: torch.optim.lr_scheduler._LRScheduler
:rtype: Scheduler
"""
return self.schedulers[0]
@@ -356,7 +361,7 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
The scheduler associated to the mask model.
:return: The scheduler for the mask model.
:rtype: torch.optim.lr_scheduler._LRScheduler
:rtype: Scheduler
"""
return self.schedulers[1]
@@ -366,7 +371,7 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
Returns the optimizer associated to the model.
:return: The optimizer for the model.
:rtype: torch.optim.Optimizer
:rtype: Optimizer
"""
return self.optimizers[0]
@@ -376,6 +381,6 @@ class SelfAdaptivePINN(PINNInterface, MultiSolverInterface):
The optimizer associated to the mask model.
:return: The optimizer for the mask model.
:rtype: torch.optim.Optimizer
:rtype: Optimizer
"""
return self.optimizers[1]