Co-authored-by: dario-coscia <dario-coscia@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
e033f3b125
commit
c709f386b4
36
tutorials/tutorial15/tutorial.py
vendored
36
tutorials/tutorial15/tutorial.py
vendored
@@ -2,15 +2,15 @@
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: Chemical Properties Prediction with Graph Neural Networks
|
||||
#
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial15/tutorial.ipynb)
|
||||
#
|
||||
# In this tutorial we will use **Graph Neural Networks** (GNNs) for chemical properties prediction. Chemical properties prediction involves estimating or determining the physical, chemical, or biological characteristics of molecules based on their structure.
|
||||
#
|
||||
#
|
||||
# In this tutorial we will use **Graph Neural Networks** (GNNs) for chemical properties prediction. Chemical properties prediction involves estimating or determining the physical, chemical, or biological characteristics of molecules based on their structure.
|
||||
#
|
||||
# Molecules can naturally be represented as graphs, where atoms serve as the nodes and chemical bonds as the edges connecting them. This graph-based structure makes GNNs a great fit for predicting chemical properties.
|
||||
#
|
||||
#
|
||||
# In the tutorial we will use the [QM9 dataset](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.datasets.QM9.html#torch_geometric.datasets.QM9) from Pytorch Geometric. The dataset contains small molecules, each consisting of up to 29 atoms, with every atom having a corresponding 3D position. Each atom is also represented by a five-dimensional one-hot encoded vector that indicates the atom type (H, C, N, O, F).
|
||||
#
|
||||
#
|
||||
# First of all, let's start by importing useful modules!
|
||||
|
||||
# In[1]:
|
||||
@@ -42,7 +42,7 @@ warnings.filterwarnings("ignore")
|
||||
# ## Download Data and create the Problem
|
||||
|
||||
# We download the dataset and save the molecules as a list of `Data` objects (`input_`), where each element contains one molecule encoded in a graph structure. The corresponding target properties (`target_`) are listed below:
|
||||
#
|
||||
#
|
||||
# | Target | Property | Description | Unit |
|
||||
# |--------|----------------------------------|-----------------------------------------------------------------------------------|---------------------------------------------|
|
||||
# | 0 | $\mu$ | Dipole moment | $D$ |
|
||||
@@ -64,7 +64,7 @@ warnings.filterwarnings("ignore")
|
||||
# | 16 | $A$ | Rotational constant | $GHz$ |
|
||||
# | 17 | $B$ | Rotational constant | $GHz$ |
|
||||
# | 18 | $C$ | Rotational constant | $GHz$ |
|
||||
#
|
||||
#
|
||||
|
||||
# In[2]:
|
||||
|
||||
@@ -92,9 +92,9 @@ problem = SupervisedProblem(input_=input_, output_=target_)
|
||||
|
||||
|
||||
# ## Build the Model
|
||||
#
|
||||
#
|
||||
# To predict molecular properties, we will construct a simple Convolutional Graph Neural Network using the [`GCNConv`]() module from PyG. While this tutorial focuses on a straightforward model, more advanced architectures—such as Equivariant Networks—could potentially yield better performance. Please note that this tutorial serves only for demonstration purposes.
|
||||
#
|
||||
#
|
||||
# **Importantly** notice that in the `forward` pass we pass a data object as input, and unpack inside the graph attributes. This is the only requirement in **PINA** to use graphs and solvers together.
|
||||
|
||||
# In[4]:
|
||||
@@ -118,7 +118,7 @@ class GNN(torch.nn.Module):
|
||||
|
||||
|
||||
# ## Train the Model
|
||||
#
|
||||
#
|
||||
# Now that the problem is created and the model is built, we can train the model using the [`SupervisedSolver`](https://mathlab.github.io/PINA/_rst/solver/supervised.html), which is the solver for standard supervised learning task. We will optimize the Maximum Absolute Error and test on the same metric. In the [`Trainer`](https://mathlab.github.io/PINA/_rst/trainer.html) class we specify the optimization hyperparameters.
|
||||
|
||||
# In[5]:
|
||||
@@ -153,7 +153,7 @@ _ = trainer.test()
|
||||
|
||||
|
||||
# We observe that the model achieves an average error of approximately 0.4 MAE across all property predictions. This error is an average, but we can also inspect the error for each individual property prediction.
|
||||
#
|
||||
#
|
||||
# To do this, we need access to the test dataset, which can be retrieved from the trainer's datamodule. Each datamodule contains both the dataloader and dataset objects. For the dataset, we can use the [`get_all_data()`](https://mathlab.github.io/PINA/_rst/data/dataset.html#pina.data.dataset.PinaDataset.get_all_data) method. This function returns the entire dataset as a dictionary, where the keys represent the Condition names, and the values are dictionaries containing input and target tensors.
|
||||
|
||||
# In[7]:
|
||||
@@ -301,15 +301,15 @@ plt.show()
|
||||
# By looking more into details, we can see that $A$ is not predicted that well, but the small values of the quantity lead to a lower MAE than the other properties. From the plot we can see that the atomatization energies, free energy and enthalpy are the predicted properties with higher correlation with the true chemical properties.
|
||||
|
||||
# ## What's Next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the tutorial on chemical properties prediction with **PINA**! Now that you've got the basics, there are several exciting directions to explore:
|
||||
#
|
||||
#
|
||||
# 1. **Train the network for longer or with different layer sizes**: Experiment with various configurations to see how the network's accuracy improves.
|
||||
#
|
||||
#
|
||||
# 2. **Use a different network**: For example, Equivariant Graph Neural Networks (EGNNs) have shown great results on molecular tasks by leveraging group symmetries. If you're interested, check out [*E(n) Equivariant Graph Neural Networks*](https://arxiv.org/abs/2102.09844) for more details.
|
||||
#
|
||||
#
|
||||
# 3. **What if the input is time-dependent?**: For example, predicting force fields in Molecular Dynamics simulations. In PINA, you can predict force fields with ease, as it's still a supervised learning task. If this interests you, have a look at [*Machine Learning Force Fields*](https://pubs.acs.org/doi/10.1021/acs.chemrev.0c01111).
|
||||
#
|
||||
#
|
||||
# 4. **...and many more!**: The possibilities are vast, including exploring new architectures, working with larger datasets, and applying this framework to more complex systems.
|
||||
#
|
||||
#
|
||||
# For more resources and tutorials, check out the [PINA Documentation](https://mathlab.github.io/PINA/).
|
||||
|
||||
124
tutorials/tutorial16/tutorial.py
vendored
124
tutorials/tutorial16/tutorial.py
vendored
@@ -2,19 +2,19 @@
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: How to build a Problem in PINA
|
||||
#
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial16/tutorial.ipynb)
|
||||
#
|
||||
#
|
||||
|
||||
# In this tutorial, we will demonstrate how to build a **Problem** in **PINA** using a toy example. The tutorial will cover the following topics:
|
||||
#
|
||||
#
|
||||
# - **Building a Problem**: Learn how to construct a problem using the built-in PINA classes.
|
||||
# - **Generating Data for Physics-Informed Training**: Understand how to generate the necessary data for training.
|
||||
# - **Exploring the `problem.zoo` Module**: Get familiar with the `problem.zoo` module, which collects pre-built problems for easy use.
|
||||
#
|
||||
#
|
||||
# By the end of this tutorial, you'll be able to write **data-driven** or **differential problems** in **PINA** and prepare them for model training!
|
||||
|
||||
# In[ ]:
|
||||
# In[1]:
|
||||
|
||||
|
||||
## routine needed to run the notebook on Google Colab
|
||||
@@ -37,34 +37,34 @@ warnings.filterwarnings("ignore")
|
||||
# ## Build a PINA problem
|
||||
|
||||
# In **PINA**, defining a problem is done by creating a Python `class` that inherits from one or more problem classes, such as `SpatialProblem`, `TimeDependentProblem`, or `ParametricProblem`, depending on the nature of the problem. We refer to the `model` as the object that solves the problem, e.g., a **Neural Network**.
|
||||
#
|
||||
#
|
||||
# We can have two types of problems:
|
||||
# 1. ***Data-Driven Problems***: The model is trained using data, such as in classification networks or autoencoders.
|
||||
# 2. **&Physics-Driven Problems***: The model is trained using physical laws representing the problem, such as in **PINNs**.
|
||||
# Let's start by building the first type, the data driven type.
|
||||
#
|
||||
# 2. ***Physics-Driven Problems***: The model is trained using physical laws representing the problem, such as in **PINNs**.
|
||||
# Let's start by building the first type, the data driven type.
|
||||
#
|
||||
# ### Data driven modelling
|
||||
# In data-driven modelling, we always have an **input** and a **target**. The model's objective is to reconstruct the target from the input. Examples include:
|
||||
# - Image reconstruction (perturbed image as input, clear image as target)
|
||||
# - Classification (e.g., input: molecule, target: chemical properties)
|
||||
#
|
||||
#
|
||||
# To build a data-driven problem in **PINA**, you can inherit from the `AbstractProblem` class. Below is an example of a regression problem where the input is a scalar value `x` and the target is a scalar value `y`.
|
||||
#
|
||||
#
|
||||
# ```python
|
||||
# from pina.problem import AbstractProblem
|
||||
#
|
||||
#
|
||||
# class SupervisedProblem(AbstractProblem):
|
||||
#
|
||||
#
|
||||
# input_variables = ['x']
|
||||
# output_variables = ['y']
|
||||
#
|
||||
#
|
||||
# # other stuff ...
|
||||
# ```
|
||||
# Observe that we define `input_variables` and `output_variables` as lists of symbols. This is because, in PINA, `torch.Tensors` can be labeled (see [`LabelTensor`](https://mathlab.github.io/PINA/_rst/label_tensor.html)), providing maximum flexibility for tensor manipulation. If you prefer to use regular tensors, you can simply set these to ``None``.
|
||||
#
|
||||
#
|
||||
# To specify the input and target data, you need to use the [`Condition`](https://mathlab.github.io/PINA/_rst/condition/condition.html) interface. A condition defines the constraints (such as physical equations, boundary conditions, etc.) that must be satisfied within the problem. Once the condition is applied, the full problem is outlined below:
|
||||
|
||||
# In[3]:
|
||||
# In[2]:
|
||||
|
||||
|
||||
from pina import Condition, LabelTensor
|
||||
@@ -92,13 +92,13 @@ problem = SupervisedProblem()
|
||||
|
||||
|
||||
# You can define as many conditions as needed, and the model will attempt to minimize all of them simultaneously! You can access the data in various ways:
|
||||
#
|
||||
# - `problem.conditions['<condition name>'].input`, `problem.conditions['<condition name>'].output` – Access the input and output data for the specified condition `<condition name>`.
|
||||
#
|
||||
# - `problem.conditions['<condition name>'].input`, `problem.conditions['<condition name>'].target` – Access the input and output data for the specified condition `<condition name>`.
|
||||
# - `problem.input_pts` – Access the input points for all conditions.
|
||||
#
|
||||
#
|
||||
# To ensure that the problem is ready, you can check if all domains have been discretized, meaning all conditions have input points available to pass to the model:
|
||||
|
||||
# In[4]:
|
||||
# In[3]:
|
||||
|
||||
|
||||
# check if all domains are discretised
|
||||
@@ -109,7 +109,7 @@ problem.are_all_domains_discretised
|
||||
|
||||
# ### Simple Ordinary Differential Equation
|
||||
# What if we don't have data but we know the physical laws that define the data? Then physics-informed training is the solution! As an example, consider the following Ordinary Differential Equation (ODE):
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# \begin{equation}
|
||||
# \begin{cases}
|
||||
@@ -118,39 +118,39 @@ problem.are_all_domains_discretised
|
||||
# \end{cases}
|
||||
# \end{equation}
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# with the analytical solution $u(x) = e^x$. This problem is a spatial problem because the ODE depends only on the spatial variable $x\in(0,1)$. In PINA, differential problems are categorized by their nature, e.g.:
|
||||
# * `SpatialProblem` $\rightarrow$ a differential equation with spatial variable(s)
|
||||
# * `TimeDependentProblem` $\rightarrow$ a time-dependent differential equation with temporal variable(s)
|
||||
# * `ParametricProblem` $\rightarrow$ a parametrized differential equation with parametric variable(s)
|
||||
# * `InverseProblem` $\rightarrow$ this is a more advanced topic, see [this tutorial](https://mathlab.github.io/PINA/tutorial7/tutorial.html) for more details.
|
||||
#
|
||||
#
|
||||
# In our case, the physical ODE inherits from the `SpatialProblem` class, since only spatial variables define the ODE.
|
||||
#
|
||||
#
|
||||
# ```python
|
||||
# class SimpleODE(SpatialProblem):
|
||||
#
|
||||
#
|
||||
# output_variables = ['u']
|
||||
# spatial_domain = CartesianDomain{'x': [0, 1]})
|
||||
#
|
||||
#
|
||||
# # other stuff ...
|
||||
# ```
|
||||
#
|
||||
#
|
||||
# What if our equation is was also time-dependent, e.g. Partial Differential Equations (PDE)? In this case, our `class` will inherit from both `SpatialProblem` and `TimeDependentProblem`:
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# ```python
|
||||
# class TimeSpaceODE(SpatialProblem, TimeDependentProblem):
|
||||
#
|
||||
#
|
||||
# output_variables = ["u"]
|
||||
# spatial_domain = CartesianDomain({"x": [0, 1]})
|
||||
# temporal_domain = CartesianDomain({"t": [0, 1]})
|
||||
#
|
||||
#
|
||||
# # other stuff ...
|
||||
# ```
|
||||
#
|
||||
#
|
||||
# Differently from data-driven problems, differential-problems need to specify the domain type. If you look at our ODE definition, the spatial varibale $x$ is defined in the interval $(0,1)$, and accordingly the `spatial_domain` is a `CartesianDomain` with the input variable `x` in `[0,1]`. To know more about the Domain class see the [related tutorial](https://mathlab.github.io/PINA/tutorial6/tutorial.html). Different problems require different domain, here below we summarize the relevant ones:
|
||||
#
|
||||
#
|
||||
# | Problem Type | Required Domain |
|
||||
# |-------------------------|--------------------------------|
|
||||
# | `SpatialProblem` | `spatial_domain` |
|
||||
@@ -160,7 +160,7 @@ problem.are_all_domains_discretised
|
||||
|
||||
# Nice, the Problem class is initialized! How to represent the differential equation in **PINA**? To do this, we need to load the **PINA** operators from `pina.operator` module. Again, we'll consider Equation (1) and represent it in **PINA**:
|
||||
|
||||
# In[5]:
|
||||
# In[4]:
|
||||
|
||||
|
||||
from pina.problem import SpatialProblem
|
||||
@@ -203,28 +203,28 @@ class SimpleODE(SpatialProblem):
|
||||
return torch.exp(pts.extract(["x"]))
|
||||
|
||||
|
||||
# As you can see, we implemented the `ode_equation` function which given the model ouput and input returns the equation residual. These residuals are the ones minimized during PINN optimization (for more on PINN see [the related tutorials](https://mathlab.github.io/PINA/_tutorial.html#physics-informed-neural-networks)).
|
||||
#
|
||||
# As you can see, we implemented the `ode_equation` function which given the model ouput and input returns the equation residual. These residuals are the ones minimized during PINN optimization (for more on PINN see [the related tutorials](https://mathlab.github.io/PINA/_tutorial.html#physics-informed-neural-networks)).
|
||||
#
|
||||
# How are the residuals computed?
|
||||
# Givem the output we perform differential operation using the [operator modulus](https://mathlab.github.io/PINA/_rst/operator.html). It is pretty intuitive, each differential operator takes the following inputs:
|
||||
# - A tensor on which the operator is applied.
|
||||
# - A tensor with respect to which the operator is computed.
|
||||
# - The names of the output variables for which the operator is evaluated.
|
||||
# Given the output we perform differential operation using the [operator modulus](https://mathlab.github.io/PINA/_rst/operator.html). It is pretty intuitive, each differential operator takes the following inputs:
|
||||
# - A tensor on which the operator is applied.
|
||||
# - A tensor with respect to which the operator is computed.
|
||||
# - The names of the output variables for which the operator is evaluated.
|
||||
# - The names of the variables with respect to which the operator is computed.
|
||||
# We also have a `fast` version of differential operators, where no checks are performed. This can be used to boost performances, once you know the standard ones are doing their job.
|
||||
#
|
||||
# We also have a `fast` version of differential operators, where no checks are performed. This can be used to boost performances, once you know the standard ones are doing their job.
|
||||
#
|
||||
# Notice that we do not pass directly a `python` function, but an `Equation` object, which is initialized with the `python` function. This is done so that all the computations and internal checks are done inside **PINA**, see [the related tutorials](https://mathlab.github.io/PINA/tutorial12/tutorial.html) for more.
|
||||
#
|
||||
#
|
||||
# Once we have defined the function, we need to tell the neural network where these methods are to be applied. To do so, we use again the `Condition` class. In the `Condition` class, we pass the location points and the equation we want minimized on those points.
|
||||
#
|
||||
#
|
||||
# Finally, it's possible to define a `solution` function, which can be useful if we want to plot the results and see how the real solution compares to the expected (true) solution. Notice that the `solution` function is a method of the `Problem` class, but it is not mandatory for problem definition.
|
||||
#
|
||||
#
|
||||
|
||||
# ## Generate data for Physical Problems
|
||||
#
|
||||
#
|
||||
# When training physics based models, data can come in form of direct numerical simulation results (tensors, graph), or points in the domains which need to be sampled. In case we perform unsupervised learning, we just need the collocation points for training, i.e. points where we want to evaluate the neural network. Sampling point in **PINA** is very easy. But first, let's check if the domains are dicsretized by using the `are_all_domains_discretised` method.
|
||||
|
||||
# In[16]:
|
||||
# In[5]:
|
||||
|
||||
|
||||
problem = SimpleODE()
|
||||
@@ -232,7 +232,7 @@ problem.are_all_domains_discretised
|
||||
|
||||
|
||||
# This is false becase the input points are not available (we need to discretize!). If you call `problem.input_points` at this stage you will get an error due to point missing in the condition.
|
||||
#
|
||||
#
|
||||
# ```bash
|
||||
# >>> problem.input_pts
|
||||
# ```
|
||||
@@ -241,19 +241,19 @@ problem.are_all_domains_discretised
|
||||
# KeyError Traceback (most recent call last)
|
||||
# Cell In[32], line 1
|
||||
# ----> 1 problem.input_pts
|
||||
#
|
||||
#
|
||||
# File ~/GitHub/PINA/pina/problem/abstract_problem.py:78, in AbstractProblem.input_pts(self)
|
||||
# 76 to_return[cond_name] = cond.input
|
||||
# 77 elif hasattr(cond, "domain"):
|
||||
# ---> 78 to_return[cond_name] = self._discretised_domains[cond.domain]
|
||||
# 79 return to_return
|
||||
#
|
||||
#
|
||||
# KeyError: 'x0'
|
||||
# ```
|
||||
|
||||
# To discretise the problem you can use the `discretise_domain` method:
|
||||
|
||||
# In[25]:
|
||||
# In[6]:
|
||||
|
||||
|
||||
# sampling 20 points in [0, 1] through discretization in all locations
|
||||
@@ -269,7 +269,7 @@ problem.discretise_domain(n=20, mode="random")
|
||||
|
||||
# We are going to use latin hypercube points for sampling. We need to sample in all the conditions domains. In our case we sample in `D` and `x0`.
|
||||
|
||||
# In[26]:
|
||||
# In[7]:
|
||||
|
||||
|
||||
# sampling for training
|
||||
@@ -279,7 +279,7 @@ problem.discretise_domain(5, "lh", domains=["D"])
|
||||
|
||||
# The points are saved in a python `dict`, and can be accessed by calling the attributes `input_pts` or `discretised_domains` of the problem.
|
||||
|
||||
# In[29]:
|
||||
# In[8]:
|
||||
|
||||
|
||||
print("Input points:", problem.input_pts)
|
||||
@@ -288,7 +288,7 @@ print("Input points labels:", problem.discretised_domains)
|
||||
|
||||
# To visualize the sampled points we can use `matplotlib.pyplot`:
|
||||
|
||||
# In[28]:
|
||||
# In[9]:
|
||||
|
||||
|
||||
for location in problem.input_pts:
|
||||
@@ -300,12 +300,12 @@ plt.legend()
|
||||
|
||||
|
||||
# ## The Problem Zoo module
|
||||
#
|
||||
#
|
||||
# In PINA many problems are already implemented for you in the [Problem Zoo module](https://mathlab.github.io/PINA/_rst/_code.html#problems-zoo). For example, the supervised problem at the beginning of the tutorial is implemented in [`SupervisedProblem`](https://mathlab.github.io/PINA/_rst/problem/zoo/supervised_problem.html)!
|
||||
#
|
||||
#
|
||||
# Let's see now a physics based example, the advection equation
|
||||
|
||||
# In[30]:
|
||||
# In[10]:
|
||||
|
||||
|
||||
from pina.problem.zoo import AdvectionProblem
|
||||
@@ -324,13 +324,13 @@ print(
|
||||
|
||||
|
||||
# ## What's Next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the introductory tutorial of **PINA** problems! There are several directions you can explore next:
|
||||
#
|
||||
#
|
||||
# 1. **Create Custom Problems**: Try building your own problems using the PINA framework, experiment with different PDEs, initial/boundary conditions, and data structures.
|
||||
#
|
||||
#
|
||||
# 2. **Explore the Problem Zoo**: Dive into the [`problem.zoo` module](https://mathlab.github.io/PINA/_rst/_code.html#problems-zoo) to find a variety of predefined problem setups and use them as a starting point or inspiration for your own.
|
||||
#
|
||||
#
|
||||
# 3. **...and many more!**: The possibilities are vast! Consider experimenting with different solver strategies, model architectures, or even implementing your own physical constraints.
|
||||
#
|
||||
#
|
||||
# For more examples and in-depth guides, be sure to check out the [PINA Documentation](https://mathlab.github.io/PINA/).
|
||||
|
||||
120
tutorials/tutorial21/tutorial.py
vendored
120
tutorials/tutorial21/tutorial.py
vendored
@@ -1,16 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: Introductory Tutorial: Supervised Learning with PINA
|
||||
#
|
||||
# # Tutorial: Introductory Tutorial: Neural Operator Learning with PINA
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial21/tutorial.ipynb)
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# > ##### ⚠️ ***Before starting:***
|
||||
# > We assume you are already familiar with the concepts covered in the [Getting started with PINA](https://mathlab.github.io/PINA/_tutorial.html#getting-started-with-pina) tutorials. If not, we strongly recommend reviewing them before exploring this advanced topic.
|
||||
#
|
||||
#
|
||||
# In this tutorial, we will demonstrate a typical use case of **PINA** for Neural Operator learning. We will cover the basics of training a Neural Operator with PINA, if you want to go further into the topic look at our dedicated [tutorials](https://mathlab.github.io/PINA/_tutorial.html#neural-operator-learning) on the topic.
|
||||
#
|
||||
#
|
||||
# Let's start by importing the useful modules:
|
||||
|
||||
# In[ ]:
|
||||
@@ -40,55 +40,55 @@ from pina.problem.zoo import SupervisedProblem
|
||||
|
||||
|
||||
# ## Learning Differential Operators via Neural Operator
|
||||
#
|
||||
#
|
||||
# In this tutorial, we explore how **Neural Operators** can be used to learn and approximate **differential operators**, which are fundamental in modeling physical and engineering systems governed by differential equations.
|
||||
#
|
||||
#
|
||||
# ### What Are Neural Operators?
|
||||
#
|
||||
#
|
||||
# **Neural Operators (NOs)** are a class of machine learning models designed to learn mappings *between function spaces*, unlike traditional neural networks which learn mappings between finite-dimensional vectors. In the context of differential equations, this means a Neural Operator can learn the **solution operator**:
|
||||
# $$
|
||||
# \mathcal{G}(a) = u,
|
||||
# $$
|
||||
# where $a$ is an input function (e.g., a PDE coefficient) and $u$ is the solution function.
|
||||
#
|
||||
#
|
||||
# ### Why Are Neural Operators Useful?
|
||||
#
|
||||
#
|
||||
# - **Mesh-free learning**: Neural Operators work directly with functions, allowing them to generalize across different spatial resolutions or grids.
|
||||
# - **Fast inference**: Once trained, they can predict the solution of a PDE for new input data almost instantaneously.
|
||||
# - **Physics-aware extensions**: Some variants can incorporate physical laws and constraints into the training process, improving accuracy and generalization.
|
||||
#
|
||||
#
|
||||
# ## Learning the 1D Advection Equation with a Neural Operator
|
||||
#
|
||||
#
|
||||
# To make things concrete, we'll a Neural Operator to learn the 1D advection equation. We generate synthetic data based on the analytical solution:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# \frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = 0
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# For a given initial condition $u(x, 0)$, the exact solution at time $t$ is:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# u(x, t) = u(x - ct)
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# We use this property to generate training data without solving the PDE numerically.
|
||||
#
|
||||
#
|
||||
# ### Problem Setup
|
||||
#
|
||||
#
|
||||
# 1. **Define the spatial domain**: We work on a 1D grid $x \in [0, 1]$ with periodic boundary conditions.
|
||||
#
|
||||
#
|
||||
# 2. **Generate initial conditions**: Each initial condition $u(x, 0)$ is created as a sum of sine waves with random amplitudes and phases:
|
||||
# $$
|
||||
# u(x, 0) = \sum_{k=1}^K A_k \sin(2\pi k x + \phi_k)
|
||||
# $$
|
||||
# where $A_k \in [0, 0.5]$ and $\phi_k \in [0, 2\pi]$ are sampled randomly for each sample.
|
||||
#
|
||||
# 3. **Compute the solution at time $t$**:
|
||||
#
|
||||
# 3. **Compute the solution at time $t$**:
|
||||
# Using the analytical solution, we shift each initial condition by $t=0.5$ ($c=1$), applying periodic wrap-around:
|
||||
# $$
|
||||
# u(x, t=0.5) = u(x - 0.5)
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# 4. **Create input-output pairs**: The input to the model is the function $u(x, 0)$, and the target output is $u(x, 0.5)$. These pairs can be used to train a Neural Operator to learn the underlying differential operator.
|
||||
|
||||
# In[18]:
|
||||
@@ -123,59 +123,59 @@ plt.grid(True)
|
||||
|
||||
|
||||
# ## Solving the Neural Operator Problem
|
||||
#
|
||||
#
|
||||
# At their core, **Neural Operators** transform an input function $a$ into an output function $u$. The general structure of a Neural Operator consists of three key components:
|
||||
#
|
||||
#
|
||||
# <p align="center">
|
||||
# <img src="http://raw.githubusercontent.com/mathLab/PINA/master/tutorials/static/neural_operator.png" alt="Neural Operators" width="800"/>
|
||||
# </p>
|
||||
#
|
||||
#
|
||||
# 1. **Encoder**: The encoder maps the input into a specific embedding space.
|
||||
#
|
||||
# 2. **Processor**: The processor consists of multiple layers performing **function convolutions**, which is the core computational unit in a Neural Operator.
|
||||
#
|
||||
# 2. **Processor**: The processor consists of multiple layers performing **function convolutions**, which is the core computational unit in a Neural Operator.
|
||||
# 3. **Decoder**: The decoder maps the processor's output back into the desired output space.
|
||||
#
|
||||
#
|
||||
# By varying the design and implementation of these three components — encoder, processor, and decoder — different Neural Operators are created, each tailored for specific applications or types of data.
|
||||
#
|
||||
#
|
||||
# ### Types of Neural Operators
|
||||
#
|
||||
#
|
||||
# Different variants of Neural Operators are designed to solve specific tasks. Some prominent examples include:
|
||||
#
|
||||
# - **Fourier Neural Operator (FNO)**:
|
||||
# The **Fourier Neural Operator** utilizes the **Fourier transform** in the processor to perform global convolutions. This enables the operator to capture long-range dependencies efficiently. FNOs are particularly useful for problems with periodic data or problems where global patterns and interactions are important.
|
||||
#
|
||||
# - **Fourier Neural Operator (FNO)**:
|
||||
# The **Fourier Neural Operator** utilizes the **Fourier transform** in the processor to perform global convolutions. This enables the operator to capture long-range dependencies efficiently. FNOs are particularly useful for problems with periodic data or problems where global patterns and interactions are important.
|
||||
# ➤ [Learn more about FNO](https://mathlab.github.io/PINA/_rst/model/fourier_neural_operator.html).
|
||||
#
|
||||
# - **Graph Neural Operator (GNO)**:
|
||||
# The **Graph Neural Operator** leverages **Graph Neural Networks (GNNs)** to exchange information between nodes, enabling the operator to perform convolutions on unstructured domains, such as graphs or meshes. GNOs are especially useful for problems that naturally involve irregular data, such as graph-based datasets or data on non-Euclidean spaces.
|
||||
#
|
||||
# - **Graph Neural Operator (GNO)**:
|
||||
# The **Graph Neural Operator** leverages **Graph Neural Networks (GNNs)** to exchange information between nodes, enabling the operator to perform convolutions on unstructured domains, such as graphs or meshes. GNOs are especially useful for problems that naturally involve irregular data, such as graph-based datasets or data on non-Euclidean spaces.
|
||||
# ➤ [Learn more about GNO](https://mathlab.github.io/PINA/_rst/model/graph_neural_operator.html).
|
||||
#
|
||||
# - **Deep Operator Network (DeepONet)**:
|
||||
#
|
||||
# - **Deep Operator Network (DeepONet)**:
|
||||
# **DeepONet** is a variant of Neural Operators designed to solve operator equations by learning mappings between input and output functions. Unlike other Neural Operators, **DeepONet** does not use the typical encoder-processor-decoder structure. Instead, it uses two distinct neural networks:
|
||||
#
|
||||
#
|
||||
# 1. **Branch Network**: Takes the **function inputs** (e.g., $u(x)$) and learns a feature map of the input function.
|
||||
# 2. **Trunk Network**: Takes the **spatial locations** (e.g., $x$) and maps them to the output space.
|
||||
#
|
||||
# The output of **DeepONet** is the combination of these two networks' outputs, which together provide the mapping from the input function to the output function.
|
||||
#
|
||||
# The output of **DeepONet** is the combination of these two networks' outputs, which together provide the mapping from the input function to the output function.
|
||||
# ➤ [Learn more about DeepONet](https://mathlab.github.io/PINA/_rst/model/deeponet.html).
|
||||
#
|
||||
#
|
||||
# In this tutorial we will focus on Neural Operator which follow the Encoder - Processor - Decoder structure, which we call *Kernel* Neural Operator. Implementing kernel neural Operators in PINA is very simple, you just need to use the `KernelNeuralOperator` API.
|
||||
#
|
||||
#
|
||||
# ### KernelNeuralOperator API
|
||||
# The `KernelNeuralOperator` API requires three parameters:
|
||||
#
|
||||
# The `KernelNeuralOperator` API requires three parameters:
|
||||
#
|
||||
# 1. `lifting_operator`: a `torch.nn.Module` apping the input to its hidden dimension (Encoder).
|
||||
#
|
||||
#
|
||||
# 2. `integral_kernels`: a `torch.nn.Module` representing the integral kernels mapping each hidden representation to the next one.
|
||||
#
|
||||
#
|
||||
# 3. `projection_operator`: a `torch.nn.Module` representing the hidden representation to the output function.
|
||||
#
|
||||
#
|
||||
# To construct the kernel, you can use the Neural Operator Blocks available in PINA (see [here](https://mathlab.github.io/PINA/_rst/_code.html#blocks)) or implement you own one! Let's build a simple FNO using the `FourierBlock1D`. In particular we will:
|
||||
#
|
||||
#
|
||||
# 1. Define the encoder, a simple linear layer mapping the input dimension to the hidden dimension
|
||||
# 2. Define the decoder, two linear layers mapping the hidden dimension to 128 and back to the input dimension
|
||||
# 3. Define the processor, a two layer Fourier block with a specific hidden dimension.
|
||||
# 4. Combine the encoder-processor-decoder using the `KernelNeuralOperator` API to create the `model`.
|
||||
#
|
||||
#
|
||||
|
||||
# In[23]:
|
||||
|
||||
@@ -236,9 +236,9 @@ model = KernelNeuralOperator(
|
||||
|
||||
|
||||
# Done! Let's now solve the Neural Operator problem. The problem we will define is a basic `SupervisedProblem`, and we will use the `SupervisedSolver` to train the Neural Operator.
|
||||
#
|
||||
#
|
||||
# > **👉 We have a dedicated [tutorial](https://mathlab.github.io/PINA/tutorial16/tutorial.html) to teach how to build a Problem from scratch — have a look if you're interested!**
|
||||
#
|
||||
#
|
||||
# > **👉 We have a dedicated [tutorial](http://mathlab.github.io/PINA/_rst/tutorials/tutorial18/tutorial.html) for an overview of Solvers in PINA — have a look if you're interested!**
|
||||
|
||||
# In[24]:
|
||||
@@ -265,7 +265,7 @@ _ = trainer.test()
|
||||
|
||||
|
||||
# ## Visualizing the Predictions
|
||||
#
|
||||
#
|
||||
# As we can see, we have achieved a very low MSE, even after training for only one epoch. Now, we will visualize the results in the same way as we did previously:
|
||||
|
||||
# In[30]:
|
||||
@@ -288,15 +288,15 @@ plt.grid(True)
|
||||
|
||||
|
||||
# Nice! We can see that the network is correctly learning the solution operator and it was very simple!
|
||||
#
|
||||
#
|
||||
# ## What's Next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the introductory tutorial on Neural Operators! Now that you have a solid foundation, here are a few directions you can explore:
|
||||
#
|
||||
#
|
||||
# 1. **Experiment with Training Duration & Network Architecture** — Try different training durations and tweak the network architecture to optimize performance. Choose different integral kernels and see how the results vary.
|
||||
#
|
||||
#
|
||||
# 2. **Explore Other Models in `pina.model`** — Check out other models available in `pina.model` or design your own custom PyTorch module to suit your needs. What about trying a `DeepONet`?
|
||||
#
|
||||
#
|
||||
# 3. **...and many more!** — The possibilities are vast! Continue experimenting with advanced configurations, solvers, and features in PINA. For example, consider incorporating physics-informed terms during training to enhance model generalization.
|
||||
#
|
||||
#
|
||||
# For more resources and tutorials, check out the [PINA Documentation](https://mathlab.github.io/PINA/).
|
||||
|
||||
Reference in New Issue
Block a user