From e2c8ed4bf80209308da43fac6fdf0394e1e1dee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Zalewski?= Date: Wed, 7 Apr 2021 17:15:09 +0200 Subject: [PATCH] Dev (#123) * fix quickstart command in readme * fix documentation links in model and datamodule * add `terminate_on_nan` trainer arg to readme * improve docstrings and type hinting --- README.md | 13 ++++++++----- src/datamodules/mnist_datamodule.py | 17 ++++++++++++----- src/models/mnist_model.py | 7 +++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index dd8b664..b3865b0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Lightning-Hydra-Template - PyTorch @@ -113,7 +113,7 @@ The directory structure of new project looks like this: ## 🚀  Quickstart ```yaml # clone project -git clone https://github.com/hobogalaxy/lightning-hydra-template +git clone https://github.com/ashleve/lightning-hydra-template cd lightning-hydra-template # [OPTIONAL] create conda environment @@ -232,7 +232,7 @@ python run.py callbacks=default_callbacks python run.py +trainer.gradient_clip_val=0.5 # stochastic weight averaging can make your models generalize better -python run.py +trainer.stochastic_weight_avg=True +python run.py +trainer.stochastic_weight_avg=true # run validation loop 4 times during a training epoch python run.py +trainer.val_check_interval=0.25 @@ -257,6 +257,9 @@ python run.py trainer.weights_summary="full" # print execution time profiling after training ends python run.py +trainer.profiler="simple" +# raise exception, if any of the parameters or the loss are NaN or +/-inf +python run.py trainer.terminate_on_nan=true + # try overfitting to 1 batch python run.py +trainer.overfit_batches=1 trainer.max_epochs=20 @@ -360,7 +363,7 @@ If you want to use some popular official image instead, I recommend the [nvidia ## :heart:  Contributions Have a question? Found a bug? Missing a specific feature? Ran into a problem? Feel free to file a new issue with respective title and description. If you already found a solution to your problem, don't hesitate to share it. Suggestions for new best practices and tricks are always welcome! -


+



@@ -424,7 +427,7 @@ data_dir: ${work_dir}/data/ # use `python run.py debug=true` for easy debugging! -# (equivalent to running `python run.py trainer.fast_dev_run=True`) +# (equivalent to running `python run.py trainer.fast_dev_run=true`) debug: False diff --git a/src/datamodules/mnist_datamodule.py b/src/datamodules/mnist_datamodule.py index e3a969d..0287ec0 100644 --- a/src/datamodules/mnist_datamodule.py +++ b/src/datamodules/mnist_datamodule.py @@ -9,12 +9,19 @@ from torchvision.transforms import transforms class MNISTDataModule(LightningDataModule): """ Example of LightningDataModule for MNIST dataset. - - A DataModule standardizes the train, val, test splits, data preparation and transforms. - The main advantage is consistent data splits, data preparation and transforms across models. + + A DataModule implements 5 key methods: + - prepare_data (things to do on 1 GPU/TPU, not on every GPU/TPU in distributed mode) + - setup (things to do on every accelerator in distributed mode) + - train_dataloader (the training dataloader) + - val_dataloader (the validation dataloader(s)) + - test_dataloader (the test dataloader(s)) + + This allows you to share a full dataset without explaining how to download, + split, transform and process the data Read the docs: - https://pytorch-lightning.readthedocs.io/en/latest/datamodules.html + https://pytorch-lightning.readthedocs.io/en/latest/extensions/datamodules.html """ def __init__( @@ -24,7 +31,7 @@ class MNISTDataModule(LightningDataModule): batch_size: int = 64, num_workers: int = 0, pin_memory: bool = False, - **kwargs, + **kwargs ): super().__init__() diff --git a/src/models/mnist_model.py b/src/models/mnist_model.py index fb8c94e..38ce7a0 100644 --- a/src/models/mnist_model.py +++ b/src/models/mnist_model.py @@ -19,7 +19,7 @@ class MNISTLitModel(LightningModule): - Optimizers (configure_optimizers) Read the docs: - https://pytorch-lightning.readthedocs.io/en/latest/lightning_module.html + https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html """ def __init__( @@ -57,10 +57,10 @@ class MNISTLitModel(LightningModule): "val/loss": [], } - def forward(self, x): + def forward(self, x: torch.Tensor): return self.model(x) - def step(self, batch): + def step(self, batch: Any): x, y = batch logits = self.forward(x) loss = self.criterion(logits, y) @@ -123,7 +123,6 @@ class MNISTLitModel(LightningModule): See examples here: https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers - """ return torch.optim.Adam( params=self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams.weight_decay