|
25 | 25 | "source": [ |
26 | 26 | "## routine needed to run the notebook on Google Colab\n", |
27 | 27 | "try:\n", |
28 | | - " import google.colab\n", |
29 | | - " IN_COLAB = True\n", |
| 28 | + " import google.colab\n", |
| 29 | + "\n", |
| 30 | + " IN_COLAB = True\n", |
30 | 31 | "except:\n", |
31 | | - " IN_COLAB = False\n", |
| 32 | + " IN_COLAB = False\n", |
32 | 33 | "if IN_COLAB:\n", |
33 | | - " !pip install \"pina-mathlab\"\n", |
34 | | - " # get the data\n", |
35 | | - " !mkdir \"data\"\n", |
36 | | - " !wget \"https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial10/data/Data_KS.mat\" -O \"data/Data_KS.mat\"\n", |
37 | | - " !wget \"https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial10/data/Data_KS2.mat\" -O \"data/Data_KS2.mat\"\n", |
| 34 | + " !pip install \"pina-mathlab\"\n", |
| 35 | + " # get the data\n", |
| 36 | + " !mkdir \"data\"\n", |
| 37 | + " !wget \"https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial10/data/Data_KS.mat\" -O \"data/Data_KS.mat\"\n", |
| 38 | + " !wget \"https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial10/data/Data_KS2.mat\" -O \"data/Data_KS2.mat\"\n", |
38 | 39 | "\n", |
39 | 40 | "import torch\n", |
40 | 41 | "import matplotlib.pyplot as plt\n", |
|
46 | 47 | "from pina.solver import SupervisedSolver\n", |
47 | 48 | "from pina.problem.zoo import SupervisedProblem\n", |
48 | 49 | "\n", |
49 | | - "warnings.filterwarnings('ignore')" |
| 50 | + "warnings.filterwarnings(\"ignore\")" |
50 | 51 | ] |
51 | 52 | }, |
52 | 53 | { |
|
106 | 107 | ], |
107 | 108 | "source": [ |
108 | 109 | "# load data\n", |
109 | | - "data=io.loadmat(\"data/Data_KS.mat\")\n", |
| 110 | + "data = io.loadmat(\"data/Data_KS.mat\")\n", |
110 | 111 | "\n", |
111 | 112 | "# converting to label tensor\n", |
112 | | - "initial_cond_train = LabelTensor(torch.tensor(data['initial_cond_train'], dtype=torch.float), ['t','x','u0'])\n", |
113 | | - "initial_cond_test = LabelTensor(torch.tensor(data['initial_cond_test'], dtype=torch.float), ['t','x','u0'])\n", |
114 | | - "sol_train = LabelTensor(torch.tensor(data['sol_train'], dtype=torch.float), ['u'])\n", |
115 | | - "sol_test = LabelTensor(torch.tensor(data['sol_test'], dtype=torch.float), ['u'])\n", |
116 | | - "\n", |
117 | | - "print('Data Loaded')\n", |
118 | | - "print(f' shape initial condition: {initial_cond_train.shape}')\n", |
119 | | - "print(f' shape solution: {sol_train.shape}')" |
| 113 | + "initial_cond_train = LabelTensor(\n", |
| 114 | + " torch.tensor(data[\"initial_cond_train\"], dtype=torch.float),\n", |
| 115 | + " [\"t\", \"x\", \"u0\"],\n", |
| 116 | + ")\n", |
| 117 | + "initial_cond_test = LabelTensor(\n", |
| 118 | + " torch.tensor(data[\"initial_cond_test\"], dtype=torch.float), [\"t\", \"x\", \"u0\"]\n", |
| 119 | + ")\n", |
| 120 | + "sol_train = LabelTensor(\n", |
| 121 | + " torch.tensor(data[\"sol_train\"], dtype=torch.float), [\"u\"]\n", |
| 122 | + ")\n", |
| 123 | + "sol_test = LabelTensor(torch.tensor(data[\"sol_test\"], dtype=torch.float), [\"u\"])\n", |
| 124 | + "\n", |
| 125 | + "print(\"Data Loaded\")\n", |
| 126 | + "print(f\" shape initial condition: {initial_cond_train.shape}\")\n", |
| 127 | + "print(f\" shape solution: {sol_train.shape}\")" |
120 | 128 | ] |
121 | 129 | }, |
122 | 130 | { |
|
151 | 159 | "# helper function\n", |
152 | 160 | "def plot_trajectory(coords, real, no_sol=None):\n", |
153 | 161 | " # find the x-t shapes\n", |
154 | | - " dim_x = len(torch.unique(coords.extract('x')))\n", |
155 | | - " dim_t = len(torch.unique(coords.extract('t')))\n", |
| 162 | + " dim_x = len(torch.unique(coords.extract(\"x\")))\n", |
| 163 | + " dim_t = len(torch.unique(coords.extract(\"t\")))\n", |
156 | 164 | " # if we don't have the Neural Operator solution we simply plot the real one\n", |
157 | 165 | " if no_sol is None:\n", |
158 | 166 | " fig, axs = plt.subplots(1, 1, figsize=(15, 5), sharex=True, sharey=True)\n", |
159 | | - " c = axs.imshow(real.reshape(dim_t, dim_x).T.detach(),extent=[0, 50, 0, 64], cmap='PuOr_r', aspect='auto')\n", |
160 | | - " axs.set_title('Real solution')\n", |
| 167 | + " c = axs.imshow(\n", |
| 168 | + " real.reshape(dim_t, dim_x).T.detach(),\n", |
| 169 | + " extent=[0, 50, 0, 64],\n", |
| 170 | + " cmap=\"PuOr_r\",\n", |
| 171 | + " aspect=\"auto\",\n", |
| 172 | + " )\n", |
| 173 | + " axs.set_title(\"Real solution\")\n", |
161 | 174 | " fig.colorbar(c, ax=axs)\n", |
162 | | - " axs.set_xlabel('t')\n", |
163 | | - " axs.set_ylabel('x')\n", |
| 175 | + " axs.set_xlabel(\"t\")\n", |
| 176 | + " axs.set_ylabel(\"x\")\n", |
164 | 177 | " # otherwise we plot the real one, the Neural Operator one, and their difference\n", |
165 | 178 | " else:\n", |
166 | 179 | " fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True)\n", |
167 | | - " axs[0].imshow(real.reshape(dim_t, dim_x).T.detach(),extent=[0, 50, 0, 64], cmap='PuOr_r', aspect='auto')\n", |
168 | | - " axs[0].set_title('Real solution')\n", |
169 | | - " axs[1].imshow(no_sol.reshape(dim_t, dim_x).T.detach(),extent=[0, 50, 0, 64], cmap='PuOr_r', aspect='auto')\n", |
170 | | - " axs[1].set_title('NO solution')\n", |
171 | | - " c = axs[2].imshow((real - no_sol).abs().reshape(dim_t, dim_x).T.detach(),extent=[0, 50, 0, 64], cmap='PuOr_r', aspect='auto')\n", |
172 | | - " axs[2].set_title('Absolute difference')\n", |
| 180 | + " axs[0].imshow(\n", |
| 181 | + " real.reshape(dim_t, dim_x).T.detach(),\n", |
| 182 | + " extent=[0, 50, 0, 64],\n", |
| 183 | + " cmap=\"PuOr_r\",\n", |
| 184 | + " aspect=\"auto\",\n", |
| 185 | + " )\n", |
| 186 | + " axs[0].set_title(\"Real solution\")\n", |
| 187 | + " axs[1].imshow(\n", |
| 188 | + " no_sol.reshape(dim_t, dim_x).T.detach(),\n", |
| 189 | + " extent=[0, 50, 0, 64],\n", |
| 190 | + " cmap=\"PuOr_r\",\n", |
| 191 | + " aspect=\"auto\",\n", |
| 192 | + " )\n", |
| 193 | + " axs[1].set_title(\"NO solution\")\n", |
| 194 | + " c = axs[2].imshow(\n", |
| 195 | + " (real - no_sol).abs().reshape(dim_t, dim_x).T.detach(),\n", |
| 196 | + " extent=[0, 50, 0, 64],\n", |
| 197 | + " cmap=\"PuOr_r\",\n", |
| 198 | + " aspect=\"auto\",\n", |
| 199 | + " )\n", |
| 200 | + " axs[2].set_title(\"Absolute difference\")\n", |
173 | 201 | " fig.colorbar(c, ax=axs.ravel().tolist())\n", |
174 | 202 | " for ax in axs:\n", |
175 | | - " ax.set_xlabel('t')\n", |
176 | | - " ax.set_ylabel('x')\n", |
| 203 | + " ax.set_xlabel(\"t\")\n", |
| 204 | + " ax.set_ylabel(\"x\")\n", |
177 | 205 | " plt.show()\n", |
178 | 206 | "\n", |
| 207 | + "\n", |
179 | 208 | "# a sample trajectory (we use the sample 5, feel free to change)\n", |
180 | 209 | "sample_number = 20\n", |
181 | | - "plot_trajectory(coords=initial_cond_train[sample_number].extract(['x', 't']),\n", |
182 | | - " real=sol_train[sample_number].extract('u'))\n" |
| 210 | + "plot_trajectory(\n", |
| 211 | + " coords=initial_cond_train[sample_number].extract([\"x\", \"t\"]),\n", |
| 212 | + " real=sol_train[sample_number].extract(\"u\"),\n", |
| 213 | + ")" |
183 | 214 | ] |
184 | 215 | }, |
185 | 216 | { |
|
300 | 331 | ], |
301 | 332 | "source": [ |
302 | 333 | "# initialize problem\n", |
303 | | - "problem = SupervisedProblem(initial_cond_train, sol_train, input_variables=initial_cond_train.labels, output_variables=sol_train.labels)\n", |
| 334 | + "problem = SupervisedProblem(\n", |
| 335 | + " initial_cond_train,\n", |
| 336 | + " sol_train,\n", |
| 337 | + " input_variables=initial_cond_train.labels,\n", |
| 338 | + " output_variables=sol_train.labels,\n", |
| 339 | + ")\n", |
304 | 340 | "# initialize solver\n", |
305 | 341 | "solver = SupervisedSolver(problem=problem, model=model)\n", |
306 | 342 | "# train, only CPU and avoid model summary at beginning of training (optional)\n", |
|
343 | 379 | "source": [ |
344 | 380 | "sample_number = 2\n", |
345 | 381 | "no_sol = solver(initial_cond_test)\n", |
346 | | - "plot_trajectory(coords=initial_cond_test[sample_number].extract(['x', 't']),\n", |
347 | | - " real=sol_test[sample_number].extract('u'),\n", |
348 | | - " no_sol=no_sol[5])" |
| 382 | + "plot_trajectory(\n", |
| 383 | + " coords=initial_cond_test[sample_number].extract([\"x\", \"t\"]),\n", |
| 384 | + " real=sol_test[sample_number].extract(\"u\"),\n", |
| 385 | + " no_sol=no_sol[5],\n", |
| 386 | + ")" |
349 | 387 | ] |
350 | 388 | }, |
351 | 389 | { |
|
373 | 411 | "source": [ |
374 | 412 | "from pina.loss import PowerLoss\n", |
375 | 413 | "\n", |
376 | | - "error_metric = PowerLoss(p=2) # we use the MSE loss\n", |
| 414 | + "error_metric = PowerLoss(p=2) # we use the MSE loss\n", |
377 | 415 | "\n", |
378 | 416 | "with torch.no_grad():\n", |
379 | 417 | " no_sol_train = solver(initial_cond_train)\n", |
380 | | - " err_train = error_metric(sol_train.extract('u'), no_sol_train).mean() # we average the error over trajectories\n", |
| 418 | + " err_train = error_metric(\n", |
| 419 | + " sol_train.extract(\"u\"), no_sol_train\n", |
| 420 | + " ).mean() # we average the error over trajectories\n", |
381 | 421 | " no_sol_test = solver(initial_cond_test)\n", |
382 | | - " err_test = error_metric(sol_test.extract('u'),no_sol_test).mean() # we average the error over trajectories\n", |
383 | | - " print(f'Training error: {float(err_train):.3f}')\n", |
384 | | - " print(f'Testing error: {float(err_test):.3f}')" |
| 422 | + " err_test = error_metric(\n", |
| 423 | + " sol_test.extract(\"u\"), no_sol_test\n", |
| 424 | + " ).mean() # we average the error over trajectories\n", |
| 425 | + " print(f\"Training error: {float(err_train):.3f}\")\n", |
| 426 | + " print(f\"Testing error: {float(err_test):.3f}\")" |
385 | 427 | ] |
386 | 428 | }, |
387 | 429 | { |
|
0 commit comments