Update tutorials 1 through 12 to current version 0.2

This commit is contained in:
Matteo Bertocchi
2025-02-26 16:21:12 +01:00
committed by Nicola Demo
parent 8b797d589a
commit d83ca3af6e
82 changed files with 1074 additions and 1224 deletions

View File

@@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -35,10 +35,10 @@
"import torch\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",
@@ -91,16 +91,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 +133,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,107 +173,100 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n",
"/home/matte_b/.local/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py:73: `max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.\n"
]
},
{
"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, 27.90it/s, v_num=51, val_loss=0.000392, bound_cond_loss=5.29e-6, phys_cond_loss=0.000459, train_loss=0.000465] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
]
},
{
"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, 23.95it/s, v_num=51, val_loss=0.000392, bound_cond_loss=5.29e-6, phys_cond_loss=0.000459, train_loss=0.000465]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n",
"/home/matte_b/.local/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py:73: `max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.\n"
]
},
{
"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, 28.54it/s, v_num=52, val_loss=0.00267, bound_cond_loss=2.42e-5, phys_cond_loss=0.00144, train_loss=0.00146] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
]
},
{
"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, 24.59it/s, v_num=52, val_loss=0.00267, bound_cond_loss=2.42e-5, phys_cond_loss=0.00144, train_loss=0.00146]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n",
"/home/matte_b/.local/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py:73: `max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.\n"
]
},
{
"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, 29.41it/s, v_num=53, val_loss=0.00363, bound_cond_loss=1.02e-5, phys_cond_loss=0.000846, train_loss=0.000856] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
]
},
{
"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, 24.69it/s, v_num=53, val_loss=0.00363, bound_cond_loss=1.02e-5, phys_cond_loss=0.000846, train_loss=0.000856]\n"
]
}
],
@@ -351,11 +342,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,44 +370,38 @@
},
{
"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",
"HPU available: False, using: 0 HPUs\n"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n",
"/home/matte_b/.local/lib/python3.12/site-packages/lightning/pytorch/loops/utilities.py:73: `max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.\n"
]
},
{
"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, 28.21it/s, v_num=18, val_loss=0.000348, bound_cond_loss=7.54e-5, phys_cond_loss=0.000956, train_loss=0.00103] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1000` reached.\n"
]
},
{
"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, 23.99it/s, v_num=18, val_loss=0.000348, bound_cond_loss=7.54e-5, phys_cond_loss=0.000956, train_loss=0.00103]\n"
]
}
],
@@ -443,24 +429,27 @@
},
{
"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)}]"
"[{'val_loss': tensor(1.0595),\n",
" 'bound_cond_loss': tensor(1.0607),\n",
" 'phys_cond_loss': tensor(0.0043),\n",
" 'train_loss': tensor(1.0650)},\n",
" {'val_loss': tensor(1.0503),\n",
" 'bound_cond_loss': tensor(1.0522),\n",
" 'phys_cond_loss': tensor(0.0038),\n",
" 'train_loss': tensor(1.0560)},\n",
" {'val_loss': tensor(1.0412),\n",
" 'bound_cond_loss': tensor(1.0439),\n",
" 'phys_cond_loss': tensor(0.0033),\n",
" 'train_loss': tensor(1.0471)}]"
]
},
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -475,42 +464,33 @@
"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": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n"
]
},
{
"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 6468: 100%|██████████| 1/1 [00:00<00:00, 19.10it/s, v_num=19, val_loss=0.000129, bound_cond_loss=1.57e-8, phys_cond_loss=3.01e-6, train_loss=3.02e-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 +503,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 +537,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -565,38 +545,37 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n"
]
},
{
"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, 22.76it/s, v_num=20, val_loss=4.61e-5, bound_cond_loss=1.22e-6, phys_cond_loss=0.000171, train_loss=0.000172] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
]
},
{
"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, 19.28it/s, v_num=20, val_loss=4.61e-5, bound_cond_loss=1.22e-6, phys_cond_loss=0.000171, train_loss=0.000172]\n",
"Total training time 92.35361 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 +607,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -636,51 +615,50 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n"
]
},
{
"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, 30.61it/s, v_num=21, val_loss=4.54e-5, bound_cond_loss=5.03e-6, phys_cond_loss=0.000247, train_loss=0.000252] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Swapping scheduler `ConstantLR` for `SWALR`\n"
"INFO:pytorch_lightning.utilities.rank_zero:Swapping scheduler `ConstantLR` for `SWALR`\n"
]
},
{
"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, 28.21it/s, v_num=21, val_loss=3.45e-5, bound_cond_loss=2.41e-7, phys_cond_loss=9.02e-5, train_loss=9.04e-5] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
]
},
{
"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, 22.97it/s, v_num=21, val_loss=3.45e-5, bound_cond_loss=2.41e-7, phys_cond_loss=9.02e-5, train_loss=9.04e-5]\n",
"Total training time 86.25178 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 +694,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -724,46 +702,45 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: True (mps), 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"
"INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False\n",
"INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs\n"
]
},
{
"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, 27.78it/s, v_num=22, val_loss=1.52e-5, bound_cond_loss=5.29e-8, phys_cond_loss=4.07e-5, train_loss=4.08e-5] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Swapping scheduler `ConstantLR` for `SWALR`\n"
"INFO:pytorch_lightning.utilities.rank_zero:Swapping scheduler `ConstantLR` for `SWALR`\n"
]
},
{
"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, 28.11it/s, v_num=22, val_loss=0.000427, bound_cond_loss=0.000311, phys_cond_loss=0.000849, train_loss=0.00116] "
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
"INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=2000` reached.\n"
]
},
{
"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, 22.88it/s, v_num=22, val_loss=0.000427, bound_cond_loss=0.000311, phys_cond_loss=0.000849, train_loss=0.00116]\n",
"Total training time 87.27789 s\n"
]
}
],

