python tqdm庫的使用
Tqdm庫比較常用,用于顯示進(jìn)度條。
簡單用法:
from tqdm import tqdm for i in tqdm(range(2)): pass
100%|???????????????????| 2/2 [00:00<00:00, 1998.72it/s]
從上面可以看到生成一個(gè)長度為2的列表傳入tqdm中,在for中迭代,此時(shí)輸出了進(jìn)度條,這里tqdm全部使用了默認(rèn)參數(shù),默認(rèn)進(jìn)度條樣式就是如上所示;通常默認(rèn)進(jìn)度條所輸出的信息并不滿足我們的需求,tqdm還可以定制進(jìn)度條樣式; tdqm數(shù)據(jù)參數(shù)支持的數(shù)據(jù)類型是可迭代的對象iterable,在Python中默認(rèn)的可迭代對象有:list、str、tuple、dict、file、xrange等,當(dāng)然還有自定義可迭代對象;
tqdm參數(shù)
desc=None, str類型,作為進(jìn)度條說明total=None, 預(yù)期的迭代次數(shù) file=None, 輸出方式,默認(rèn)為sys.stderrncols=None, 進(jìn)度條長度mininterval=0.1, 進(jìn)度條最小的更新間隔,單位秒,默認(rèn):0.1maxinterval=10.0, 進(jìn)度條最大更新間隔,單位秒,默認(rèn):10unit=’it’, 單位,默認(rèn)it每秒迭代數(shù)bar_format=None, 進(jìn)度條格式postfix 字典形式信息,例如:速度=5
這些參數(shù)為相對比較常用的參數(shù),并且全部都是可選參數(shù);在自定義進(jìn)度條當(dāng)中比較重要的的一個(gè)參數(shù)為:bar_format,用于定義進(jìn)度條的具體格式,所包含的具體數(shù)據(jù)信息; 下面主要介紹這個(gè)參數(shù)的具體用法;
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. Note that a trailing ': ' is automatically removed after {desc} if the latter is empty.
上面為tqdm對bar_format的參數(shù)描述;從中可看出:進(jìn)度條默認(rèn)格式為: {l_bar}{bar}{r_bar}進(jìn)度條分為三部分: 中間的圖形(bar),圖形左邊(l_bar)、圖形右邊(r_bar)
l_bar: {desc}: {percentage:3.0f}%| bar: 進(jìn)度條 r_bar: |{n_fmt}/{total_fmt}[{elapsed}<{remaining},{rate_fmt}{postfix}]100%|?????????????????| 3/3 [00:03<00:00, 1.00s/it]
percentage:百分比n_fmt:當(dāng)前數(shù)total_fmt:總數(shù)elapsed:消耗的時(shí)間remaining:剩余時(shí)間rate_fmt:速率postifx:后綴字典描述desc、postfix默認(rèn)為空;
自定義進(jìn)度條:
1、bar_format=’進(jìn)度:{percentage:3.0f}%|{bar}|{n}/{total}[{elapsed}<{remaining},{rate_fmt}{postfix}]’進(jìn)度:100%|????????????????????|3/3[00:03<00:00, 1.00s/it]
2、bar_format=’進(jìn)度:{percentage:3.0f}%|{bar}|{n}/{total}[{rate_fmt}{postfix}]’進(jìn)度:100%|????????????????????|3/3[ 1.00s/it]
批量數(shù)據(jù)進(jìn)度條
import numpy as npfrom torch.utils.data import DataLoaderimport timefrom tqdm import tqdm, tqdm_notebookfrom random import randomdata =np.array([1,2,3,4])data_loader = DataLoader(data, batch_size=2, num_workers=0, shuffle=False)iterator = tqdm(data_loader,maxinterval=10,mininterval=2, ncols=80,bar_format=’{l_bar}|{bar}| {n_fmt}/{total_fmt} [{rate_fmt}{postfix}|{elapsed}<{remaining}]’,nrows=10,smoothing=0.1)epoch =0for d in iterator: time.sleep(2) epoch +=1 print(d) iterator.set_description(’epoch %d’ %epoch) iterator.set_postfix_str(’loss={:^7.3f}’.format(random()))
以上就是python tqdm庫的使用的詳細(xì)內(nèi)容,更多關(guān)于python tqdm庫的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)2. CSS自定義滾動條樣式案例詳解3. 通過工廠模式返回Spring Bean方法解析4. python 批量下載bilibili視頻的gui程序5. python numpy庫np.percentile用法說明6. Ajax提交post請求案例分析7. 關(guān)于Mysql-connector-java驅(qū)動版本問題總結(jié)8. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)之類屬性與類常量實(shí)現(xiàn)方法分析
