From cbbf15dd326c1c81401cce550157d984ddfbaae4 Mon Sep 17 00:00:00 2001 From: FilippoOlivo Date: Thu, 31 Oct 2024 14:31:13 +0100 Subject: [PATCH] Fix tutorial5 --- tutorials/tutorial5/tutorial.ipynb | 154 ++++++++++++----------------- 1 file changed, 65 insertions(+), 89 deletions(-) diff --git a/tutorials/tutorial5/tutorial.ipynb b/tutorials/tutorial5/tutorial.ipynb index 64032d31..d8e98546 100644 --- a/tutorials/tutorial5/tutorial.ipynb +++ b/tutorials/tutorial5/tutorial.ipynb @@ -22,8 +22,8 @@ "id": "5f2744dc", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:28.837348Z", - "start_time": "2024-09-19T13:35:27.611334Z" + "end_time": "2024-10-31T13:30:49.886194Z", + "start_time": "2024-10-31T13:30:48.624214Z" } }, "source": [ @@ -61,8 +61,8 @@ "id": "2ffb8a4c", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:28.989631Z", - "start_time": "2024-09-19T13:35:28.952744Z" + "end_time": "2024-10-31T13:30:49.920642Z", + "start_time": "2024-10-31T13:30:49.888843Z" } }, "source": [ @@ -70,14 +70,18 @@ "data = io.loadmat(\"Data_Darcy.mat\")\n", "\n", "# extract data (we use only 100 data for train)\n", - "k_train = LabelTensor(torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1), \n", - " labels={3:{'dof': ['u0'], 'name': 'k_train'}})\n", - "u_train = LabelTensor(torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1),\n", - " labels={3:{'dof': ['u'], 'name': 'u_train'}})\n", - "k_test = LabelTensor(torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1),\n", - " labels={3:{'dof': ['u0'], 'name': 'k_test'}})\n", - "u_test= LabelTensor(torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1),\n", - " labels={3:{'dof': ['u'], 'name': 'u_test'}})\n", + "k_train = LabelTensor(\n", + " torch.tensor(data['k_train'], dtype=torch.float).unsqueeze(-1),\n", + " labels={3: {'dof': ['u0'], 'name': 'k_train'}})\n", + "u_train = LabelTensor(\n", + " torch.tensor(data['u_train'], dtype=torch.float).unsqueeze(-1),\n", + " labels={3: {'dof': ['u'], 'name': 'u_train'}})\n", + "k_test = LabelTensor(\n", + " torch.tensor(data['k_test'], dtype=torch.float).unsqueeze(-1),\n", + " labels={3: {'dof': ['u0'], 'name': 'k_test'}})\n", + "u_test = LabelTensor(\n", + " torch.tensor(data['u_test'], dtype=torch.float).unsqueeze(-1),\n", + " labels={3: {'dof': ['u'], 'name': 'u_test'}})\n", "x = torch.tensor(data['x'], dtype=torch.float)[0]\n", "y = torch.tensor(data['y'], dtype=torch.float)[0]" ], @@ -97,8 +101,8 @@ "id": "c8501b6f", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:29.108381Z", - "start_time": "2024-09-19T13:35:29.031076Z" + "end_time": "2024-10-31T13:30:50.101674Z", + "start_time": "2024-10-31T13:30:50.034859Z" } }, "source": [ @@ -124,32 +128,6 @@ ], "execution_count": 3 }, - { - "cell_type": "code", - "id": "082ab7a8-22e0-498b-b138-158dc9f2658f", - "metadata": { - "ExecuteTime": { - "end_time": "2024-09-19T13:35:29.122858Z", - "start_time": "2024-09-19T13:35:29.119985Z" - } - }, - "source": [ - "u_train.labels[3]['dof']" - ], - "outputs": [ - { - "data": { - "text/plain": [ - "['u']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "execution_count": 4 - }, { "cell_type": "markdown", "id": "89a77ff1", @@ -163,33 +141,23 @@ "id": "8b27d283", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:29.136572Z", - "start_time": "2024-09-19T13:35:29.134124Z" + "end_time": "2024-10-31T13:30:50.191764Z", + "start_time": "2024-10-31T13:30:50.189977Z" } }, "source": [ "class NeuralOperatorSolver(AbstractProblem):\n", - " input_variables = k_train.labels[3]['dof']\n", - " output_variables = u_train.labels[3]['dof']\n", - " domains = {\n", - " 'pts': k_train\n", - " }\n", - " conditions = {'data' : Condition(domain='pts', \n", - " output_points=u_train)}\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", "\n", "# make problem\n", "problem = NeuralOperatorSolver()" ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "execution_count": 5 + "outputs": [], + "execution_count": 4 }, { "cell_type": "markdown", @@ -206,20 +174,21 @@ "id": "e34f18b0", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:31.245429Z", - "start_time": "2024-09-19T13:35:29.154937Z" + "end_time": "2024-10-31T13:30:52.528635Z", + "start_time": "2024-10-31T13:30:50.225049Z" } }, "source": [ "# make model\n", "model = FeedForward(input_dimensions=1, output_dimensions=1)\n", "\n", - "\n", "# make solver\n", "solver = SupervisedSolver(problem=problem, model=model)\n", "\n", "# make the trainer and train\n", - "trainer = Trainer(solver=solver, max_epochs=10, accelerator='cpu', enable_model_summary=False, batch_size=10) \n", + "trainer = Trainer(solver=solver, max_epochs=10, accelerator='cpu',\n", + " enable_model_summary=False, batch_size=10, train_size=1.,\n", + " test_size=0., val_size=0.)\n", "# We train on CPU and avoid model summary at the beginning of training (optional)\n", "trainer.train()" ], @@ -231,14 +200,15 @@ "GPU available: True (mps), used: False\n", "TPU available: False, using: 0 TPU cores\n", "HPU available: False, using: 0 HPUs\n", - "/Users/filippoolivo/miniconda3/envs/PINAv0.2/lib/python3.11/site-packages/pytorch_lightning/trainer/setup.py:177: GPU available but not used. You can set it by doing `Trainer(accelerator='gpu')`.\n" + "/Users/filippoolivo/miniconda3/envs/PINAv0.2-test/lib/python3.11/site-packages/pytorch_lightning/trainer/setup.py:177: GPU available but not used. You can set it by doing `Trainer(accelerator='gpu')`.\n", + "/Users/filippoolivo/miniconda3/envs/PINAv0.2-test/lib/python3.11/site-packages/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py:75: Starting from v1.9.0, `tensorboardX` has been removed as a dependency of the `pytorch_lightning` package, due to potential conflicts with other packages in the ML ecosystem. For this reason, `logger=True` will use `CSVLogger` as the default logger, unless the `tensorboard` or `tensorboardX` packages are found. Please `pip install lightning[extra]` or one of them to enable TensorBoard support by default\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 552.80it/s, v_num=18, mean_loss=0.113]" + "Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 459.79it/s, v_num=58, mean_loss=0.108]" ] }, { @@ -252,11 +222,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 547.37it/s, v_num=18, mean_loss=0.113]\n" + "Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 456.20it/s, v_num=58, mean_loss=0.108]\n" ] } ], - "execution_count": 6 + "execution_count": 5 }, { "cell_type": "markdown", @@ -271,8 +241,8 @@ "id": "0e2a6aa4", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:31.295336Z", - "start_time": "2024-09-19T13:35:31.256308Z" + "end_time": "2024-10-31T13:30:52.600181Z", + "start_time": "2024-10-31T13:30:52.564456Z" } }, "source": [ @@ -282,10 +252,12 @@ "metric_err = LpLoss(relative=True)\n", "\n", "model = solver.models[0]\n", - "err = float(metric_err(u_train.squeeze(-1), model(k_train).squeeze(-1)).mean())*100\n", + "err = float(\n", + " metric_err(u_train.squeeze(-1), model(k_train).squeeze(-1)).mean()) * 100\n", "print(f'Final error training {err:.2f}%')\n", "\n", - "err = float(metric_err(u_test.squeeze(-1), model(k_test).squeeze(-1)).mean())*100\n", + "err = float(\n", + " metric_err(u_test.squeeze(-1), model(k_test).squeeze(-1)).mean()) * 100\n", "print(f'Final error testing {err:.2f}%')" ], "outputs": [ @@ -294,11 +266,11 @@ "output_type": "stream", "text": [ "Final error training 56.05%\n", - "Final error testing 55.95%\n" + "Final error testing 56.02%\n" ] } ], - "execution_count": 7 + "execution_count": 6 }, { "cell_type": "markdown", @@ -315,8 +287,8 @@ "id": "9af523a5", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:44.717807Z", - "start_time": "2024-09-19T13:35:31.306689Z" + "end_time": "2024-10-31T13:31:06.537460Z", + "start_time": "2024-10-31T13:30:52.694424Z" } }, "source": [ @@ -330,12 +302,14 @@ " inner_size=24,\n", " padding=8)\n", "\n", - "\n", "# make solver\n", "solver = SupervisedSolver(problem=problem, model=model)\n", "\n", "# make the trainer and train\n", - "trainer = Trainer(solver=solver, max_epochs=10, accelerator='cpu', enable_model_summary=False, batch_size=10) # we train on CPU and avoid model summary at beginning of training (optional)\n", + "trainer = Trainer(solver=solver, max_epochs=10, accelerator='cpu',\n", + " enable_model_summary=False, batch_size=10, train_size=1.,\n", + " test_size=0.,\n", + " val_size=0.) # we train on CPU and avoid model summary at beginning of training (optional)\n", "trainer.train()" ], "outputs": [ @@ -352,7 +326,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Epoch 9: 100%|██████████| 100/100 [00:01<00:00, 73.04it/s, v_num=19, mean_loss=0.00215]" + "Epoch 9: 100%|██████████| 100/100 [00:01<00:00, 72.88it/s, v_num=59, mean_loss=0.00291]" ] }, { @@ -366,11 +340,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "Epoch 9: 100%|██████████| 100/100 [00:01<00:00, 72.84it/s, v_num=19, mean_loss=0.00215]\n" + "Epoch 9: 100%|██████████| 100/100 [00:01<00:00, 72.69it/s, v_num=59, mean_loss=0.00291]\n" ] } ], - "execution_count": 8 + "execution_count": 7 }, { "cell_type": "markdown", @@ -385,17 +359,19 @@ "id": "58e2db89", "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:35:45.259819Z", - "start_time": "2024-09-19T13:35:44.729042Z" + "end_time": "2024-10-31T13:31:07.134298Z", + "start_time": "2024-10-31T13:31:06.657198Z" } }, "source": [ "model = solver.models[0]\n", "\n", - "err = float(metric_err(u_train.squeeze(-1), model(k_train).squeeze(-1)).mean())*100\n", + "err = float(\n", + " metric_err(u_train.squeeze(-1), model(k_train).squeeze(-1)).mean()) * 100\n", "print(f'Final error training {err:.2f}%')\n", "\n", - "err = float(metric_err(u_test.squeeze(-1), model(k_test).squeeze(-1)).mean())*100\n", + "err = float(\n", + " metric_err(u_test.squeeze(-1), model(k_test).squeeze(-1)).mean()) * 100\n", "print(f'Final error testing {err:.2f}%')" ], "outputs": [ @@ -403,12 +379,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "Final error training 7.48%\n", - "Final error testing 7.73%\n" + "Final error training 7.21%\n", + "Final error testing 7.39%\n" ] } ], - "execution_count": 9 + "execution_count": 8 }, { "cell_type": "markdown", @@ -431,8 +407,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-09-19T13:08:35.195331Z", - "start_time": "2024-09-19T13:08:35.193830Z" + "end_time": "2024-10-31T13:31:07.231206Z", + "start_time": "2024-10-31T13:31:07.230017Z" } }, "cell_type": "code",