嵌套工作正常:
from tqdm import trange
from time import sleep
n_epochs, n_steps = 5, 100
with trange(1, n_epochs + 1, desc="All epochs") as epochs:
for epoch in epochs:
with trange(1, n_steps + 1, desc="Epoch {}/{}".format(epoch, n_epochs)) as steps:
for step in steps:
epochs.set_postfix(foo=epoch * n_steps + step)
steps.set_postfix(bar="hello {}".format(step), baz=1 / step)
sleep(0.01)
编辑
运行时的输出如下所示(在此示例中,我们处于第三个 epoch 的中间):
Epoch 1/5: 100%|██████| 100/100 [00:01<00:00, 81.41it/s, bar=hello 100, baz=0.01]
Epoch 2/5: 100%|██████| 100/100 [00:01<00:00, 81.04it/s, bar=hello 100, baz=0.01]
All epochs: 40%|█████████▍ | 2/5 [00:03<00:04, 1.26s/it, foo=349]
Epoch 3/5: 48%|███▎ | 48/100 [00:00<00:00, 79.08it/s, bar=hello 49, baz=0.0204]
最后看起来像这样:
Epoch 1/5: 100%|██████| 100/100 [00:01<00:00, 81.41it/s, bar=hello 100, baz=0.01]
Epoch 2/5: 100%|██████| 100/100 [00:01<00:00, 81.04it/s, bar=hello 100, baz=0.01]
Epoch 3/5: 100%|██████| 100/100 [00:01<00:00, 80.23it/s, bar=hello 100, baz=0.01]
Epoch 4/5: 100%|██████| 100/100 [00:01<00:00, 80.27it/s, bar=hello 100, baz=0.01]
Epoch 5/5: 100%|██████| 100/100 [00:01<00:00, 80.20it/s, bar=hello 100, baz=0.01]
All epochs: 100%|█████████████████████████| 5/5 [00:06<00:00, 1.24s/it, foo=600]