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]
從上面可以看到生成一個長度為2的列表傳入tqdm中,在for中迭代,此時輸出了進(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.stderr ncols=None, 進(jìn)度條長度 mininterval=0.1, 進(jìn)度條最小的更新間隔,單位秒,默認(rèn):0.1 maxinterval=10.0, 進(jìn)度條最大更新間隔,單位秒,默認(rèn):10 unit='it', 單位,默認(rèn)it每秒迭代數(shù) bar_format=None, 進(jìn)度條格式 postfix 字典形式信息,例如:速度=5
這些參數(shù)為相對比較常用的參數(shù),并且全部都是可選參數(shù);在自定義進(jìn)度條當(dāng)中比較重要的的一個參數(shù)為:bar_format,用于定義進(jìn)度條的具體格式,所包含的具體數(shù)據(jù)信息;
下面主要介紹這個參數(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:消耗的時間 remaining:剩余時間 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 np
from torch.utils.data import DataLoader
import time
from tqdm import tqdm, tqdm_notebook
from random import random
data =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 =0
for 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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python基于遞歸算法求最小公倍數(shù)和最大公約數(shù)示例
這篇文章主要介紹了Python基于遞歸算法求最小公倍數(shù)和最大公約數(shù),結(jié)合實例形式分析了Python使用遞歸算法進(jìn)行數(shù)值計算的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
python矩陣的轉(zhuǎn)置和逆轉(zhuǎn)實例
今天小編就為大家分享一篇python矩陣的轉(zhuǎn)置和逆轉(zhuǎn)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Pytorch中torch.utils.checkpoint()及用法詳解
在PyTorch中,torch.utils.checkpoint?模塊提供了實現(xiàn)梯度檢查點(也稱為checkpointing)的功能,這篇文章給大家介紹了Pytorch中torch.utils.checkpoint()的相關(guān)知識,感興趣的朋友一起看看吧2024-03-03
python 實現(xiàn)二維字典的鍵值合并等函數(shù)
今天小編就為大家分享一篇python 實現(xiàn)二維字典的鍵值合并等函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

