天天看点

关于tqdm的使用

import numpy as np
from tqdm import tqdm
from time import sleep


a = range(50)
for j in range(50):
    with tqdm(a, ncols=120) as p_bar:
        for i in p_bar:
            p_bar.set_description(desc='Epoch: {:>5d}'.format(j))
            p_bar.set_postfix({'acc': '{:.>5f}'.format(np.random.rand()),
                               'loss': '{:.>5f}'.format(np.random.randn())})
            sleep(0.1)
            print('hah')
        print('acc: {:.3f}  |  loss: {:.3f}'.format(np.random.randn(), np.random.randn()))
        print(' ----- Model saved! ------ ')

           

情况一:

基础用法这里就不提及了,在tqdm的迭代中,如果加入了print()函数,则会出现这种情况,即一次迭代出现很多进度条:

关于tqdm的使用

情况二:

在tqdm中没有使用print()函数,则一次迭代只会出现一个进度条:

关于tqdm的使用

PS:

在服务器上跑代码时,加入print()函数不会出现输出顺序不按照真实输出的现象;如果在本地跑代码,则可能出现输出顺序随机的现象,如下:

关于tqdm的使用
关于tqdm的使用

注:这里的代码是在本地的Pycharm上跑的,网上查阅资料后,发现可以修改 RUN=>Edict Configurations=>Emulate terminal in output sonsole,勾上即可。

关于tqdm的使用