Automatize Tutorials html, py files creation (#496)

* workflow to export tutorials

---------
This commit is contained in:
Dario Coscia
2025-03-15 11:01:19 +01:00
committed by FilippoOlivo
parent 8dfc9d19db
commit 3ff9f0c9a2
51 changed files with 140529 additions and 440 deletions

View File

@@ -23,12 +23,13 @@
"source": [
"## routine needed to run the notebook on Google Colab\n",
"try:\n",
" import google.colab\n",
" IN_COLAB = True\n",
" import google.colab\n",
"\n",
" IN_COLAB = True\n",
"except:\n",
" IN_COLAB = False\n",
" IN_COLAB = False\n",
"if IN_COLAB:\n",
" !pip install \"pina-mathlab\"\n",
" !pip install \"pina-mathlab\"\n",
"\n",
"import torch\n",
"import matplotlib.pyplot as plt\n",
@@ -39,7 +40,7 @@
"from pina.solver import PINN\n",
"from torch.nn import Softplus\n",
"\n",
"warnings.filterwarnings('ignore')"
"warnings.filterwarnings(\"ignore\")"
]
},
{
@@ -186,8 +187,7 @@
" train_size=0.8, # set train size\n",
" val_size=0.0, # set validation size\n",
" test_size=0.2, # set testing size\n",
" shuffle=True, # shuffle the data\n",
" \n",
" shuffle=True, # shuffle the data\n",
")\n",
"\n",
"# train\n",
@@ -348,7 +348,7 @@
"\n",
" def forward(self, pts):\n",
" x, y = pts.extract([\"x\"]), pts.extract([\"y\"])\n",
" f = 2 *torch.pi**2 * torch.sin(x * torch.pi) * torch.sin(y * torch.pi)\n",
" f = 2 * torch.pi**2 * torch.sin(x * torch.pi) * torch.sin(y * torch.pi)\n",
" return LabelTensor(f, [\"feat\"])\n",
"\n",
"\n",
@@ -487,28 +487,36 @@
"source": [
"class SinSinAB(torch.nn.Module):\n",
" \"\"\" \"\"\"\n",
"\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.alpha = torch.nn.Parameter(torch.tensor([1.0]))\n",
" self.beta = torch.nn.Parameter(torch.tensor([1.0]))\n",
"\n",
" def forward(self, x):\n",
" t = (\n",
" self.beta*torch.sin(self.alpha*x.extract(['x'])*torch.pi)*\n",
" torch.sin(self.alpha*x.extract(['y'])*torch.pi)\n",
" t = (\n",
" self.beta\n",
" * torch.sin(self.alpha * x.extract([\"x\"]) * torch.pi)\n",
" * torch.sin(self.alpha * x.extract([\"y\"]) * torch.pi)\n",
" )\n",
" return LabelTensor(t, ['b*sin(a*x)sin(a*y)'])\n",
" return LabelTensor(t, [\"b*sin(a*x)sin(a*y)\"])\n",
"\n",
"\n",
"# make model + solver + trainer\n",
"model_learn = FeedForwardWithExtraFeatures(\n",
" input_dimensions=len(problem.input_variables) + 1, #we add one as also we consider the extra feature dimension\n",
" input_dimensions=len(problem.input_variables)\n",
" + 1, # we add one as also we consider the extra feature dimension\n",
" output_dimensions=len(problem.output_variables),\n",
" func=Softplus,\n",
" layers=[10, 10],\n",
" extra_features=SinSinAB())\n",
" extra_features=SinSinAB(),\n",
")\n",
"\n",
"pinn_learn = PINN(problem, model_learn, optimizer=TorchOptimizer(torch.optim.Adam, lr=0.006,weight_decay=1e-8))\n",
"pinn_learn = PINN(\n",
" problem,\n",
" model_learn,\n",
" optimizer=TorchOptimizer(torch.optim.Adam, lr=0.006, weight_decay=1e-8),\n",
")\n",
"trainer_learn = Trainer(\n",
" solver=pinn_learn, # setting the solver, i.e. PINN\n",
" max_epochs=1000, # setting max epochs in training\n",
@@ -649,14 +657,14 @@
],
"source": [
"# test error base pinn\n",
"print('PINN')\n",
"print(\"PINN\")\n",
"trainer_base.test()\n",
"# test error extra features pinn\n",
"print(\"PINN with extra features\")\n",
"trainer_feat.test()\n",
"# test error learnable extra features pinn\n",
"print(\"PINN with learnable extra features\")\n",
"_=trainer_learn.test()"
"_ = trainer_learn.test()"
]
},
{