View File

@@ -13,7 +13,7 @@
#
# Let's start by importing useful modules, define the `SimpleODE` problem and the `PINN` solver.
# In[18]:
# In[1]:
## routine needed to run the notebook on Google Colab
@@ -28,10 +28,10 @@ if IN_COLAB:
import torch
from pina import Condition, Trainer
from pina.solvers import PINN
from pina.solver import PINN
from pina.model import FeedForward
from pina.problem import SpatialProblem
from pina.operators import grad
from pina.operator import grad
from pina.domain import CartesianDomain
from pina.equation import Equation, FixedValue
@@ -77,7 +77,7 @@ pinn = PINN(problem, model)
# Till now we just followed the extact step of the previous tutorials. The `Trainer` object
# can be initialized by simiply passing the `PINN` solver
# In[3]:
# In[2]:
trainer = Trainer(solver=pinn)
@@ -96,7 +96,7 @@ trainer = Trainer(solver=pinn)
#
# * `accelerator = {'gpu', 'cpu', 'hpu', 'mps', 'cpu', 'ipu'}` sets the accelerator to a specific one
# In[5]:
# In[3]:
trainer = Trainer(solver=pinn,
@@ -114,7 +114,7 @@ trainer = Trainer(solver=pinn,
# We will now import `TensorBoardLogger`, do three runs of training and then visualize the results. Notice we set `enable_model_summary=False` to avoid model summary specifications (e.g. number of parameters), set it to true if needed.
#
# In[7]:
# In[4]:
from pytorch_lightning.loggers import TensorBoardLogger
@@ -161,10 +161,11 @@ for _ in range(3):
#
# <!-- Suppose we want to log the accuracy on some validation poit -->
# In[8]:
# In[5]:
from pytorch_lightning.callbacks import Callback
from lightning.pytorch.callbacks import Callback
from lightning.pytorch.callbacks import EarlyStopping
import torch
# define a simple callback
@@ -180,7 +181,7 @@ class NaiveMetricTracker(Callback):
# Let's see the results when applyed to the `SimpleODE` problem. You can define callbacks when initializing the `Trainer` by the `callbacks` argument, which expects a list of callbacks.
# In[10]:
# In[6]:
model = FeedForward(
@@ -199,7 +200,7 @@ trainer.train()
# We can easily access the data by calling `trainer.callbacks[0].saved_metrics` (notice the zero representing the first callback in the list given at initialization).
# In[9]:
# In[7]:
trainer.callbacks[0].saved_metrics[:3] # only the first three epochs
@@ -207,13 +208,12 @@ trainer.callbacks[0].saved_metrics[:3] # only the first three epochs
# 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).
#
# 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`.
# In[7]:
# In[ ]:
# ~2 mins
from pytorch_lightning.callbacks import EarlyStopping
# ~5 mins
model = FeedForward(
layers=[10, 10],
@@ -226,7 +226,7 @@ trainer = Trainer(solver=pinn,
accelerator='cpu',
max_epochs = -1,
enable_model_summary=False,
callbacks=[EarlyStopping('mean_loss')]) # adding a callbacks
callbacks=[EarlyStopping('train_loss')]) # adding a callbacks
trainer.train()
@@ -248,11 +248,11 @@ trainer.train()
# We will just demonstrate how to use the first two, and see the results compared to a standard training.
# We use the [`Timer`](https://lightning.ai/docs/pytorch/stable/api/lightning.pytorch.callbacks.Timer.html#lightning.pytorch.callbacks.Timer) callback from `pytorch_lightning.callbacks` to take the times. Let's start by training a simple model without any optimization (train for 2000 epochs).
# In[19]:
# In[9]:
from pytorch_lightning.callbacks import Timer
from pytorch_lightning import seed_everything
from lightning.pytorch.callbacks import Timer
from lightning.pytorch import seed_everything
# setting the seed for reproducibility
seed_everything(42, workers=True)
@@ -277,10 +277,10 @@ print(f'Total training time {trainer.callbacks[0].time_elapsed("train"):.5f} s')
# Now we do the same but with StochasticWeightAveraging
# In[36]:
# In[10]:
from pytorch_lightning.callbacks import StochasticWeightAveraging
from lightning.pytorch.callbacks import StochasticWeightAveraging
# setting the seed for reproducibility
seed_everything(42, workers=True)
@@ -309,7 +309,7 @@ print(f'Total training time {trainer.callbacks[0].time_elapsed("train"):.5f} s')
#
# We will now now do the same but clippling the gradient to be relatively small.
# In[35]:
# In[11]:
# setting the seed for reproducibility