tianshuocong / Lightning-Hydra

Let's learn Lightning-Hydra !

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lightning-Hydra

🚑 Let's learn how to use lightning-hydra-template!

Introduction

  • PyTorch Lightning: a lightweight PyTorch wrapper for high-performance AI research. Think of it as a framework for organizing your PyTorch code.

  • Hydra: a framework for elegantly configuring complex applications. The key feature is the ability to dynamically create a hierarchical configuration by composition and override it through config files and the command line.

Project Structure

β”œβ”€β”€ configs                   <- Hydra configuration files
β”‚   β”œβ”€β”€ callbacks                <- Callbacks configs
β”‚   β”œβ”€β”€ datamodule               <- Datamodule configs
β”‚   β”œβ”€β”€ debug                    <- Debugging configs
β”‚   β”œβ”€β”€ experiment               <- Experiment configs
β”‚   β”œβ”€β”€ extras                   <- Extra utilities configs
β”‚   β”œβ”€β”€ hparams_search           <- Hyperparameter search configs
β”‚   β”œβ”€β”€ hydra                    <- Hydra configs
β”‚   β”œβ”€β”€ local                    <- Local configs
β”‚   β”œβ”€β”€ logger                   <- Logger configs
β”‚   β”œβ”€β”€ model                    <- Model configs
β”‚   β”œβ”€β”€ paths                    <- Project paths configs
β”‚   β”œβ”€β”€ trainer                  <- Trainer configs
β”‚   β”‚
β”‚   β”œβ”€β”€ eval.yaml             <- Main config for evaluation
β”‚   └── train.yaml            <- Main config for training
β”‚
β”œβ”€β”€ data                   <- Project data
β”‚
β”œβ”€β”€ logs                   <- Logs generated by hydra and lightning loggers
β”‚
β”œβ”€β”€ notebooks              <- Jupyter notebooks. Naming convention is a number (for ordering),
β”‚                             the creator's initials, and a short `-` delimited description,
β”‚                             e.g. `1.0-jqp-initial-data-exploration.ipynb`.
β”‚
β”œβ”€β”€ scripts                <- Shell scripts
β”‚
β”œβ”€β”€ src                    <- Source code
β”‚   β”œβ”€β”€ datamodules              <- Lightning datamodules
β”‚   β”œβ”€β”€ models                   <- Lightning models
β”‚   β”œβ”€β”€ utils                    <- Utility scripts
β”‚   β”‚
β”‚   β”œβ”€β”€ eval.py                  <- Run evaluation
β”‚   └── train.py                 <- Run training
β”‚
β”œβ”€β”€ tests                  <- Tests of any kind
β”‚
β”œβ”€β”€ .env.example              <- Example of file for storing private environment variables
β”œβ”€β”€ .gitignore                <- List of files ignored by git
β”œβ”€β”€ .pre-commit-config.yaml   <- Configuration of pre-commit hooks for code formatting
β”œβ”€β”€ Makefile                  <- Makefile with commands like `make train` or `make test`
β”œβ”€β”€ pyproject.toml            <- Configuration options for testing and linting
β”œβ”€β”€ requirements.txt          <- File for installing python dependencies
β”œβ”€β”€ setup.py                  <- File for installing project as a package
└── README.md

Command Line

  • Config parameter python train.py trainer.max_epochs=20 model.optimizer.lr=1e-4

  • Add new parameters python train.py +model.new_param="owo"

  • Training device python train.py trainer=gpu

  • Train with mixed precision python train.py trainer=gpu +trainer.precision=16

  • Train model with configs/experiment/example.yaml python train.py experiment=example

  • Resume ckpt python train.py ckpt_path="/path/to/ckpt/name.ckpt"

  • Evaluate ckpt on test dataset python eval.py ckpt_path="/path/to/ckpt/name.ckpt"

  • Create a sweep over hyperparameters python train.py -m datamodule.batch_size=32,64,128 model.lr=0.001,0.0005

    πŸ₯‘ result in 6 different combination

  • ❀️ HPO with Optuna: python train.py -m hparams_search=mnist_optuna experiment=example We can define everything in a single config file

  • Execute all experiments from the folder configs/experiment/ through python train.py -m 'experiment=glob(*)'

  • Execute with multiple seed python train.py -m seed=1,2,3,4,5 trainer.deterministic=True logger=csv tags=["benchmark"]

Workflow

Steps

  1. Write PyTorch Lightning module as src/models/mnist_module.py

  2. Write PyTorch Lightning datamodule as src/datamodules/mnist_datamodule.py

  3. Write experiment config.

  4. Run training with command line as python src/train.py experiment=experiment_name.yaml

Experiment design

  1. Acc VS Batch size
python train.py -m logger=csv datamodule.batch_size=16,32,64,128 tags=["batch_size_exp"]
  1. Logs

Configuration is in configs/logger and run

python train.py logger=logger_name
  1. Tests
pytest

pytest tests/test_train.py

Hyperparameter Search

  1. Config file is in configs/hparams_search

  2. Command line: python train.py -m hparams_search=mnist_optuna

  3. Supported frameworks: Optuna, Ax, and Nevergrad

  4. The optimization_results.yaml will be available under logs/task_name/multirun folder.

Q&A

About

Let's learn Lightning-Hydra !


Languages

Language:Python 100.0%