Update tutorials (#463)

---------

Co-authored-by: Dario Coscia <93731561+dario-coscia@users.noreply.github.com>
This commit is contained in:
Matteo Bertocchi
2025-02-26 16:21:12 +01:00
committed by FilippoOlivo
parent 8b797d589a
commit bd9b49530a
30 changed files with 3057 additions and 1453 deletions

View File

@@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -33,15 +33,18 @@
" !pip install \"pina-mathlab\"\n",
"\n",
"import torch\n",
"import warnings\n",
"\n",
"from pina import Condition, Trainer\n",
"from pina.solvers import PINN\n",
"from pina.solver import PINN\n",
"from pina.model import FeedForward\n",
"from pina.problem import SpatialProblem\n",
"from pina.operators import grad\n",
"from pina.operator import grad\n",
"from pina.domain import CartesianDomain\n",
"from pina.equation import Equation, FixedValue\n",
"\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"class SimpleODE(SpatialProblem):\n",
"\n",
" output_variables = ['u']\n",
@@ -91,16 +94,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: True\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
}
@@ -134,16 +136,15 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
}
@@ -175,16 +176,15 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -192,14 +192,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 8: 100%|██████████| 1/1 [00:00<00:00, 232.78it/s, v_num=6, x0_loss=0.436, D_loss=0.129, mean_loss=0.283] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 222.52it/s, v_num=6, x0_loss=1.48e-5, D_loss=0.000655, mean_loss=0.000335]"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 91.87it/s, v_num=8, bound_cond_loss=6.07e-5, phys_cond_loss=0.000828, train_loss=0.000889] "
]
},
{
@@ -213,16 +206,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 133.46it/s, v_num=6, x0_loss=1.48e-5, D_loss=0.000655, mean_loss=0.000335]\n"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 57.75it/s, v_num=8, bound_cond_loss=6.07e-5, phys_cond_loss=0.000828, train_loss=0.000889]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -230,7 +222,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 274.80it/s, v_num=7, x0_loss=6.21e-6, D_loss=0.000221, mean_loss=0.000114]"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 79.80it/s, v_num=9, bound_cond_loss=8.63e-5, phys_cond_loss=0.00215, train_loss=0.00223] "
]
},
{
@@ -244,16 +236,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 154.49it/s, v_num=7, x0_loss=6.21e-6, D_loss=0.000221, mean_loss=0.000114]\n"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 54.20it/s, v_num=9, bound_cond_loss=8.63e-5, phys_cond_loss=0.00215, train_loss=0.00223]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -261,7 +252,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 78.56it/s, v_num=8, x0_loss=1.44e-5, D_loss=0.000572, mean_loss=0.000293] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 82.98it/s, v_num=10, bound_cond_loss=2.84e-5, phys_cond_loss=0.00118, train_loss=0.00121] "
]
},
{
@@ -275,7 +266,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 62.60it/s, v_num=8, x0_loss=1.44e-5, D_loss=0.000572, mean_loss=0.000293]\n"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 55.87it/s, v_num=10, bound_cond_loss=2.84e-5, phys_cond_loss=0.00118, train_loss=0.00121]\n"
]
}
],
@@ -294,8 +285,11 @@
" pinn = PINN(problem, model)\n",
" trainer = Trainer(solver=pinn,\n",
" accelerator='cpu',\n",
" logger=TensorBoardLogger(save_dir='simpleode'),\n",
" enable_model_summary=False)\n",
" logger=TensorBoardLogger(save_dir='training_log'),\n",
" enable_model_summary=False,\n",
" train_size=1.0,\n",
" val_size=0.0,\n",
" test_size=0.0)\n",
" trainer.train()"
]
},
@@ -351,11 +345,12 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from pytorch_lightning.callbacks import Callback\n",
"from lightning.pytorch.callbacks import Callback\n",
"from lightning.pytorch.callbacks import EarlyStopping\n",
"import torch\n",
"\n",
"# define a simple callback\n",
@@ -378,22 +373,15 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"IPU available: False, using: 0 IPUs\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -401,7 +389,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 241.30it/s, v_num=1, x0_loss=7.27e-5, D_loss=0.0016, mean_loss=0.000838] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 90.01it/s, v_num=70, bound_cond_loss=2.14e-5, phys_cond_loss=0.000448, train_loss=0.000469] "
]
},
{
@@ -415,7 +403,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 149.27it/s, v_num=1, x0_loss=7.27e-5, D_loss=0.0016, mean_loss=0.000838]\n"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 57.95it/s, v_num=70, bound_cond_loss=2.14e-5, phys_cond_loss=0.000448, train_loss=0.000469]\n"
]
}
],
@@ -429,8 +417,12 @@
"pinn = PINN(problem, model)\n",
"trainer = Trainer(solver=pinn,\n",
" accelerator='cpu',\n",
" logger=True,\n",
" callbacks=[NaiveMetricTracker()], # adding a callbacks\n",
" enable_model_summary=False,\n",
" callbacks=[NaiveMetricTracker()]) # adding a callbacks\n",
" train_size=1.0,\n",
" val_size=0.0,\n",
" test_size=0.0)\n",
"trainer.train()"
]
},
@@ -443,24 +435,24 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'x0_loss': tensor(0.9141),\n",
" 'D_loss': tensor(0.0304),\n",
" 'mean_loss': tensor(0.4722)},\n",
" {'x0_loss': tensor(0.8906),\n",
" 'D_loss': tensor(0.0287),\n",
" 'mean_loss': tensor(0.4596)},\n",
" {'x0_loss': tensor(0.8674),\n",
" 'D_loss': tensor(0.0274),\n",
" 'mean_loss': tensor(0.4474)}]"
"[{'bound_cond_loss': tensor(0.0385),\n",
" 'phys_cond_loss': tensor(0.7217),\n",
" 'train_loss': tensor(0.7602)},\n",
" {'bound_cond_loss': tensor(0.0399),\n",
" 'phys_cond_loss': tensor(0.7142),\n",
" 'train_loss': tensor(0.7541)},\n",
" {'bound_cond_loss': tensor(0.0413),\n",
" 'phys_cond_loss': tensor(0.7067),\n",
" 'train_loss': tensor(0.7480)}]"
]
},
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -475,21 +467,20 @@
"source": [
"PyTorch Lightning also has some built in `Callbacks` which can be used in **PINA**, [here an extensive list](https://lightning.ai/docs/pytorch/stable/extensions/callbacks.html#built-in-callbacks). \n",
"\n",
"We can for example try the `EarlyStopping` routine, which automatically stops the training when a specific metric converged (here the `mean_loss`). In order to let the training keep going forever set `max_epochs=-1`."
"We can for example try the `EarlyStopping` routine, which automatically stops the training when a specific metric converged (here the `train_loss`). In order to let the training keep going forever set `max_epochs=-1`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -497,20 +488,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 4: 100%|██████████| 1/1 [00:00<00:00, 255.67it/s, v_num=9, x0_loss=0.876, D_loss=0.00542, mean_loss=0.441]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 6157: 100%|██████████| 1/1 [00:00<00:00, 139.84it/s, v_num=9, x0_loss=4.21e-9, D_loss=9.93e-6, mean_loss=4.97e-6] \n"
"Epoch 4186: 100%|██████████| 1/1 [00:00<00:00, 37.06it/s, v_num=71, bound_cond_loss=1.91e-10, phys_cond_loss=3.88e-6, train_loss=3.88e-6] \n"
]
}
],
"source": [
"# ~2 mins\n",
"from pytorch_lightning.callbacks import EarlyStopping\n",
"# ~5 mins\n",
"\n",
"model = FeedForward(\n",
" layers=[10, 10],\n",
@@ -523,7 +506,7 @@
" accelerator='cpu',\n",
" max_epochs = -1,\n",
" enable_model_summary=False,\n",
" callbacks=[EarlyStopping('mean_loss')]) # adding a callbacks\n",
" callbacks=[EarlyStopping('train_loss')]) # adding a callbacks\n",
"trainer.train()"
]
},
@@ -557,7 +540,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -565,9 +548,8 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -575,7 +557,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 275.87it/s, v_num=31, x0_loss=1.12e-6, D_loss=0.000127, mean_loss=6.4e-5] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 82.19it/s, v_num=72, bound_cond_loss=1.74e-6, phys_cond_loss=0.00018, train_loss=0.000182] "
]
},
{
@@ -589,14 +571,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 163.58it/s, v_num=31, x0_loss=1.12e-6, D_loss=0.000127, mean_loss=6.4e-5]\n",
"Total training time 17.36381 s\n"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 56.83it/s, v_num=72, bound_cond_loss=1.74e-6, phys_cond_loss=0.00018, train_loss=0.000182]\n",
"Total training time 32.64355 s\n"
]
}
],
"source": [
"from pytorch_lightning.callbacks import Timer\n",
"from pytorch_lightning import seed_everything\n",
"from lightning.pytorch.callbacks import Timer\n",
"from lightning.pytorch import seed_everything\n",
"\n",
"# setting the seed for reproducibility\n",
"seed_everything(42, workers=True)\n",
@@ -628,7 +610,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -636,9 +618,8 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -646,7 +627,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 210.04it/s, v_num=47, x0_loss=4.17e-6, D_loss=0.000204, mean_loss=0.000104]"
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 70.77it/s, v_num=73, bound_cond_loss=7.01e-6, phys_cond_loss=0.000283, train_loss=0.00029] "
]
},
{
@@ -660,7 +641,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 259.39it/s, v_num=47, x0_loss=1.56e-7, D_loss=7.49e-5, mean_loss=3.75e-5] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 62.57it/s, v_num=73, bound_cond_loss=2.74e-7, phys_cond_loss=9.51e-5, train_loss=9.54e-5] "
]
},
{
@@ -674,13 +655,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 120.85it/s, v_num=47, x0_loss=1.56e-7, D_loss=7.49e-5, mean_loss=3.75e-5]\n",
"Total training time 17.10627 s\n"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 40.66it/s, v_num=73, bound_cond_loss=2.74e-7, phys_cond_loss=9.51e-5, train_loss=9.54e-5]\n",
"Total training time 39.14717 s\n"
]
}
],
"source": [
"from pytorch_lightning.callbacks import StochasticWeightAveraging\n",
"from lightning.pytorch.callbacks import StochasticWeightAveraging\n",
"\n",
"# setting the seed for reproducibility\n",
"seed_everything(42, workers=True)\n",
@@ -716,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -724,9 +705,8 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), used: False\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n",
"HPU available: False, using: 0 HPUs\n"
]
},
@@ -734,7 +714,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 261.80it/s, v_num=46, x0_loss=9e-8, D_loss=2.39e-5, mean_loss=1.2e-5] "
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 69.88it/s, v_num=74, bound_cond_loss=5.16e-8, phys_cond_loss=3.54e-5, train_loss=3.54e-5] "
]
},
{
@@ -748,7 +728,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 261.78it/s, v_num=46, x0_loss=7.08e-7, D_loss=1.77e-5, mean_loss=9.19e-6] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 73.42it/s, v_num=74, bound_cond_loss=0.000126, phys_cond_loss=0.000315, train_loss=0.000441] "
]
},
{
@@ -762,8 +742,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 148.99it/s, v_num=46, x0_loss=7.08e-7, D_loss=1.77e-5, mean_loss=9.19e-6]\n",
"Total training time 17.01149 s\n"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 47.28it/s, v_num=74, bound_cond_loss=0.000126, phys_cond_loss=0.000315, train_loss=0.000441]\n",
"Total training time 40.19983 s\n"
]
}
],