committed by
GitHub
parent
5aec5f52c6
commit
4357f8681f
44
tutorials/tutorial13/tutorial.py
vendored
44
tutorials/tutorial13/tutorial.py
vendored
@@ -2,15 +2,15 @@
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: Multiscale PDE learning with Fourier Feature Network
|
||||
#
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial13/tutorial.ipynb)
|
||||
#
|
||||
#
|
||||
# This tutorial presents how to solve with Physics-Informed Neural Networks (PINNs)
|
||||
# a PDE characterized by multiscale behaviour, as
|
||||
# presented in [*On the eigenvector bias of Fourier feature networks: From regression to solving
|
||||
# multi-scale PDEs with physics-informed neural networks*](
|
||||
# https://doi.org/10.1016/j.cma.2021.113938).
|
||||
#
|
||||
# https://doi.org/10.1016/j.cma.2021.113938).
|
||||
#
|
||||
# First of all, some useful imports.
|
||||
|
||||
# In[ ]:
|
||||
@@ -44,24 +44,24 @@ warnings.filterwarnings("ignore")
|
||||
|
||||
|
||||
# ## Multiscale Problem
|
||||
#
|
||||
#
|
||||
# We begin by presenting the problem which also can be found in Section 2 of [*On the eigenvector bias of Fourier feature networks: From regression to solving
|
||||
# multi-scale PDEs with physics-informed neural networks*](
|
||||
# https://doi.org/10.1016/j.cma.2021.113938). The one-dimensional Poisson problem we aim to solve is mathematically written as:
|
||||
#
|
||||
#
|
||||
# \begin{equation}
|
||||
# \begin{cases}
|
||||
# \Delta u (x) + f(x) = 0 \quad x \in [0,1], \\
|
||||
# u(x) = 0 \quad x \in \partial[0,1], \\
|
||||
# \end{cases}
|
||||
# \end{equation}
|
||||
#
|
||||
#
|
||||
# We impose the solution as $u(x) = \sin(2\pi x) + 0.1 \sin(50\pi x)$ and obtain the force term $f(x) = (2\pi)^2 \sin(2\pi x) + 0.1 (50 \pi)^2 \sin(50\pi x)$.
|
||||
# Though this example is simple and pedagogical, it is worth noting that
|
||||
# the solution exhibits low frequency in the macro-scale and high frequency in the micro-scale, which resembles many
|
||||
# practical scenarios.
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# In **PINA** this problem is written, as always, as a class [see here for a tutorial on the Problem class](https://mathlab.github.io/PINA/_rst/tutorials/tutorial1/tutorial.html). Below you can find the `Poisson` problem which is mathmatically described above.
|
||||
|
||||
# In[2]:
|
||||
@@ -110,8 +110,8 @@ problem.discretise_domain(1, "grid", domains=["bound_cond0", "bound_cond1"])
|
||||
|
||||
# A standard PINN approach would be to fit this model using a Feed Forward (fully connected) Neural Network. For a conventional fully-connected neural network is easy to
|
||||
# approximate a function $u$, given sufficient data inside the computational domain. However solving high-frequency or multi-scale problems presents great challenges to PINNs especially when the number of data cannot capture the different scales.
|
||||
#
|
||||
# Below we run a simulation using the `PINN` solver and the self adaptive `SAPINN` solver, using a [`FeedForward`](https://mathlab.github.io/PINA/_modules/pina/model/feed_forward.html#FeedForward) model.
|
||||
#
|
||||
# Below we run a simulation using the `PINN` solver and the self adaptive `SAPINN` solver, using a [`FeedForward`](https://mathlab.github.io/PINA/_modules/pina/model/feed_forward.html#FeedForward) model.
|
||||
|
||||
# In[3]:
|
||||
|
||||
@@ -177,7 +177,7 @@ plot_solution(sapinn, "Self Adaptive PINN solution")
|
||||
|
||||
|
||||
# We can clearly see that the solution has not been learned by the two different solvers. Indeed the big problem is not in the optimization strategy (i.e. the solver), but in the model used to solve the problem. A simple `FeedForward` network can hardly handle multiscales if not enough collocation points are used!
|
||||
#
|
||||
#
|
||||
# We can also compute the $l_2$ relative error for the `PINN` and `SAPINN` solutions:
|
||||
|
||||
# In[5]:
|
||||
@@ -204,11 +204,11 @@ print(
|
||||
# first introduced in [*On the eigenvector bias of Fourier feature networks: From regression to solving
|
||||
# multi-scale PDEs with physics-informed neural networks*](
|
||||
# https://doi.org/10.1016/j.cma.2021.113938) showing great results for multiscale problems. The basic idea is to map the input $\mathbf{x}$ into an embedding $\tilde{\mathbf{x}}$ where:
|
||||
#
|
||||
#
|
||||
# $$ \tilde{\mathbf{x}} =\left[\cos\left( \mathbf{B} \mathbf{x} \right), \sin\left( \mathbf{B} \mathbf{x} \right)\right] $$
|
||||
#
|
||||
# and $\mathbf{B}_{ij} \sim \mathcal{N}(0, \sigma^2)$. This simple operation allow the network to learn on multiple scales!
|
||||
#
|
||||
#
|
||||
# and $\mathbf{B}_{ij} \sim \mathcal{N}(0, \sigma^2)$. This simple operation allow the network to learn on multiple scales!
|
||||
#
|
||||
# In PINA we already have implemented the feature as a `layer` called [`FourierFeatureEmbedding`](https://mathlab.github.io/PINA/_rst/layers/fourier_embedding.html). Below we will build the *Multi-scale Fourier Feature Architecture*. In this architecture multiple Fourier feature embeddings (initialized with different $\sigma$)
|
||||
# are applied to input coordinates and then passed through the same fully-connected neural network, before the outputs are finally concatenated with a linear layer.
|
||||
|
||||
@@ -269,15 +269,15 @@ print(
|
||||
|
||||
|
||||
# It is pretty clear that the network has learned the correct solution, with also a very low error. Obviously a longer training and a more expressive neural network could improve the results!
|
||||
#
|
||||
#
|
||||
# ## What's next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the one dimensional Poisson tutorial of **PINA** using `FourierFeatureEmbedding`! 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. Understand the role of `sigma` in `FourierFeatureEmbedding` (see original paper for a nice reference)
|
||||
#
|
||||
#
|
||||
# 3. Code the *Spatio-temporal multi-scale Fourier feature architecture* for a more complex time dependent PDE (section 3 of the original reference)
|
||||
#
|
||||
#
|
||||
# 4. Many more...
|
||||
|
||||
Reference in New Issue
Block a user