Update Tutorials 0.2 (#490)

This commit is contained in:
Dario Coscia
2025-03-13 20:15:48 +01:00
committed by FilippoOlivo
parent 03be447e02
commit 86222b3ac6
30 changed files with 1526 additions and 5227 deletions

View File

@@ -23,7 +23,6 @@
"metadata": {},
"outputs": [],
"source": [
"## routine needed to run the notebook on Google Colab\n",
"try:\n",
" import google.colab\n",
" IN_COLAB = True\n",
@@ -43,34 +42,59 @@
"from pina.domain import CartesianDomain\n",
"from pina.equation import Equation, FixedValue\n",
"\n",
"warnings.filterwarnings('ignore')\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define problem and solver."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# defining the ode equation\n",
"def ode_equation(input_, output_):\n",
"\n",
" # computing the derivative\n",
" u_x = grad(output_, input_, components=[\"u\"], d=[\"x\"])\n",
"\n",
" # extracting the u input variable\n",
" u = output_.extract([\"u\"])\n",
"\n",
" # calculate the residual and return it\n",
" return u_x - u\n",
"\n",
"class SimpleODE(SpatialProblem):\n",
"\n",
" output_variables = ['u']\n",
" spatial_domain = CartesianDomain({'x': [0, 1]})\n",
" output_variables = [\"u\"]\n",
" spatial_domain = CartesianDomain({\"x\": [0, 1]})\n",
"\n",
" # defining the ode equation\n",
" def ode_equation(input_, output_):\n",
" u_x = grad(output_, input_, components=['u'], d=['x'])\n",
" u = output_.extract(['u'])\n",
" return u_x - u\n",
" domains = {\n",
" \"x0\": CartesianDomain({\"x\": 0.0}),\n",
" \"D\": CartesianDomain({\"x\": [0, 1]}),\n",
" }\n",
"\n",
" # conditions to hold\n",
" conditions = {\n",
" 'bound_cond': Condition(domain=CartesianDomain({'x': 0.}), equation=FixedValue(1)), # We fix initial condition to value 1\n",
" 'phys_cond': Condition(domain=CartesianDomain({'x': [0, 1]}), equation=Equation(ode_equation)), # We wrap the python equation using Equation\n",
" \"bound_cond\": Condition(domain=\"x0\", equation=FixedValue(1.0)),\n",
" \"phys_cond\": Condition(domain=\"D\", equation=Equation(ode_equation)),\n",
" }\n",
"\n",
" # defining the true solution\n",
" def truth_solution(self, pts):\n",
" return torch.exp(pts.extract(['x']))\n",
" \n",
" def solution(self, pts):\n",
" return torch.exp(pts.extract([\"x\"]))\n",
"\n",
"\n",
"# sampling for training\n",
"problem = SimpleODE()\n",
"problem.discretise_domain(1, 'random', domains=['bound_cond'])\n",
"problem.discretise_domain(20, 'lh', domains=['phys_cond'])\n",
"problem.discretise_domain(1, \"random\", domains=[\"x0\"])\n",
"problem.discretise_domain(20, \"lh\", domains=[\"D\"])\n",
"\n",
"# build the model\n",
"model = FeedForward(\n",
@@ -94,14 +118,14 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: True\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -136,22 +160,21 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
}
],
"source": [
"trainer = Trainer(solver=pinn,\n",
" accelerator='cpu')"
"trainer = Trainer(solver=pinn, accelerator=\"cpu\")"
]
},
{
@@ -176,14 +199,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -192,7 +215,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 233.15it/s, v_num=0, bound_cond_loss=1.22e-5, phys_cond_loss=0.000517, train_loss=0.000529]"
]
},
{
@@ -206,14 +229,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 137.95it/s, v_num=0, bound_cond_loss=1.22e-5, phys_cond_loss=0.000517, train_loss=0.000529]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -222,7 +245,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 248.63it/s, v_num=1, bound_cond_loss=2.29e-5, phys_cond_loss=0.00106, train_loss=0.00108] "
]
},
{
@@ -236,14 +259,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 149.06it/s, v_num=1, bound_cond_loss=2.29e-5, phys_cond_loss=0.00106, train_loss=0.00108]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -252,7 +275,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 254.65it/s, v_num=2, bound_cond_loss=0.00029, phys_cond_loss=0.00253, train_loss=0.00282] "
]
},
{
@@ -266,12 +289,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 150.72it/s, v_num=2, bound_cond_loss=0.00029, phys_cond_loss=0.00253, train_loss=0.00282]\n"
]
}
],
"source": [
"from pytorch_lightning.loggers import TensorBoardLogger\n",
"from lightning.pytorch.loggers import TensorBoardLogger\n",
"\n",
"# three run of training, by default it trains for 1000 epochs\n",
"# we reinitialize the model each time otherwise the same parameters will be optimized\n",
@@ -280,16 +303,18 @@
" layers=[10, 10],\n",
" func=torch.nn.Tanh,\n",
" output_dimensions=len(problem.output_variables),\n",
" input_dimensions=len(problem.input_variables)\n",
" input_dimensions=len(problem.input_variables),\n",
" )\n",
" pinn = PINN(problem, model)\n",
" trainer = Trainer(solver=pinn,\n",
" accelerator='cpu',\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 = Trainer(\n",
" solver=pinn,\n",
" accelerator=\"cpu\",\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",
" )\n",
" trainer.train()"
]
},
@@ -297,7 +322,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now visualize the logs by simply running `tensorboard --logdir=simpleode/` on terminal, you should obtain a webpage as the one shown below:"
"We can now visualize the logs by simply running `tensorboard --logdir=training_log/` on terminal, you should obtain a webpage as the one shown below:"
]
},
{
@@ -345,7 +370,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -353,12 +378,15 @@
"from lightning.pytorch.callbacks import EarlyStopping\n",
"import torch\n",
"\n",
"\n",
"# define a simple callback\n",
"class NaiveMetricTracker(Callback):\n",
" def __init__(self):\n",
" self.saved_metrics = []\n",
"\n",
" def on_train_epoch_end(self, trainer, __): # function called at the end of each epoch\n",
" def on_train_epoch_end(\n",
" self, trainer, __\n",
" ): # function called at the end of each epoch\n",
" self.saved_metrics.append(\n",
" {key: value for key, value in trainer.logged_metrics.items()}\n",
" )"
@@ -373,14 +401,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -389,7 +417,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 278.93it/s, v_num=0, bound_cond_loss=6.94e-5, phys_cond_loss=0.00116, train_loss=0.00123] "
]
},
{
@@ -403,7 +431,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 999: 100%|██████████| 1/1 [00:00<00:00, 140.62it/s, v_num=0, bound_cond_loss=6.94e-5, phys_cond_loss=0.00116, train_loss=0.00123]\n"
]
}
],
@@ -435,24 +463,24 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'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)}]"
"[{'bound_cond_loss': tensor(0.9935),\n",
" 'phys_cond_loss': tensor(0.0303),\n",
" 'train_loss': tensor(1.0239)},\n",
" {'bound_cond_loss': tensor(0.9875),\n",
" 'phys_cond_loss': tensor(0.0293),\n",
" 'train_loss': tensor(1.0169)},\n",
" {'bound_cond_loss': tensor(0.9815),\n",
" 'phys_cond_loss': tensor(0.0284),\n",
" 'train_loss': tensor(1.0099)}]"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -472,14 +500,14 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -488,25 +516,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 2343: 100%|██████████| 1/1 [00:00<00:00, 64.24it/s, v_num=1, val_loss=4.79e-6, bound_cond_loss=1.15e-7, phys_cond_loss=2.33e-5, train_loss=2.34e-5] \n"
]
}
],
"source": [
"# ~5 mins\n",
"\n",
"model = FeedForward(\n",
" layers=[10, 10],\n",
" func=torch.nn.Tanh,\n",
" output_dimensions=len(problem.output_variables),\n",
" input_dimensions=len(problem.input_variables)\n",
" )\n",
" layers=[10, 10],\n",
" func=torch.nn.Tanh,\n",
" output_dimensions=len(problem.output_variables),\n",
" input_dimensions=len(problem.input_variables),\n",
")\n",
"pinn = PINN(problem, model)\n",
"trainer = Trainer(solver=pinn,\n",
" accelerator='cpu',\n",
" max_epochs = -1,\n",
" enable_model_summary=False,\n",
" callbacks=[EarlyStopping('train_loss')]) # adding a callbacks\n",
"trainer = Trainer(\n",
" solver=pinn,\n",
" accelerator=\"cpu\",\n",
" max_epochs=-1,\n",
" enable_model_summary=False,\n",
" val_size=0.2,\n",
" train_size=0.8,\n",
" test_size=0.0,\n",
" callbacks=[EarlyStopping(\"val_loss\")],\n",
") # adding a callbacks\n",
"trainer.train()"
]
},
@@ -540,7 +571,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -548,7 +579,7 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -557,7 +588,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 156.69it/s, v_num=2, bound_cond_loss=1.53e-6, phys_cond_loss=0.000169, train_loss=0.000171]"
]
},
{
@@ -571,8 +602,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 108.75it/s, v_num=2, bound_cond_loss=1.53e-6, phys_cond_loss=0.000169, train_loss=0.000171]\n",
"Total training time 15.36648 s\n"
]
}
],
@@ -610,7 +641,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -618,7 +649,7 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -627,7 +658,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 224.16it/s, v_num=3, bound_cond_loss=5.7e-6, phys_cond_loss=0.000257, train_loss=0.000263] "
]
},
{
@@ -641,7 +672,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 261.43it/s, v_num=3, bound_cond_loss=2.58e-7, phys_cond_loss=9.4e-5, train_loss=9.43e-5] "
]
},
{
@@ -655,8 +686,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 145.96it/s, v_num=3, bound_cond_loss=2.58e-7, phys_cond_loss=9.4e-5, train_loss=9.43e-5]\n",
"Total training time 17.78182 s\n"
]
}
],
@@ -697,7 +728,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -705,7 +736,7 @@
"output_type": "stream",
"text": [
"Seed set to 42\n",
"GPU available: False, used: False\n",
"GPU available: True (mps), used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"HPU available: False, using: 0 HPUs\n"
]
@@ -714,7 +745,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 1598: 100%|██████████| 1/1 [00:00<00:00, 251.76it/s, v_num=4, bound_cond_loss=5.98e-8, phys_cond_loss=3.88e-5, train_loss=3.88e-5] "
]
},
{
@@ -728,7 +759,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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] "
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 239.11it/s, v_num=4, bound_cond_loss=0.000333, phys_cond_loss=0.000676, train_loss=0.00101] "
]
},
{
@@ -742,8 +773,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"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"
"Epoch 1999: 100%|██████████| 1/1 [00:00<00:00, 127.88it/s, v_num=4, bound_cond_loss=0.000333, phys_cond_loss=0.000676, train_loss=0.00101]\n",
"Total training time 15.12576 s\n"
]
}
],
@@ -789,7 +820,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "pina",
"language": "python",
"name": "python3"
},
@@ -803,7 +834,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.9.21"
}
},
"nbformat": 4,