Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 38 additions & 80 deletions tutorials/tutorial1/tutorial.ipynb

Large diffs are not rendered by default.

69 changes: 14 additions & 55 deletions tutorials/tutorial10/tutorial.ipynb

Large diffs are not rendered by default.

58 changes: 24 additions & 34 deletions tutorials/tutorial11/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,7 +37,7 @@
"import warnings\n",
"\n",
"from pina import Trainer\n",
"from pina.solver import SupervisedSolver\n",
"from pina.solver import SupervisedSingleModelSolver\n",
"from pina.model import FeedForward\n",
"from pina.problem.zoo import SupervisedProblem\n",
"\n",
Expand All @@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -71,16 +71,16 @@
" input_dimensions=1,\n",
")\n",
"\n",
"# create the SupervisedSolver object\n",
"solver = SupervisedSolver(problem, model, use_lt=False)"
"# create the SupervisedSingleModelSolver object\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Till now we just followed the extact step of the previous tutorials. The `Trainer` object\n",
"can be initialized by simiply passing the `SupervisedSolver` solver"
"Until now we just followed the exact step of the previous tutorials. The `Trainer` object\n",
"can be initialized by simply passing the `SupervisedSingleModelSolver` solver:"
]
},
{
Expand Down Expand Up @@ -132,7 +132,7 @@
"source": [
"## Trainer Logging\n",
"\n",
"In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callbacks`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n",
"In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callback`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n",
"\n",
"However, especially when we need to train multiple times to get an average of the loss across multiple runs, `lightning.pytorch.loggers` might be useful. Here we will use `TensorBoardLogger` (more on [logging](https://lightning.ai/docs/pytorch/stable/extensions/logging.html) here), but you can choose the one you prefer (or make your own one).\n",
"\n",
Expand All @@ -156,7 +156,7 @@
" output_dimensions=1,\n",
" input_dimensions=1,\n",
" )\n",
" solver = SupervisedSolver(problem, model, use_lt=False)\n",
" solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
" trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand Down Expand Up @@ -194,7 +194,7 @@
"\n",
"## Trainer Callbacks\n",
"\n",
"Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** `Solver`s.\n",
"Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** Solvers.\n",
"\n",
"Lightning has a callback system to execute them when needed. **Callbacks** should capture NON-ESSENTIAL logic that is NOT required for your lightning module to run.\n",
"\n",
Expand All @@ -206,12 +206,12 @@
"* Directly calling methods (e.g., on_validation_end) is strongly discouraged.\n",
"* Whenever possible, your callbacks should not depend on the order in which they are executed.\n",
"\n",
"We will try now to implement a naive version of `MetricTraker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callbacks`."
"We will try now to implement a naive version of `MetricTracker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callback`."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -227,7 +227,10 @@
"\n",
" def on_train_epoch_end(\n",
" self, trainer, __\n",
" ): # function called at the end of each epoch\n",
" ): \n",
" \"\"\"\n",
" Function called at the end of each epoch.\n",
" \"\"\"\n",
" self.saved_metrics.append(\n",
" {key: value for key, value in trainer.logged_metrics.items()}\n",
" )"
Expand All @@ -252,7 +255,7 @@
" output_dimensions=1,\n",
" input_dimensions=1,\n",
")\n",
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
"trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand All @@ -276,22 +279,9 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'data_loss': tensor(104.4973), 'train_loss': tensor(104.4973)},\n",
" {'data_loss': tensor(104.3082), 'train_loss': tensor(104.3082)},\n",
" {'data_loss': tensor(104.1189), 'train_loss': tensor(104.1189)}]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"trainer.callbacks[0].saved_metrics[:3] # only the first three epochs"
]
Expand All @@ -312,12 +302,12 @@
"outputs": [],
"source": [
"model = FeedForward(\n",
" layers=[10, 10],\n",
" layers=[64, 64],\n",
" func=torch.nn.Tanh,\n",
" output_dimensions=1,\n",
" input_dimensions=1,\n",
")\n",
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
"trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand Down Expand Up @@ -378,7 +368,7 @@
" input_dimensions=1,\n",
")\n",
"\n",
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
"trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand Down Expand Up @@ -415,7 +405,7 @@
" output_dimensions=1,\n",
" input_dimensions=1,\n",
")\n",
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
"trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand Down Expand Up @@ -454,7 +444,7 @@
" output_dimensions=1,\n",
" input_dimensions=1,\n",
")\n",
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
"trainer = Trainer(\n",
" solver=solver,\n",
" accelerator=\"cpu\",\n",
Expand Down
23 changes: 11 additions & 12 deletions tutorials/tutorial12/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial12/tutorial.ipynb)\n",
"\n",
"\n",
"In this tutorial, we will explore how to use the `Equation` class in **PINA**. We will focus on how to leverage this class, along with its inherited subclasses, to enforce residual minimization in **Physics-Informed Neural Networks (PINNs)**.\n",
"In this tutorial, we will explore how to use the `Equation` class in **PINA**. We will focus on how to leverage this class, along with its inherited subclasses, to enforce residual minimization in **Physics-Informed Solvers**.\n",
"\n",
"By the end of this guide, you'll understand how to integrate physical laws and constraints directly into your model training, ensuring that the solution adheres to the underlying differential equations.\n",
"\n",
Expand Down Expand Up @@ -53,16 +53,17 @@
"# useful imports\n",
"from pina import Condition\n",
"from pina.problem import SpatialProblem, TimeDependentProblem\n",
"from pina.equation import Equation, FixedValue\n",
"from pina.equation import Equation\n",
"from pina.equation.zoo import FixedValue\n",
"from pina.domain import CartesianDomain\n",
"from pina.operator import grad, fast_grad, laplacian"
"from pina.operator import grad, laplacian"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's begin by defining the Burgers equation and its initial condition as Python functions. These functions will take the model's `input` (spatial and temporal coordinates) and `output` (predicted solution) as arguments. The goal is to compute the residuals for the Burgers equation, which we will minimize during training."
"Let's begin by defining the Burgers equation and its initial condition as Python functions. These functions will take the model's `input_` (spatial and temporal coordinates) and `output_` (predicted solution) as arguments. The goal is to compute the residuals for the Burgers equation, which we will minimize during training."
]
},
{
Expand Down Expand Up @@ -135,16 +136,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Equation` class takes as input a function (in this case it happens twice, with `initial_condition` and `burgers_equation`) which computes a residual of an equation, such as a PDE. In a problem class such as the one above, the `Equation` class with such a given input is passed as a parameter in the specified `Condition`. \n",
"\n",
"The `FixedValue` class takes as input a value of the same dimensions as the output functions. This class can be used to enforce a fixed value for a specific condition, such as Dirichlet boundary conditions, as demonstrated in our example.\n",
"\n",
"Once the equations are set as above in the problem conditions, the PINN solver will aim to minimize the residuals described in each equation during the training phase. \n",
"\n",
"### Available classes of equations:\n",
"- `FixedGradient` and `FixedFlux`: These work analogously to the `FixedValue` class, where we can enforce a constant value on the gradient or the divergence of the solution, respectively.\n",
"- `Laplace`: This class can be used to enforce that the Laplacian of the solution is zero.\n",
"- `SystemEquation`: This class allows you to enforce multiple conditions on the same subdomain by passing a list of residual equations defined in the problem.\n",
"The `Equation` class takes as input a function that computes the residual of an equation, such as a PDE. In the example above, this is done twice, using `initial_condition` and `burgers_equation`. Once defined, each `Equation` object is passed to a specific `Condition` inside the problem class. When multiple residual equations need to be enforced on the same subdomain, they can be grouped using `SystemEquation`, which takes a list of equations defined in the problem.\n",
"\n",
"For common use cases, PINA also provides several predefined equation classes. The `FixedValue`, `FixedGradient`, `FixedFlux`, and `FixedLaplacian` classes can be used to enforce fixed constraints on the solution or on its derivatives. In particular, `FixedValue` enforces a constant value with the same dimensions as the output function, making it suitable for conditions such as Dirichlet boundary conditions. Similarly, `FixedGradient` and `FixedFlux` enforce fixed values on the gradient or flux of the solution, respectively, while `FixedLaplacian` can be used to impose a fixed value on the Laplacian of the solution. Many ready-to-use equations are also available in the `pina.equation.zoo` module.\n",
"\n",
"Once the equations are assigned to the problem conditions, the physics-informed solver aims to minimize the residuals defined by each equation during the training phase.\n",
"\n",
"## Defining a new Equation class\n",
"`Equation` classes can also be inherited to define a new class. For example, we can define a new class `Burgers1D` to represent the Burgers equation. During the class call, we can pass the viscosity parameter $\\nu$:\n",
Expand Down Expand Up @@ -240,7 +239,7 @@
"From here, you can:\n",
"\n",
"- **Define Additional Complex Equation Classes**: Create your own equation classes, such as `SchrodingerEquation`, `NavierStokesEquation`, etc.\n",
"- **Define More `FixedOperator` Classes**: Implement operators like `FixedCurl`, `FixedDivergence`, and others for more advanced simulations.\n",
"- **Define More `FixedOperator` Classes**: Implement operators like `FixedCurl` for more advanced simulations.\n",
"- **Integrate Custom Equations and Operators**: Combine your custom equations and operators into larger systems for more complex simulations.\n",
"- **and many more!**: Explore for example different residual minimization techniques to improve the performance and accuracy of your models.\n",
"\n",
Expand Down
101 changes: 28 additions & 73 deletions tutorials/tutorial13/tutorial.ipynb

Large diffs are not rendered by default.

101 changes: 71 additions & 30 deletions tutorials/tutorial14/tutorial.ipynb

Large diffs are not rendered by default.

Loading
Loading