|
20 | 20 | }, |
21 | 21 | { |
22 | 22 | "cell_type": "code", |
23 | | - "execution_count": 1, |
| 23 | + "execution_count": null, |
24 | 24 | "metadata": {}, |
25 | 25 | "outputs": [], |
26 | 26 | "source": [ |
|
37 | 37 | "import warnings\n", |
38 | 38 | "\n", |
39 | 39 | "from pina import Trainer\n", |
40 | | - "from pina.solver import SupervisedSolver\n", |
| 40 | + "from pina.solver import SupervisedSingleModelSolver\n", |
41 | 41 | "from pina.model import FeedForward\n", |
42 | 42 | "from pina.problem.zoo import SupervisedProblem\n", |
43 | 43 | "\n", |
|
53 | 53 | }, |
54 | 54 | { |
55 | 55 | "cell_type": "code", |
56 | | - "execution_count": 2, |
| 56 | + "execution_count": null, |
57 | 57 | "metadata": {}, |
58 | 58 | "outputs": [], |
59 | 59 | "source": [ |
|
71 | 71 | " input_dimensions=1,\n", |
72 | 72 | ")\n", |
73 | 73 | "\n", |
74 | | - "# create the SupervisedSolver object\n", |
75 | | - "solver = SupervisedSolver(problem, model, use_lt=False)" |
| 74 | + "# create the SupervisedSingleModelSolver object\n", |
| 75 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)" |
76 | 76 | ] |
77 | 77 | }, |
78 | 78 | { |
79 | 79 | "cell_type": "markdown", |
80 | 80 | "metadata": {}, |
81 | 81 | "source": [ |
82 | | - "Till now we just followed the extact step of the previous tutorials. The `Trainer` object\n", |
83 | | - "can be initialized by simiply passing the `SupervisedSolver` solver" |
| 82 | + "Untill now we just followed the extact step of the previous tutorials. The `Trainer` object\n", |
| 83 | + "can be initialized by simiply passing the `SupervisedSingleModelSolver` solver:" |
84 | 84 | ] |
85 | 85 | }, |
86 | 86 | { |
|
132 | 132 | "source": [ |
133 | 133 | "## Trainer Logging\n", |
134 | 134 | "\n", |
135 | | - "In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callbacks`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n", |
| 135 | + "In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callback`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n", |
136 | 136 | "\n", |
137 | 137 | "However, especially when we need to train multiple times to get an average of the loss across multiple runs, `lightning.pytorch.loggers` might be useful. Here we will use `TensorBoardLogger` (more on [logging](https://lightning.ai/docs/pytorch/stable/extensions/logging.html) here), but you can choose the one you prefer (or make your own one).\n", |
138 | 138 | "\n", |
|
156 | 156 | " output_dimensions=1,\n", |
157 | 157 | " input_dimensions=1,\n", |
158 | 158 | " )\n", |
159 | | - " solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 159 | + " solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
160 | 160 | " trainer = Trainer(\n", |
161 | 161 | " solver=solver,\n", |
162 | 162 | " accelerator=\"cpu\",\n", |
|
194 | 194 | "\n", |
195 | 195 | "## Trainer Callbacks\n", |
196 | 196 | "\n", |
197 | | - "Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** `Solver`s.\n", |
| 197 | + "Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** Solvers.\n", |
198 | 198 | "\n", |
199 | 199 | "Lightning has a callback system to execute them when needed. **Callbacks** should capture NON-ESSENTIAL logic that is NOT required for your lightning module to run.\n", |
200 | 200 | "\n", |
|
206 | 206 | "* Directly calling methods (e.g., on_validation_end) is strongly discouraged.\n", |
207 | 207 | "* Whenever possible, your callbacks should not depend on the order in which they are executed.\n", |
208 | 208 | "\n", |
209 | | - "We will try now to implement a naive version of `MetricTraker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callbacks`." |
| 209 | + "We will try now to implement a naive version of `MetricTraker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callback`." |
210 | 210 | ] |
211 | 211 | }, |
212 | 212 | { |
213 | 213 | "cell_type": "code", |
214 | | - "execution_count": 6, |
| 214 | + "execution_count": null, |
215 | 215 | "metadata": {}, |
216 | 216 | "outputs": [], |
217 | 217 | "source": [ |
|
227 | 227 | "\n", |
228 | 228 | " def on_train_epoch_end(\n", |
229 | 229 | " self, trainer, __\n", |
230 | | - " ): # function called at the end of each epoch\n", |
| 230 | + " ): \n", |
| 231 | + " \"\"\"\n", |
| 232 | + " Function called at the end of each epoch.\n", |
| 233 | + " \"\"\"\n", |
231 | 234 | " self.saved_metrics.append(\n", |
232 | 235 | " {key: value for key, value in trainer.logged_metrics.items()}\n", |
233 | 236 | " )" |
|
252 | 255 | " output_dimensions=1,\n", |
253 | 256 | " input_dimensions=1,\n", |
254 | 257 | ")\n", |
255 | | - "solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 258 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
256 | 259 | "trainer = Trainer(\n", |
257 | 260 | " solver=solver,\n", |
258 | 261 | " accelerator=\"cpu\",\n", |
|
276 | 279 | }, |
277 | 280 | { |
278 | 281 | "cell_type": "code", |
279 | | - "execution_count": 8, |
| 282 | + "execution_count": null, |
280 | 283 | "metadata": {}, |
281 | | - "outputs": [ |
282 | | - { |
283 | | - "data": { |
284 | | - "text/plain": [ |
285 | | - "[{'data_loss': tensor(104.4973), 'train_loss': tensor(104.4973)},\n", |
286 | | - " {'data_loss': tensor(104.3082), 'train_loss': tensor(104.3082)},\n", |
287 | | - " {'data_loss': tensor(104.1189), 'train_loss': tensor(104.1189)}]" |
288 | | - ] |
289 | | - }, |
290 | | - "execution_count": 8, |
291 | | - "metadata": {}, |
292 | | - "output_type": "execute_result" |
293 | | - } |
294 | | - ], |
| 284 | + "outputs": [], |
295 | 285 | "source": [ |
296 | 286 | "trainer.callbacks[0].saved_metrics[:3] # only the first three epochs" |
297 | 287 | ] |
|
312 | 302 | "outputs": [], |
313 | 303 | "source": [ |
314 | 304 | "model = FeedForward(\n", |
315 | | - " layers=[10, 10],\n", |
| 305 | + " layers=[64, 64],\n", |
316 | 306 | " func=torch.nn.Tanh,\n", |
317 | 307 | " output_dimensions=1,\n", |
318 | 308 | " input_dimensions=1,\n", |
319 | 309 | ")\n", |
320 | | - "solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 310 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
321 | 311 | "trainer = Trainer(\n", |
322 | 312 | " solver=solver,\n", |
323 | 313 | " accelerator=\"cpu\",\n", |
324 | | - " max_epochs=-1,\n", |
| 314 | + " max_epochs=1000,\n", |
325 | 315 | " enable_model_summary=False,\n", |
326 | 316 | " enable_progress_bar=False,\n", |
327 | 317 | " val_size=0.2,\n", |
|
378 | 368 | " input_dimensions=1,\n", |
379 | 369 | ")\n", |
380 | 370 | "\n", |
381 | | - "solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 371 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
382 | 372 | "trainer = Trainer(\n", |
383 | 373 | " solver=solver,\n", |
384 | 374 | " accelerator=\"cpu\",\n", |
|
415 | 405 | " output_dimensions=1,\n", |
416 | 406 | " input_dimensions=1,\n", |
417 | 407 | ")\n", |
418 | | - "solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 408 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
419 | 409 | "trainer = Trainer(\n", |
420 | 410 | " solver=solver,\n", |
421 | 411 | " accelerator=\"cpu\",\n", |
|
454 | 444 | " output_dimensions=1,\n", |
455 | 445 | " input_dimensions=1,\n", |
456 | 446 | ")\n", |
457 | | - "solver = SupervisedSolver(problem, model, use_lt=False)\n", |
| 447 | + "solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n", |
458 | 448 | "trainer = Trainer(\n", |
459 | 449 | " solver=solver,\n", |
460 | 450 | " accelerator=\"cpu\",\n", |
|
0 commit comments