2021-01-07 05:24:47 +00:00
## Basic Examples
2021-08-03 18:19:09 +00:00
2021-10-26 17:38:56 +00:00
Use these examples to test how Lightning works.
2019-10-05 18:13:55 +00:00
2021-11-02 08:04:29 +00:00
## MNIST Examples
2021-08-03 18:19:09 +00:00
2021-11-02 08:04:29 +00:00
Here are 5 MNIST examples showing you how to gradually convert from pure PyTorch to PyTorch Lightning.
2021-08-03 18:19:09 +00:00
2021-11-02 08:04:29 +00:00
The transition through [LightningLite ](https://pytorch-lightning.readthedocs.io/en/stable/starter/lightning_lite.rst ) from pure PyTorch is optional but it might be helpful to learn about it.
#### 1. Image Classifier with Vanilla PyTorch
2019-10-05 18:13:55 +00:00
2021-11-02 08:04:29 +00:00
Trains a simple CNN over MNIST using vanilla PyTorch.
2019-10-05 18:13:55 +00:00
2021-11-02 08:04:29 +00:00
```bash
# CPU
python image_classifier_1_pytorch.py
2020-09-23 04:19:46 +00:00
```
2019-10-05 18:13:55 +00:00
2021-08-03 18:19:09 +00:00
______________________________________________________________________
2021-11-02 08:04:29 +00:00
#### 2. Image Classifier with LightningLite
2021-08-03 18:19:09 +00:00
2021-11-02 08:04:29 +00:00
This script shows you how to scale the previous script to enable GPU and multi-GPU training using [LightningLite ](https://pytorch-lightning.readthedocs.io/en/stable/starter/lightning_lite.html ).
2021-08-03 18:19:09 +00:00
2020-11-06 14:53:46 +00:00
```bash
2021-11-02 08:04:29 +00:00
# CPU / multiple GPUs if available
python image_classifier_2_lite.py
2020-11-06 14:53:46 +00:00
```
2021-08-03 18:19:09 +00:00
______________________________________________________________________
2021-11-02 08:04:29 +00:00
#### 3. Image Classifier - Conversion from Lite to Lightning
2021-08-03 18:19:09 +00:00
2021-11-02 08:04:29 +00:00
This script shows you how to prepare your conversion from [LightningLite ](https://pytorch-lightning.readthedocs.io/en/stable/starter/lightning_lite.html ) to `LightningModule` .
2021-08-03 18:19:09 +00:00
2019-10-05 18:13:55 +00:00
```bash
2021-11-02 08:04:29 +00:00
# CPU / multiple GPUs if available
python image_classifier_3_lite_to_lightning_module.py
```
2019-10-05 18:13:55 +00:00
2021-11-02 08:04:29 +00:00
______________________________________________________________________
#### 4. Image Classifier with LightningModule
This script shows you the result of the conversion to the `LightningModule` and finally all the benefits you get from the Lightning ecosystem.
```bash
# CPU
python image_classifier_4_lightning_module.py
2019-10-05 18:13:55 +00:00
2021-11-02 08:04:29 +00:00
# GPUs (any number)
python image_classifier_4_lightning_module.py --trainer.gpus 2
2019-10-05 18:13:55 +00:00
```
2021-08-03 18:19:09 +00:00
______________________________________________________________________
2021-11-02 08:04:29 +00:00
#### 5. Image Classifier with LightningModule and LightningDataModule
2021-08-03 18:19:09 +00:00
2021-11-02 08:04:29 +00:00
This script shows you how to extract the data related components into a `LightningDataModule` .
2021-08-03 18:19:09 +00:00
2019-10-05 18:13:55 +00:00
```bash
2021-11-02 08:04:29 +00:00
# CPU
python image_classifier_5_lightning_datamodule.py
2020-04-03 21:57:34 +00:00
2021-11-02 08:04:29 +00:00
# GPUs (any number)
python image_classifier_5_lightning_datamodule.py --trainer.gpus 2
2020-04-03 21:57:34 +00:00
2021-11-02 08:04:29 +00:00
# Distributed Data Parallel (DDP)
python image_classifier_5_lightning_datamodule.py --trainer.gpus 2 --trainer.strategy 'ddp'
2020-09-23 04:19:46 +00:00
```