committed by
FilippoOlivo
parent
3684782fb5
commit
578c5bc2f4
40
tutorials/tutorial2/tutorial.py
vendored
40
tutorials/tutorial2/tutorial.py
vendored
@@ -2,11 +2,11 @@
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: Two dimensional Poisson problem using Extra Features Learning
|
||||
#
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial2/tutorial.ipynb)
|
||||
#
|
||||
#
|
||||
# This tutorial presents how to solve with Physics-Informed Neural Networks (PINNs) a 2D Poisson problem with Dirichlet boundary conditions. We will train with standard PINN's training, and with extrafeatures. For more insights on extrafeature learning please read [*An extended physics informed neural network for preliminary analysis of parametric optimal control problems*](https://www.sciencedirect.com/science/article/abs/pii/S0898122123002018).
|
||||
#
|
||||
#
|
||||
# First of all, some useful imports.
|
||||
|
||||
# In[ ]:
|
||||
@@ -44,10 +44,10 @@ warnings.filterwarnings("ignore")
|
||||
# \end{cases}
|
||||
# \end{equation}
|
||||
# where $D$ is a square domain $[0,1]^2$, and $\Gamma_i$, with $i=1,...,4$, are the boundaries of the square.
|
||||
#
|
||||
#
|
||||
# The Poisson problem is written in **PINA** code as a class. The equations are written as *conditions* that should be satisfied in the corresponding domains. The *solution*
|
||||
# is the exact solution which will be compared with the predicted one. If interested in how to write problems see [this tutorial](https://mathlab.github.io/PINA/_rst/tutorials/tutorial1/tutorial.html).
|
||||
#
|
||||
#
|
||||
# We will directly import the problem from `pina.problem.zoo`, which contains a vast list of PINN problems and more.
|
||||
|
||||
# In[2]:
|
||||
@@ -76,7 +76,7 @@ problem.discretise_domain(
|
||||
# ## Solving the problem with standard PINNs
|
||||
|
||||
# After the problem, the feed-forward neural network is defined, through the class `FeedForward`. This neural network takes as input the coordinates (in this case $x$ and $y$) and provides the unkwown field of the Poisson problem. The residual of the equations are evaluated at several sampling points and the loss minimized by the neural network is the sum of the residuals.
|
||||
#
|
||||
#
|
||||
# In this tutorial, the neural network is composed by two hidden layers of 10 neurons each, and it is trained for 1000 epochs with a learning rate of 0.006 and $l_2$ weight regularization set to $10^{-8}$. These parameters can be modified as desired. We set the `train_size` to 0.8 and `test_size` to 0.2, this mean that the discretised points will be divided in a 80%-20% fashion, where 80% will be used for training and the remaining 20% for testing.
|
||||
|
||||
# In[3]:
|
||||
@@ -112,7 +112,7 @@ trainer_base.train()
|
||||
|
||||
|
||||
# Now we plot the results using `matplotlib`.
|
||||
# The solution predicted by the neural network is plotted on the left, the exact one is represented at the center and on the right the error between the exact and the predicted solutions is showed.
|
||||
# The solution predicted by the neural network is plotted on the left, the exact one is represented at the center and on the right the error between the exact and the predicted solutions is showed.
|
||||
|
||||
# In[4]:
|
||||
|
||||
@@ -157,17 +157,17 @@ plot_solution(solver=pinn)
|
||||
# ## Solving the problem with extra-features PINNs
|
||||
|
||||
# Now, the same problem is solved in a different way.
|
||||
# A new neural network is now defined, with an additional input variable, named extra-feature, which coincides with the forcing term in the Laplace equation.
|
||||
# A new neural network is now defined, with an additional input variable, named extra-feature, which coincides with the forcing term in the Laplace equation.
|
||||
# The set of input variables to the neural network is:
|
||||
#
|
||||
#
|
||||
# \begin{equation}
|
||||
# [x, y, k(x, y)], \text{ with } k(x, y)= 2\pi^2\sin{(\pi x)}\sin{(\pi y)},
|
||||
# \end{equation}
|
||||
#
|
||||
#
|
||||
# where $x$ and $y$ are the spatial coordinates and $k(x, y)$ is the added feature which is equal to the forcing term.
|
||||
#
|
||||
#
|
||||
# This feature is initialized in the class `SinSin`, which is a simple `torch.nn.Module`. After declaring such feature, we can just adjust the `FeedForward` class by creating a subclass `FeedForwardWithExtraFeatures` with an adjusted forward method and the additional attribute `extra_features`.
|
||||
#
|
||||
#
|
||||
# Finally, we perform the same training as before: the problem is `Poisson`, the network is composed by the same number of neurons and optimizer parameters are equal to previous test, the only change is the new extra feature.
|
||||
|
||||
# In[6]:
|
||||
@@ -236,14 +236,14 @@ plot_solution(solver=pinn_feat)
|
||||
# ## Solving the problem with learnable extra-features PINNs
|
||||
|
||||
# We can still do better!
|
||||
#
|
||||
#
|
||||
# Another way to exploit the extra features is the addition of learnable parameter inside them.
|
||||
# In this way, the added parameters are learned during the training phase of the neural network. In this case, we use:
|
||||
#
|
||||
#
|
||||
# \begin{equation}
|
||||
# k(x, \mathbf{y}) = \beta \sin{(\alpha x)} \sin{(\alpha y)},
|
||||
# \end{equation}
|
||||
#
|
||||
#
|
||||
# where $\alpha$ and $\beta$ are the abovementioned parameters.
|
||||
# Their implementation is quite trivial: by using the class `torch.nn.Parameter` we cam define all the learnable parameters we need, and they are managed by `autograd` module!
|
||||
|
||||
@@ -348,13 +348,13 @@ _ = trainer_learn.test()
|
||||
|
||||
|
||||
# ## What's next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the two dimensional Poisson tutorial of **PINA**! There are multiple directions you can go now:
|
||||
#
|
||||
#
|
||||
# 1. Train the network for longer or with different layer sizes and assert the finaly accuracy
|
||||
#
|
||||
#
|
||||
# 2. Propose new types of extrafeatures and see how they affect the learning
|
||||
#
|
||||
#
|
||||
# 3. Exploit extrafeature training in more complex problems
|
||||
#
|
||||
#
|
||||
# 4. Many more...
|
||||
|
||||
Reference in New Issue
Block a user