python tqdm庫(kù)的使用
Tqdm庫(kù)比較常用,用于顯示進(jìn)度條。
簡(jiǎn)單用法:
from tqdm import tqdm for i in tqdm(range(2)): pass
100%|???????????????????| 2/2 [00:00<00:00, 1998.72it/s]
從上面可以看到生成一個(gè)長(zhǎng)度為2的列表傳入tqdm中,在for中迭代,此時(shí)輸出了進(jìn)度條,這里tqdm全部使用了默認(rèn)參數(shù),默認(rèn)進(jìn)度條樣式就是如上所示;通常默認(rèn)進(jìn)度條所輸出的信息并不滿(mǎn)足我們的需求,tqdm還可以定制進(jìn)度條樣式; tdqm數(shù)據(jù)參數(shù)支持的數(shù)據(jù)類(lèi)型是可迭代的對(duì)象iterable,在Python中默認(rèn)的可迭代對(duì)象有:list、str、tuple、dict、file、xrange等,當(dāng)然還有自定義可迭代對(duì)象;
tqdm參數(shù)
desc=None, str類(lèi)型,作為進(jìn)度條說(shuō)明total=None, 預(yù)期的迭代次數(shù) file=None, 輸出方式,默認(rèn)為sys.stderrncols=None, 進(jìn)度條長(zhǎng)度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ù)為相對(duì)比較常用的參數(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對(duì)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庫(kù)的使用的詳細(xì)內(nèi)容,更多關(guān)于python tqdm庫(kù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. 解析原生JS getComputedStyle4. 無(wú)線(xiàn)標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題8. css代碼優(yōu)化的12個(gè)技巧9. 利用CSS3新特性創(chuàng)建透明邊框三角10. 前端從瀏覽器的渲染到性能優(yōu)化
