fix bug network
This commit is contained in:
committed by
Nicola Demo
parent
ee39b39805
commit
a9f14ac323
62
tutorials/tutorial5/tutorial.ipynb
vendored
62
tutorials/tutorial5/tutorial.ipynb
vendored
@@ -19,7 +19,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 1,
|
||||
"id": "5f2744dc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -28,8 +28,7 @@
|
||||
"from scipy import io\n",
|
||||
"import torch\n",
|
||||
"from pina.model import FNO, FeedForward # let's import some models\n",
|
||||
"from pina import Condition\n",
|
||||
"from pina import LabelTensor\n",
|
||||
"from pina import Condition, LabelTensor\n",
|
||||
"from pina.solvers import SupervisedSolver\n",
|
||||
"from pina.trainer import Trainer\n",
|
||||
"from pina.problem import AbstractProblem\n",
|
||||
@@ -63,10 +62,10 @@
|
||||
"data = io.loadmat(\"Data_Darcy.mat\")\n",
|
||||
"\n",
|
||||
"# extract data (we use only 100 data for train)\n",
|
||||
"k_train = torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1)\n",
|
||||
"u_train = torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1)\n",
|
||||
"k_test = torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1)\n",
|
||||
"u_test= torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1)\n",
|
||||
"k_train = LabelTensor(torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1), ['u0'])\n",
|
||||
"u_train = LabelTensor(torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1), ['u'])\n",
|
||||
"k_test = LabelTensor(torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1), ['u0'])\n",
|
||||
"u_test= LabelTensor(torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1), ['u'])\n",
|
||||
"x = torch.tensor(data['x'], dtype=torch.float)[0]\n",
|
||||
"y = torch.tensor(data['y'], dtype=torch.float)[0]"
|
||||
]
|
||||
@@ -116,16 +115,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 17,
|
||||
"id": "8b27d283",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class NeuralOperatorSolver(AbstractProblem):\n",
|
||||
" input_variables = ['u_0']\n",
|
||||
" output_variables = ['u']\n",
|
||||
" conditions = {'data' : Condition(input_points=LabelTensor(k_train, input_variables), \n",
|
||||
" output_points=LabelTensor(u_train, output_variables))}\n",
|
||||
" input_variables = k_train.labels\n",
|
||||
" output_variables = u_train.labels\n",
|
||||
" conditions = {'data' : Condition(input_points=k_train, \n",
|
||||
" output_points=u_train)}\n",
|
||||
"\n",
|
||||
"# make problem\n",
|
||||
"problem = NeuralOperatorSolver()"
|
||||
@@ -143,7 +142,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 18,
|
||||
"id": "e34f18b0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -161,7 +160,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 9: : 100it [00:00, 383.36it/s, v_num=36, mean_loss=0.108]"
|
||||
"Epoch 9: : 100it [00:00, 357.28it/s, v_num=1, mean_loss=0.108]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -175,7 +174,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 9: : 100it [00:00, 380.57it/s, v_num=36, mean_loss=0.108]\n"
|
||||
"Epoch 9: : 100it [00:00, 354.81it/s, v_num=1, mean_loss=0.108]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -202,7 +201,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 19,
|
||||
"id": "0e2a6aa4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -222,10 +221,10 @@
|
||||
"metric_err = LpLoss(relative=True)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"err = float(metric_err(u_train.squeeze(-1), solver.models[0](k_train).squeeze(-1)).mean())*100\n",
|
||||
"err = float(metric_err(u_train.squeeze(-1), solver.neural_net(k_train).squeeze(-1)).mean())*100\n",
|
||||
"print(f'Final error training {err:.2f}%')\n",
|
||||
"\n",
|
||||
"err = float(metric_err(u_test.squeeze(-1), solver.models[0](k_test).squeeze(-1)).mean())*100\n",
|
||||
"err = float(metric_err(u_test.squeeze(-1), solver.neural_net(k_test).squeeze(-1)).mean())*100\n",
|
||||
"print(f'Final error testing {err:.2f}%')"
|
||||
]
|
||||
},
|
||||
@@ -241,7 +240,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 24,
|
||||
"id": "9af523a5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -259,7 +258,14 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 9: : 100it [00:04, 22.13it/s, v_num=37, mean_loss=0.000952]"
|
||||
"Epoch 0: : 0it [00:00, ?it/s]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 9: : 100it [00:02, 47.76it/s, v_num=4, mean_loss=0.00106] "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -273,7 +279,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 9: : 100it [00:04, 22.07it/s, v_num=37, mean_loss=0.000952]\n"
|
||||
"Epoch 9: : 100it [00:02, 47.65it/s, v_num=4, mean_loss=0.00106]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -283,10 +289,10 @@
|
||||
"projecting_net = torch.nn.Linear(24, 1)\n",
|
||||
"model = FNO(lifting_net=lifting_net,\n",
|
||||
" projecting_net=projecting_net,\n",
|
||||
" n_modes=16,\n",
|
||||
" n_modes=8,\n",
|
||||
" dimensions=2,\n",
|
||||
" inner_size=24,\n",
|
||||
" padding=11)\n",
|
||||
" padding=8)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# make solver\n",
|
||||
@@ -307,7 +313,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 25,
|
||||
"id": "58e2db89",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -315,16 +321,16 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Final error training 4.45%\n",
|
||||
"Final error testing 4.91%\n"
|
||||
"Final error training 4.83%\n",
|
||||
"Final error testing 5.16%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"err = float(metric_err(u_train.squeeze(-1), solver.models[0](k_train).squeeze(-1)).mean())*100\n",
|
||||
"err = float(metric_err(u_train.squeeze(-1), solver.neural_net(k_train).squeeze(-1)).mean())*100\n",
|
||||
"print(f'Final error training {err:.2f}%')\n",
|
||||
"\n",
|
||||
"err = float(metric_err(u_test.squeeze(-1), solver.models[0](k_test).squeeze(-1)).mean())*100\n",
|
||||
"err = float(metric_err(u_test.squeeze(-1), solver.neural_net(k_test).squeeze(-1)).mean())*100\n",
|
||||
"print(f'Final error testing {err:.2f}%')"
|
||||
]
|
||||
},
|
||||
|
||||
43
tutorials/tutorial5/tutorial.py
vendored
43
tutorials/tutorial5/tutorial.py
vendored
@@ -6,15 +6,14 @@
|
||||
# In this tutorial we are going to solve the Darcy flow problem in two dimensions, presented in [*Fourier Neural Operator for
|
||||
# Parametric Partial Differential Equation*](https://openreview.net/pdf?id=c8P9NQVtmnO). First of all we import the modules needed for the tutorial. Importing `scipy` is needed for input output operations.
|
||||
|
||||
# In[11]:
|
||||
# In[1]:
|
||||
|
||||
|
||||
# !pip install scipy # install scipy
|
||||
from scipy import io
|
||||
import torch
|
||||
from pina.model import FNO, FeedForward # let's import some models
|
||||
from pina import Condition
|
||||
from pina import LabelTensor
|
||||
from pina import Condition, LabelTensor
|
||||
from pina.solvers import SupervisedSolver
|
||||
from pina.trainer import Trainer
|
||||
from pina.problem import AbstractProblem
|
||||
@@ -39,10 +38,10 @@ import matplotlib.pyplot as plt
|
||||
data = io.loadmat("Data_Darcy.mat")
|
||||
|
||||
# extract data (we use only 100 data for train)
|
||||
k_train = torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1)
|
||||
u_train = torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1)
|
||||
k_test = torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1)
|
||||
u_test= torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1)
|
||||
k_train = LabelTensor(torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1), ['u0'])
|
||||
u_train = LabelTensor(torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1), ['u'])
|
||||
k_test = LabelTensor(torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1), ['u0'])
|
||||
u_test= LabelTensor(torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1), ['u'])
|
||||
x = torch.tensor(data['x'], dtype=torch.float)[0]
|
||||
y = torch.tensor(data['y'], dtype=torch.float)[0]
|
||||
|
||||
@@ -63,14 +62,14 @@ plt.show()
|
||||
|
||||
# We now create the neural operator class. It is a very simple class, inheriting from `AbstractProblem`.
|
||||
|
||||
# In[14]:
|
||||
# In[17]:
|
||||
|
||||
|
||||
class NeuralOperatorSolver(AbstractProblem):
|
||||
input_variables = ['u_0']
|
||||
output_variables = ['u']
|
||||
conditions = {'data' : Condition(input_points=LabelTensor(k_train, input_variables),
|
||||
output_points=LabelTensor(u_train, output_variables))}
|
||||
input_variables = k_train.labels
|
||||
output_variables = u_train.labels
|
||||
conditions = {'data' : Condition(input_points=k_train,
|
||||
output_points=u_train)}
|
||||
|
||||
# make problem
|
||||
problem = NeuralOperatorSolver()
|
||||
@@ -80,7 +79,7 @@ problem = NeuralOperatorSolver()
|
||||
#
|
||||
# We will first solve the problem using a Feedforward neural network. We will use the `SupervisedSolver` for solving the problem, since we are training using supervised learning.
|
||||
|
||||
# In[15]:
|
||||
# In[18]:
|
||||
|
||||
|
||||
# make model
|
||||
@@ -97,7 +96,7 @@ trainer.train()
|
||||
|
||||
# The final loss is pretty high... We can calculate the error by importing `LpLoss`.
|
||||
|
||||
# In[16]:
|
||||
# In[19]:
|
||||
|
||||
|
||||
from pina.loss import LpLoss
|
||||
@@ -106,10 +105,10 @@ from pina.loss import LpLoss
|
||||
metric_err = LpLoss(relative=True)
|
||||
|
||||
|
||||
err = float(metric_err(u_train.squeeze(-1), solver.models[0](k_train).squeeze(-1)).mean())*100
|
||||
err = float(metric_err(u_train.squeeze(-1), solver.neural_net(k_train).squeeze(-1)).mean())*100
|
||||
print(f'Final error training {err:.2f}%')
|
||||
|
||||
err = float(metric_err(u_test.squeeze(-1), solver.models[0](k_test).squeeze(-1)).mean())*100
|
||||
err = float(metric_err(u_test.squeeze(-1), solver.neural_net(k_test).squeeze(-1)).mean())*100
|
||||
print(f'Final error testing {err:.2f}%')
|
||||
|
||||
|
||||
@@ -117,7 +116,7 @@ print(f'Final error testing {err:.2f}%')
|
||||
#
|
||||
# We will now move to solve the problem using a FNO. Since we are learning operator this approach is better suited, as we shall see.
|
||||
|
||||
# In[17]:
|
||||
# In[24]:
|
||||
|
||||
|
||||
# make model
|
||||
@@ -125,10 +124,10 @@ lifting_net = torch.nn.Linear(1, 24)
|
||||
projecting_net = torch.nn.Linear(24, 1)
|
||||
model = FNO(lifting_net=lifting_net,
|
||||
projecting_net=projecting_net,
|
||||
n_modes=16,
|
||||
n_modes=8,
|
||||
dimensions=2,
|
||||
inner_size=24,
|
||||
padding=11)
|
||||
padding=8)
|
||||
|
||||
|
||||
# make solver
|
||||
@@ -141,13 +140,13 @@ trainer.train()
|
||||
|
||||
# We can clearly see that the final loss is lower. Let's see in testing.. Notice that the number of parameters is way higher than a `FeedForward` network. We suggest to use GPU or TPU for a speed up in training, when many data samples are used.
|
||||
|
||||
# In[18]:
|
||||
# In[25]:
|
||||
|
||||
|
||||
err = float(metric_err(u_train.squeeze(-1), solver.models[0](k_train).squeeze(-1)).mean())*100
|
||||
err = float(metric_err(u_train.squeeze(-1), solver.neural_net(k_train).squeeze(-1)).mean())*100
|
||||
print(f'Final error training {err:.2f}%')
|
||||
|
||||
err = float(metric_err(u_test.squeeze(-1), solver.models[0](k_test).squeeze(-1)).mean())*100
|
||||
err = float(metric_err(u_test.squeeze(-1), solver.neural_net(k_test).squeeze(-1)).mean())*100
|
||||
print(f'Final error testing {err:.2f}%')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user