如何在Pytorch Lightning中禁用进度条?

16

在Pytorch Lightning中,我对tqdm进度条有很多问题:

  • 当我在终端运行训练时,进度条会相互覆盖。在训练一个epoch结束后,验证进度条会在训练进度条下面打印出来,但当它结束后,下一个训练epoch的进度条就会覆盖前一个epoch的进度条,因此无法看到先前epoch的损失。
INFO:root:  Name    Type Params
0   l1  Linear    7 K
Epoch 2:  56%|████████████▊          | 2093/3750 [00:05<00:03, 525.47batch/s, batch_nb=1874, loss=0.714, training_loss=0.4, v_nb=51]
  • 进度条从左到右晃动,是由于某些损失小数点后数字的变化所导致。
  • 在Pycharm中运行时,验证进度条未被打印,但相反地,
INFO:root:  Name    Type Params
0   l1  Linear    7 K
Epoch 1:  50%|█████     | 1875/3750 [00:05<00:05, 322.34batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  50%|█████     | 1879/3750 [00:05<00:05, 319.41batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  52%|█████▏    | 1942/3750 [00:05<00:04, 374.05batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  53%|█████▎    | 2005/3750 [00:05<00:04, 425.01batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  55%|█████▌    | 2068/3750 [00:05<00:03, 470.56batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  57%|█████▋    | 2131/3750 [00:05<00:03, 507.69batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  59%|█████▊    | 2194/3750 [00:06<00:02, 538.19batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  60%|██████    | 2257/3750 [00:06<00:02, 561.20batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  62%|██████▏   | 2320/3750 [00:06<00:02, 579.22batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  64%|██████▎   | 2383/3750 [00:06<00:02, 591.58batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  65%|██████▌   | 2445/3750 [00:06<00:02, 599.77batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  67%|██████▋   | 2507/3750 [00:06<00:02, 605.00batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  69%|██████▊   | 2569/3750 [00:06<00:01, 607.04batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
Epoch 1:  70%|███████   | 2633/3750 [00:06<00:01, 613.98batch/s, batch_nb=1874, loss=1.534, training_loss=1.72, v_nb=49]
我想知道这些问题能否解决,或者如何禁用进度条,而是在屏幕上只打印一些日志细节。

我想知道这些问题能否解决,或者如何禁用进度条,而是在屏幕上只打印一些日志细节。

5个回答

20

提醒信息:自版本0.7.2起,show_progress_bar=False已被弃用,但您可以使用progress_bar_refresh_rate=0


更新:

progress_bar_refresh_rate在v1.5中已被弃用,并将在v1.7中删除。要禁用进度条,请将enable_progress_bar设置为false。

progress_bar_refresh_rate: How often to refresh progress bar (in steps). Value ``0`` disables progress bar.
    Ignored when a custom progress bar is passed to :paramref:`~Trainer.callbacks`. Default: None, means
    a suitable value will be chosen based on the environment (terminal, Google COLAB, etc.).

    .. deprecated:: v1.5
        ``progress_bar_refresh_rate`` has been deprecated in v1.5 and will be removed in v1.7.
        Please pass :class:`~pytorch_lightning.callbacks.progress.TQDMProgressBar` with ``refresh_rate``
        directly to the Trainer's ``callbacks`` argument instead. To disable the progress bar,
        pass ``enable_progress_bar = False`` to the Trainer.

enable_progress_bar: Whether to enable to progress bar by default.

似乎这只适用于 trainer.fit,而不适用于 trainer.test - Thomas Ahle

7

在Trainer中使用命令show_progress_bar=False


5
在1.0.5版本中,这个不再起作用了(请参见下面的答案)。 - dopexxx

5
我想知道这些问题是否可以解决,如果不能,那么我该如何禁用进度条并在屏幕上打印一些日志详情。据我所知,这个问题仍未解决。PL团队指出这是“TQDM相关的事情”,他们无法做任何事情。也许你想读一下this issue我的临时解决方案是:
from tqdm import tqdm

class LitProgressBar(ProgressBar):
   
    def init_validation_tqdm(self):
        bar = tqdm(            
            disable=True,            
        )
        return bar

bar = LitProgressBar()
trainer = Trainer(callbacks=[bar])

这种方法只是禁用了验证进度条,使你能够保留正确的训练条[参考12]。请注意,使用progress_bar_refresh_rate=0将禁用所有进度条的更新。
更好的解决方案:(更新1 2021-07-22)
根据这个答案tqdm似乎只在PyCharm控制台中出现故障。因此,可能的答案是对Pycharm进行一些设置。幸运的是,我找到了这个答案
  1. Go to "Edit configurations". Click on the run/debug configuration that is being used. There should be an option "Emulate terminal in output console". Check that. Image added for reference. image

  2. Along with the position argument also set the leave argument. The code should look like this. I have added ncols so that the progress bar doesn't take up whole of the console.

from tqdm import tqdm import time
for i in tqdm(range(5), position=0, desc="i", leave=False, colour='green', ncols=80):
    for j in tqdm(range(10), position=1, desc="j", leave=False, colour='red', ncols=80):
        time.sleep(0.5) 

When the code is now run, the output of the console is as shown below.

i:  20%|████████▍                                 | 1/5 [00:05<00:20,  5.10s/it] 
j:  60%|████████████████████████▌            | 6/10 [00:03<00:02,  1.95it/s] 
通过上述两个步骤,我们可以在Pycharm中以正常方式显示进度条。要完成Pytorch-lightning中的第二步,我们需要重写函数init_train_tqdm()、init_validation_tqdm()、init_test_tqdm()来更改ncols。一些示例代码如下(希望能有所帮助):
class LitProgressBar(ProgressBar):

    def init_train_tqdm(self) -> tqdm:
        """ Override this to customize the tqdm bar for training. """
        bar = tqdm(
            desc='Training',
            initial=self.train_batch_idx,
            position=(2 * self.process_position),
            disable=self.is_disabled,
            leave=True,
            dynamic_ncols=False,  # This two lines are only for pycharm
            ncols=100,
            file=sys.stdout,
            smoothing=0,
        )
        return bar

    def init_validation_tqdm(self) -> tqdm:
        """ Override this to customize the tqdm bar for validation. """
        # The main progress bar doesn't exist in `trainer.validate()`
        has_main_bar = self.main_progress_bar is not None
        bar = tqdm(
            desc='Validating',
            position=(2 * self.process_position + has_main_bar),
            disable=self.is_disabled,
            leave=False,
            dynamic_ncols=False,
            ncols=100,
            file=sys.stdout
        )
        return bar

    def init_test_tqdm(self) -> tqdm:
        """ Override this to customize the tqdm bar for testing. """
        bar = tqdm(
            desc="Testing",
            position=(2 * self.process_position),
            disable=self.is_disabled,
            leave=True,
            dynamic_ncols=False,
            ncols=100,
            file=sys.stdout
        )
        return bar

如果无法正常工作,请将Pytorch-lightning版本更新到最新版。

最近,我注意到当在Trainer中传递自定义进度条回调时,progress_bar_refresh_rate将被忽略。因此,如果您想更改进度条的刷新速率,请使用'bar = LitProgressBar(refresh_rate=your_refresh_rate)'而不是'bar = LitProgressBar()'。 - lollows
你是否在使用 ProgressBar 而不是 tqdm?class LitProgressBar(ProgressBar)。我得到了 NameError: name 'ProgressBar' is not defined - Sameer

2

要禁用进度条,请将enable_progress_bar = False传递给Trainer。


这是 PyTorch Lightning 2.x 的正确答案。 - Matthew Cox

0

如果你想关闭进度条,可以在Trainer中使用此方法。将"progress_bar_refresh_rate"参数设置为0将禁用进度条,但是如果你在回调函数中指定了自己的进度条,则此设置将被忽略。注意,pl是pytorch lightning模块 (import pytorch_lightning as pl) ,这可能与您的风格不同。

trainer = pl.Trainer(..., progress_bar_refresh_rate=0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接