栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python tqdm模块制作个性化进度条

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python tqdm模块制作个性化进度条

python tqdm模块制作个性化进度条

tqdm模块默认的进度条里会有不让人喜欢的剩余时间与速度显示

看tqdm模块的源代码,可以看到,关于构造函数的参数说明,其中有一个bar_format参数定义了进度框的形状

def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None,
                 ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None,
                 ascii=None, disable=False, unit='it', unit_scale=False,
                 dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0,
                 position=None, postfix=None, unit_divisor=1000, write_bytes=None,
                 lock_args=None, nrows=None, colour=None, delay=0, gui=False,
                 **kwargs):
        """
        Parameters
        ----------
        bar_format  : str, optional
            Specify a custom bar string formatting. May impact performance.
            [default: '{l_bar}{bar}{r_bar}'], where
            l_bar='{desc}: {percentage:3.0f}%|' and
            r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, '
              '{rate_fmt}{postfix}]'
            Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
              percentage, elapsed, elapsed_s, ncols, nrows, desc, unit,
              rate, rate_fmt, rate_noinv, rate_noinv_fmt,
              rate_inv, rate_inv_fmt, postfix, unit_divisor,
              remaining, remaining_s, eta.
            Note that a trailing ": " is automatically removed after {desc}
            if the latter is empty.

bar_format默认的参数为{l_bar}{bar}{r_bar},其中{r_bar}就是进度条后面的描述信息因此,为了不显示时间与速度,只需要更改bar_format参数,就能得到个性化的进度条。
bar_format = '{l_bar}{bar}| [{n_fmt}/{total_fmt}{postfix}]'
下面是示例代码

from tqdm import tqdm
from time import sleep
epoch = 0
with tqdm(total=100,desc=f'Epoch {epoch + 1}/{100}',postfix=dict,mininterval=0.3, bar_format = '{l_bar}{bar}| [{n_fmt}/{total_fmt}{postfix}]') as pbar:
    for i in range(100):
        pbar.set_postfix(**{'loss'  : epoch / (99 + 1), 
                        'lr'    : epoch})
        pbar.update(1)
        if i == 50:
            pbar.set_description(f'Epoch {epoch + 2}/{100}')
        sleep(0.1)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号