72
tutorials/tutorial1/tutorial.ipynb
vendored
72
tutorials/tutorial1/tutorial.ipynb
vendored
File diff suppressed because one or more lines are too long
20
tutorials/tutorial1/tutorial.py
vendored
20
tutorials/tutorial1/tutorial.py
vendored
@@ -69,7 +69,7 @@
|
||||
#
|
||||
# Once the problem class is initialized we need to write the differential equation in PINA language. For doing this we need to load the pina operators found in `pina.operators` module. Let's again consider the Equation (1) and try to write the PINA model class:
|
||||
|
||||
# In[14]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
from pina.problem import SpatialProblem
|
||||
@@ -110,8 +110,8 @@ class SimpleODE(SpatialProblem):
|
||||
|
||||
# Conditions to hold
|
||||
conditions = {
|
||||
'x0': Condition(Span({'x': 0.}), initial_condition),
|
||||
'D': Condition(Span({'x': [0, 1]}), ode_equation),
|
||||
'x0': Condition(location=Span({'x': 0.}), function=initial_condition),
|
||||
'D': Condition(location=Span({'x': [0, 1]}), function=ode_equation),
|
||||
}
|
||||
|
||||
# defining true solution
|
||||
@@ -129,7 +129,7 @@ class SimpleODE(SpatialProblem):
|
||||
|
||||
# The basics requirements for building a PINN model are a problem and a model. We have already covered the problem definition. For the model one can use the default models provided in PINA or use a custom model. We will not go into the details of model definition, Tutorial2 and Tutorial3 treat the topic in detail.
|
||||
|
||||
# In[31]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
from pina.model import FeedForward
|
||||
@@ -157,7 +157,7 @@ pinn = PINN(problem, model)
|
||||
# Once the `pinn` object is created, we need to generate the points for starting the optimization. For doing this we use the `span_pts` method of the `PINN` class.
|
||||
# Let's see some methods to sample in $(0,1 )$.
|
||||
|
||||
# In[32]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
# sampling 20 points in (0, 1) with discrite step
|
||||
@@ -172,7 +172,7 @@ pinn.span_pts(20, 'random', locations=['D'])
|
||||
|
||||
# We can also use a dictionary for specific variables:
|
||||
|
||||
# In[33]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
pinn.span_pts({'variables': ['x'], 'mode': 'grid', 'n': 20}, locations=['D'])
|
||||
@@ -180,7 +180,7 @@ pinn.span_pts({'variables': ['x'], 'mode': 'grid', 'n': 20}, locations=['D'])
|
||||
|
||||
# We are going to use equispaced points for sampling. We need to sample in all the conditions domains. In our case we sample in `D` and `x0`.
|
||||
|
||||
# In[34]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
# sampling for training
|
||||
@@ -192,7 +192,7 @@ pinn.span_pts(20, 'grid', locations=['D'])
|
||||
#
|
||||
# Once we have defined the PINA model, created a network and sampled points in the domain, we have everything that is necessary for training a PINN. Here we show a very short training and some method for plotting the results.
|
||||
|
||||
# In[35]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
# simple training
|
||||
@@ -201,7 +201,7 @@ final_loss = pinn.train(stop=3000, frequency_print=1000)
|
||||
|
||||
# After the training we have saved the final loss in `final_loss`, which we can inspect. By default PINA uses mean square error loss.
|
||||
|
||||
# In[36]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
# inspecting final loss
|
||||
@@ -210,7 +210,7 @@ final_loss
|
||||
|
||||
# By using the `Plotter` class from PINA we can also do some quatitative plots of the loss function.
|
||||
|
||||
# In[37]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
from pina.plotter import Plotter
|
||||
|
||||
371
tutorials/tutorial2/tutorial.ipynb
vendored
371
tutorials/tutorial2/tutorial.ipynb
vendored
File diff suppressed because one or more lines are too long
34
tutorials/tutorial2/tutorial.py
vendored
34
tutorials/tutorial2/tutorial.py
vendored
@@ -18,11 +18,11 @@
|
||||
|
||||
# First of all, some useful imports.
|
||||
|
||||
# In[1]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
import torch
|
||||
from torch.nn import ReLU, Tanh, Softplus
|
||||
from torch.nn import Softplus
|
||||
|
||||
from pina.problem import SpatialProblem
|
||||
from pina.operators import nabla
|
||||
@@ -33,7 +33,7 @@ from pina import Condition, Span, PINN, LabelTensor, Plotter
|
||||
# Now, 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. *truth_solution*
|
||||
# is the exact solution which will be compared with the predicted one.
|
||||
|
||||
# In[2]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
class Poisson(SpatialProblem):
|
||||
@@ -51,11 +51,11 @@ class Poisson(SpatialProblem):
|
||||
return output_.extract(['u']) - value
|
||||
|
||||
conditions = {
|
||||
'gamma1': Condition(Span({'x': [0, 1], 'y': 1}), nil_dirichlet),
|
||||
'gamma2': Condition(Span({'x': [0, 1], 'y': 0}), nil_dirichlet),
|
||||
'gamma3': Condition(Span({'x': 1, 'y': [0, 1]}), nil_dirichlet),
|
||||
'gamma4': Condition(Span({'x': 0, 'y': [0, 1]}), nil_dirichlet),
|
||||
'D': Condition(Span({'x': [0, 1], 'y': [0, 1]}), laplace_equation),
|
||||
'gamma1': Condition(location=Span({'x': [0, 1], 'y': 1}), function=nil_dirichlet),
|
||||
'gamma2': Condition(location=Span({'x': [0, 1], 'y': 0}), function=nil_dirichlet),
|
||||
'gamma3': Condition(location=Span({'x': 1, 'y': [0, 1]}), function=nil_dirichlet),
|
||||
'gamma4': Condition(location=Span({'x': 0, 'y': [0, 1]}), function=nil_dirichlet),
|
||||
'D': Condition(location=Span({'x': [0, 1], 'y': [0, 1]}), function=laplace_equation),
|
||||
}
|
||||
|
||||
def poisson_sol(self, pts):
|
||||
@@ -75,7 +75,7 @@ class Poisson(SpatialProblem):
|
||||
# The output of the cell below is the final loss of the training phase of the PINN.
|
||||
# We highlight that the generation of the sampling points and the train is here encapsulated within the function `generate_samples_and_train`, but only for saving some lines of code in the next cells; that function is not mandatory in the **PINA** framework.
|
||||
|
||||
# In[3]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
def generate_samples_and_train(model, problem):
|
||||
@@ -98,7 +98,7 @@ pinn = generate_samples_and_train(model, problem)
|
||||
|
||||
# The neural network of course can be saved in a file. In such a way, we can store it after the train, and load it just to infer the field. Here we don't store the model, but for demonstrative purposes we put in the next cell the commented line of code.
|
||||
|
||||
# In[4]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
# pinn.save_state('pina.poisson')
|
||||
@@ -107,7 +107,7 @@ pinn = generate_samples_and_train(model, problem)
|
||||
# Now the *Plotter* class is used to plot the results.
|
||||
# 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[5]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
plotter = Plotter()
|
||||
@@ -131,7 +131,7 @@ plotter.plot(pinn)
|
||||
#
|
||||
# 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]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
class SinSin(torch.nn.Module):
|
||||
@@ -158,7 +158,7 @@ pinn_feat = generate_samples_and_train(model_feat, problem)
|
||||
# The predicted and exact solutions and the error between them are represented below.
|
||||
# We can easily note that now our network, having almost the same condition as before, is able to reach an additional order of magnitude in accuracy.
|
||||
|
||||
# In[7]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
plotter.plot(pinn_feat)
|
||||
@@ -178,7 +178,7 @@ plotter.plot(pinn_feat)
|
||||
# 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!
|
||||
|
||||
# In[8]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
class SinSinAB(torch.nn.Module):
|
||||
@@ -209,7 +209,7 @@ pinn_learn = generate_samples_and_train(model_learn, problem)
|
||||
|
||||
# Umh, the final loss is not appreciabily better than previous model (with static extra features), despite the usage of learnable parameters. This is mainly due to the over-parametrization of the network: there are many parameter to optimize during the training, and the model in unable to understand automatically that only the parameters of the extra feature (and not the weights/bias of the FFN) should be tuned in order to fit our problem. A longer training can be helpful, but in this case the faster way to reach machine precision for solving the Poisson problem is removing all the hidden layers in the `FeedForward`, keeping only the $\alpha$ and $\beta$ parameters of the extra feature.
|
||||
|
||||
# In[9]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
model_learn = FeedForward(
|
||||
@@ -227,13 +227,13 @@ pinn_learn = generate_samples_and_train(model_learn, problem)
|
||||
#
|
||||
# We conclude here by showing the graphical comparison of the unknown field and the loss trend for all the test cases presented here: the standard PINN, PINN with extra features, and PINN with learnable extra features.
|
||||
|
||||
# In[10]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
plotter.plot(pinn_learn)
|
||||
|
||||
|
||||
# In[11]:
|
||||
# In[ ]:
|
||||
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
94
tutorials/tutorial3/tutorial.ipynb
vendored
94
tutorials/tutorial3/tutorial.ipynb
vendored
File diff suppressed because one or more lines are too long
24
tutorials/tutorial3/tutorial.py
vendored
24
tutorials/tutorial3/tutorial.py
vendored
@@ -21,7 +21,7 @@
|
||||
|
||||
# First of all, some useful imports.
|
||||
|
||||
# In[2]:
|
||||
# In[1]:
|
||||
|
||||
|
||||
import torch
|
||||
@@ -34,7 +34,7 @@ from pina import Condition, Span, PINN, Plotter
|
||||
|
||||
# Now, the wave problem is written in PINA code as a class, inheriting from `SpatialProblem` and `TimeDependentProblem` since we deal with spatial, and time dependent variables. The equations are written as `conditions` that should be satisfied in the corresponding domains. `truth_solution` is the exact solution which will be compared with the predicted one.
|
||||
|
||||
# In[3]:
|
||||
# In[2]:
|
||||
|
||||
|
||||
class Wave(TimeDependentProblem, SpatialProblem):
|
||||
@@ -58,12 +58,12 @@ class Wave(TimeDependentProblem, SpatialProblem):
|
||||
return output_.extract(['u']) - u_expected
|
||||
|
||||
conditions = {
|
||||
'gamma1': Condition(Span({'x': [0, 1], 'y': 1, 't': [0, 1]}), nil_dirichlet),
|
||||
'gamma2': Condition(Span({'x': [0, 1], 'y': 0, 't': [0, 1]}), nil_dirichlet),
|
||||
'gamma3': Condition(Span({'x': 1, 'y': [0, 1], 't': [0, 1]}), nil_dirichlet),
|
||||
'gamma4': Condition(Span({'x': 0, 'y': [0, 1], 't': [0, 1]}), nil_dirichlet),
|
||||
't0': Condition(Span({'x': [0, 1], 'y': [0, 1], 't': 0}), initial_condition),
|
||||
'D': Condition(Span({'x': [0, 1], 'y': [0, 1], 't': [0, 1]}), wave_equation),
|
||||
'gamma1': Condition(location=Span({'x': [0, 1], 'y': 1, 't': [0, 1]}), function=nil_dirichlet),
|
||||
'gamma2': Condition(location=Span({'x': [0, 1], 'y': 0, 't': [0, 1]}), function=nil_dirichlet),
|
||||
'gamma3': Condition(location=Span({'x': 1, 'y': [0, 1], 't': [0, 1]}), function=nil_dirichlet),
|
||||
'gamma4': Condition(location=Span({'x': 0, 'y': [0, 1], 't': [0, 1]}), function=nil_dirichlet),
|
||||
't0': Condition(location=Span({'x': [0, 1], 'y': [0, 1], 't': 0}), function=initial_condition),
|
||||
'D': Condition(location=Span({'x': [0, 1], 'y': [0, 1], 't': [0, 1]}), function=wave_equation),
|
||||
}
|
||||
|
||||
def wave_sol(self, pts):
|
||||
@@ -80,7 +80,7 @@ problem = Wave()
|
||||
#
|
||||
# This neural network takes as input the coordinates (in this case $x$, $y$ and $t$) and provides the unkwown field of the Wave problem. The residual of the equations are evaluated at several sampling points (which the user can manipulate using the method `span_pts`) and the loss minimized by the neural network is the sum of the residuals.
|
||||
|
||||
# In[4]:
|
||||
# In[3]:
|
||||
|
||||
|
||||
class TorchNet(torch.nn.Module):
|
||||
@@ -109,7 +109,7 @@ model = Network(model = TorchNet(),
|
||||
# In this tutorial, the neural network is trained for 2000 epochs with a learning rate of 0.001. These parameters can be modified as desired.
|
||||
# We highlight that the generation of the sampling points and the train is here encapsulated within the function `generate_samples_and_train`, but only for saving some lines of code in the next cells; that function is not mandatory in the **PINA** framework. The training takes approximately one minute.
|
||||
|
||||
# In[5]:
|
||||
# In[7]:
|
||||
|
||||
|
||||
def generate_samples_and_train(model, problem):
|
||||
@@ -126,7 +126,7 @@ pinn = generate_samples_and_train(model, problem)
|
||||
|
||||
# After the training is completed one can now plot some results using the `Plotter` class of **PINA**.
|
||||
|
||||
# In[11]:
|
||||
# In[8]:
|
||||
|
||||
|
||||
plotter = Plotter()
|
||||
@@ -137,7 +137,7 @@ plotter.plot(pinn, fixed_variables={'t': 0.6})
|
||||
|
||||
# We can also plot the pinn loss during the training to see the decrease.
|
||||
|
||||
# In[12]:
|
||||
# In[9]:
|
||||
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
Reference in New Issue
Block a user