Gridea

使用 Tqdm 实时显示 Loss 和 Acc 等信息

使用 Tqdm 实时显示 Loss 和 Acc 等信息
2022-05-30 · 2 min read
Coding Python

Tqdm 是一个可以将 Python 脚本运行进度可视化的软件包,只需要封装进任何一个迭代器 tqdm(iterator) 或直接使用 trange() 代替 range(),即可在终端实时输出循环运行进度及预估剩余时间。

安装

使用 pip install 即可进行安装

pip install tqdm

使用方法

样例:

# 注意不能直接 import tqdm
from tqdm import tqdm
from time import sleep

for i in tqdm(range(10)):
    sleep(1)

或者也可以直接

for i in trange(10):
    sleep(i)

手动方式增加显示其他信息

from tqdm import tqdm, trange
from random import random, randint
from time import sleep

with trange(10) as t:
    for i in t:
        # Description will be displayed on the left
        t.set_description('GEN %i' % i)
        # Postfix will be displayed on the right,
        # formatted automatically based on argument's datatype
        t.set_postfix(loss=random(), gen=randint(1,999), str='h',
                      lst=[1, 2])
        sleep(0.1)

with tqdm(total=10, bar_format="{postfix[0]} {postfix[1][value]:>8.2g}",
          postfix=["Batch", dict(value=0)]) as t:
    for i in range(10):
        sleep(0.1)
        t.postfix[1]["value"] = i / 2
        t.update()

参考资料

Github 主页