Path: blob/main/ch13/ch13_part3_lightning.ipynb
1245 views
Kernel: Python 3 (ipykernel)
Machine Learning with PyTorch and Scikit-Learn
-- Code Examples
Package version checks
Add folder to path in order to load from the check_packages.py script:
In [1]:
Check recommended package versions:
In [2]:
Out[2]:
[OK] Your Python version is 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:59:51)
[GCC 9.4.0]
[OK] torch 1.10.1+cu102
[OK] torchvision 0.11.2+cu102
[OK] tensorboard 2.7.0
[OK] pytorch_lightning 1.5.1
[OK] torchmetrics 0.6.2
Chapter 13: Going Deeper -- the Mechanics of PyTorch (Part 3/3)
Higher-level PyTorch APIs: a short introduction to PyTorch Lightning
Setting up the PyTorch Lightning model
Higher-level PyTorch APIs: a short introduction to PyTorch Lightning
Setting up the PyTorch Lightning model
In [3]:
In [4]:
Setting up the data loaders
In [5]:
In [6]:
Training the model using the PyTorch Lightning Trainer class
In [7]:
Out[7]:
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
2022-02-21 00:05:52.412727: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
| Name | Type | Params
-----------------------------------------
0 | train_acc | Accuracy | 0
1 | valid_acc | Accuracy | 0
2 | test_acc | Accuracy | 0
3 | model | Sequential | 25.8 K
-----------------------------------------
25.8 K Trainable params
0 Non-trainable params
25.8 K Total params
0.103 Total estimated model params size (MB)
Validation sanity check: 0it [00:00, ?it/s]
Training: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Evaluating the model using TensorBoard
In [8]:
Out[8]:
Restoring states from the checkpoint path at /home/jovyan/ch13/lightning_logs/version_0/checkpoints/epoch=8-step=7739.ckpt
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
Loaded model weights from checkpoint at /home/jovyan/ch13/lightning_logs/version_0/checkpoints/epoch=8-step=7739.ckpt
Testing: 0it [00:00, ?it/s]
--------------------------------------------------------------------------------
DATALOADER:0 TEST RESULTS
{'test_acc': 0.9499600529670715, 'test_loss': 0.14912301301956177}
--------------------------------------------------------------------------------
[{'test_loss': 0.14912301301956177, 'test_acc': 0.9499600529670715}]
In [9]:
Out[9]:
In [10]:
Out[10]:
In [11]:
Out[11]:
In [12]:
Out[12]:
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py:45: LightningDeprecationWarning: Setting `Trainer(resume_from_checkpoint=)` is deprecated in v1.5 and will be removed in v1.7. Please pass `Trainer.fit(ckpt_path=)` directly instead.
rank_zero_deprecation(
Trainer already configured with model summary callbacks: [<class 'pytorch_lightning.callbacks.model_summary.ModelSummary'>]. Skipping setting a default `ModelSummary` callback.
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py:1893: LightningDeprecationWarning: `trainer.resume_from_checkpoint` is deprecated in v1.5 and will be removed in v1.7. Specify the fit checkpoint path with `trainer.fit(ckpt_path=)` instead.
rank_zero_deprecation(
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/core/datamodule.py:469: LightningDeprecationWarning: DataModule.setup has already been called, so it will not be called again. In v1.6 this behavior will change to always call DataModule.setup.
rank_zero_deprecation(
Restoring states from the checkpoint path at lightning_logs/version_0/checkpoints/epoch=8-step=7739.ckpt
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py:247: UserWarning: You're resuming from a checkpoint that ended mid-epoch. Training will start from the beginning of the next epoch. This can cause unreliable results if further training is done, consider using an end of epoch checkpoint.
rank_zero_warn(
Restored all states from the checkpoint file at lightning_logs/version_0/checkpoints/epoch=8-step=7739.ckpt
| Name | Type | Params
-----------------------------------------
0 | train_acc | Accuracy | 0
1 | valid_acc | Accuracy | 0
2 | test_acc | Accuracy | 0
3 | model | Sequential | 25.8 K
-----------------------------------------
25.8 K Trainable params
0 Non-trainable params
25.8 K Total params
0.103 Total estimated model params size (MB)
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/callbacks/model_checkpoint.py:617: UserWarning: Checkpoint directory /home/jovyan/ch13/lightning_logs/version_0/checkpoints exists and is not empty.
rank_zero_warn(f"Checkpoint directory {dirpath} exists and is not empty.")
Validation sanity check: 0it [00:00, ?it/s]
Training: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
Validating: 0it [00:00, ?it/s]
/home/jovyan/conda/lib/python3.8/site-packages/pytorch_lightning/core/datamodule.py:469: LightningDeprecationWarning: DataModule.teardown has already been called, so it will not be called again. In v1.6 this behavior will change to always call DataModule.teardown.
rank_zero_deprecation(
In [14]:
Out[14]:
In [13]:
Out[13]:
Reusing TensorBoard on port 6006 (pid 702), started 0:02:27 ago. (Use '!kill 702' to kill it.)
In [15]:
Out[15]:
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
Testing: 0it [00:00, ?it/s]
--------------------------------------------------------------------------------
DATALOADER:0 TEST RESULTS
{'test_acc': 0.9542893767356873, 'test_loss': 0.14718961715698242}
--------------------------------------------------------------------------------
[{'test_loss': 0.14718961715698242, 'test_acc': 0.9542893767356873}]
In [16]:
Out[16]:
Restoring states from the checkpoint path at /home/jovyan/ch13/lightning_logs/version_0/checkpoints/epoch=13-step=12039.ckpt
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
Loaded model weights from checkpoint at /home/jovyan/ch13/lightning_logs/version_0/checkpoints/epoch=13-step=12039.ckpt
Testing: 0it [00:00, ?it/s]
--------------------------------------------------------------------------------
DATALOADER:0 TEST RESULTS
{'test_acc': 0.9559454321861267, 'test_loss': 0.14512717723846436}
--------------------------------------------------------------------------------
[{'test_loss': 0.14512717723846436, 'test_acc': 0.9559454321861267}]
In [17]:
Summary
Readers may ignore the next cell.
In [18]:
Out[18]:
[NbConvertApp] Converting notebook ch13_part3_lightning.ipynb to script
[NbConvertApp] Writing 7321 bytes to ch13_part3_lightning.py
In [ ]: