2022-03-24 06:27:10 +00:00
.. testsetup :: *
2023-02-27 20:14:23 +00:00
from lightning.pytorch.trainer.trainer import Trainer
2022-03-24 06:27:10 +00:00
.. _progress_bar:
2022-04-19 18:15:47 +00:00
Customize the progress bar
==========================
2022-03-24 06:27:10 +00:00
2023-02-27 20:14:23 +00:00
Lightning supports two different types of progress bars (`tqdm <https://github.com/tqdm/tqdm> `_ and `rich <https://github.com/Textualize/rich> `_ ). :class: `~lightning.pytorch.callbacks.TQDMProgressBar` is used by default,
but you can override it by passing a custom :class: `~lightning.pytorch.callbacks.TQDMProgressBar` or :class: `~lightning.pytorch.callbacks.RichProgressBar` to the `` callbacks `` argument of the :class: `~lightning.pytorch.trainer.trainer.Trainer` .
2022-03-24 06:27:10 +00:00
2023-03-14 13:22:47 +00:00
You could also use the :class: `~lightning.pytorch.callbacks.ProgressBar` class to implement your own progress bar.
2022-03-24 06:27:10 +00:00
-------------
TQDMProgressBar
---------------
2023-02-27 20:14:23 +00:00
The :class: `~lightning.pytorch.callbacks.TQDMProgressBar` uses the `tqdm <https://github.com/tqdm/tqdm> `_ library internally and is the default progress bar used by Lightning.
2022-03-24 06:27:10 +00:00
It prints to `` stdout `` and shows up to four different bars:
- **sanity check progress:** the progress during the sanity check run
2023-02-27 20:14:23 +00:00
- **train progress:** shows the training progress. It will pause if validation starts and will resume when it ends, and also accounts for multiple validation runs during training when :paramref: `~lightning.pytorch.trainer.trainer.Trainer.val_check_interval` is used.
2022-03-24 06:27:10 +00:00
- **validation progress:** only visible during validation; shows total progress over all validation datasets.
- **test progress:** only active when testing; shows total progress over all test datasets.
For infinite datasets, the progress bar never ends.
2023-02-27 20:14:23 +00:00
You can update `` refresh_rate `` (rate (number of batches) at which the progress bar get updated) for :class: `~lightning.pytorch.callbacks.TQDMProgressBar` by:
2022-03-24 06:27:10 +00:00
.. code-block :: python
2023-02-27 20:14:23 +00:00
from lightning.pytorch.callbacks import TQDMProgressBar
2022-03-24 06:27:10 +00:00
trainer = Trainer(callbacks=[TQDMProgressBar(refresh_rate=10)])
2023-02-27 20:14:23 +00:00
If you want to customize the default :class: `~lightning.pytorch.callbacks.TQDMProgressBar` used by Lightning, you can override
specific methods of the callback class and pass your custom implementation to the :class: `~lightning.pytorch.trainer.trainer.Trainer` .
2022-03-24 06:27:10 +00:00
.. code-block :: python
class LitProgressBar(TQDMProgressBar):
def init_validation_tqdm(self):
bar = super().init_validation_tqdm()
bar.set_description("running validation...")
return bar
trainer = Trainer(callbacks=[LitProgressBar()])
.. seealso ::
2023-02-27 20:14:23 +00:00
- :class: `~lightning.pytorch.callbacks.TQDMProgressBar` docs.
2022-03-24 06:27:10 +00:00
- `tqdm library <https://github.com/tqdm/tqdm> `__
----------------
RichProgressBar
---------------
`Rich <https://github.com/Textualize/rich> `_ is a Python library for rich text and beautiful formatting in the terminal.
2023-02-27 20:14:23 +00:00
To use the :class: `~lightning.pytorch.callbacks.RichProgressBar` as your progress bar, first install the package:
2022-03-24 06:27:10 +00:00
.. code-block :: bash
pip install rich
2023-02-27 20:14:23 +00:00
Then configure the callback and pass it to the :class: `~lightning.pytorch.trainer.trainer.Trainer` :
2022-03-24 06:27:10 +00:00
.. code-block :: python
2023-02-27 20:14:23 +00:00
from lightning.pytorch.callbacks import RichProgressBar
2022-03-24 06:27:10 +00:00
trainer = Trainer(callbacks=[RichProgressBar()])
2023-02-27 20:14:23 +00:00
Customize the theme for your :class: `~lightning.pytorch.callbacks.RichProgressBar` like this:
2022-03-24 06:27:10 +00:00
.. code-block :: python
2023-02-27 20:14:23 +00:00
from lightning.pytorch.callbacks import RichProgressBar
from lightning.pytorch.callbacks.progress.rich_progress import RichProgressBarTheme
2022-03-24 06:27:10 +00:00
# create your own theme!
progress_bar = RichProgressBar(
theme=RichProgressBarTheme(
description="green_yellow",
progress_bar="green1",
progress_bar_finished="green1",
progress_bar_pulse="#6206E0",
batch_progress="green_yellow",
time="grey82",
processing_speed="grey82",
metrics="grey82",
)
)
trainer = Trainer(callbacks=progress_bar)
2023-02-27 20:14:23 +00:00
You can customize the components used within :class: `~lightning.pytorch.callbacks.RichProgressBar` with ease by overriding the
:func: `~lightning.pytorch.callbacks.RichProgressBar.configure_columns` method.
2022-03-24 06:27:10 +00:00
.. code-block :: python
from rich.progress import TextColumn
custom_column = TextColumn("[progress.description]Custom Rich Progress Bar!")
class CustomRichProgressBar(RichProgressBar):
def configure_columns(self, trainer):
return [custom_column]
progress_bar = CustomRichProgressBar()
If you wish for a new progress bar to be displayed at the end of every epoch, you should enable
2023-02-27 20:14:23 +00:00
:paramref: `RichProgressBar.leave <lightning.pytorch.callbacks.RichProgressBar.leave>` by passing `` True ``
2022-03-24 06:27:10 +00:00
.. code-block :: python
2023-02-27 20:14:23 +00:00
from lightning.pytorch.callbacks import RichProgressBar
2022-03-24 06:27:10 +00:00
trainer = Trainer(callbacks=[RichProgressBar(leave=True)])
.. seealso ::
2023-02-27 20:14:23 +00:00
- :class: `~lightning.pytorch.callbacks.RichProgressBar` docs.
- :class: `~lightning.pytorch.callbacks.RichModelSummary` docs to customize the model summary table.
2022-03-24 06:27:10 +00:00
- `Rich library <https://github.com/Textualize/rich> `__ .
.. note ::
Progress bar is automatically enabled with the Trainer, and to disable it, one should do this:
.. code-block :: python
trainer = Trainer(enable_progress_bar=False)