python tqdm庫的使用
Tqdm庫比較常用,用于顯示進度條。
簡單用法:
from tqdm import tqdm for i in tqdm(range(2)): pass
100%|███████████████████| 2/2 [00:00<00:00, 1998.72it/s]
從上面可以看到生成一個長度為2的列表傳入tqdm中,在for中迭代,此時輸出了進度條,這里tqdm全部使用了默認參數(shù),默認進度條樣式就是如上所示;通常默認進度條所輸出的信息并不滿足我們的需求,tqdm還可以定制進度條樣式;
tdqm數(shù)據(jù)參數(shù)支持的數(shù)據(jù)類型是可迭代的對象iterable,在Python中默認的可迭代對象有:list、str、tuple、dict、file、xrange等,當(dāng)然還有自定義可迭代對象;
tqdm參數(shù)
desc=None, str類型,作為進度條說明 total=None, 預(yù)期的迭代次數(shù) file=None, 輸出方式,默認為sys.stderr ncols=None, 進度條長度 mininterval=0.1, 進度條最小的更新間隔,單位秒,默認:0.1 maxinterval=10.0, 進度條最大更新間隔,單位秒,默認:10 unit='it', 單位,默認it每秒迭代數(shù) bar_format=None, 進度條格式 postfix 字典形式信息,例如:速度=5
這些參數(shù)為相對比較常用的參數(shù),并且全部都是可選參數(shù);在自定義進度條當(dāng)中比較重要的的一個參數(shù)為:bar_format,用于定義進度條的具體格式,所包含的具體數(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ù)描述;從中可看出:
進度條默認格式為: {l_bar}{bar}{r_bar}
進度條分為三部分: 中間的圖形(bar),圖形左邊(l_bar)、圖形右邊(r_bar)
- l_bar: {desc}: {percentage:3.0f}%|
- bar: 進度條
- 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默認為空;
自定義進度條:
1、bar_format=
'進度:{percentage:3.0f}%|{bar}|{n}/{total}[{elapsed}<{remaining},{rate_fmt}{postfix}]'
進度:100%|████████████████████|3/3[00:03<00:00, 1.00s/it]
2、bar_format='進度:{percentage:3.0f}%|{bar}|{n}/{total}[{rate_fmt}{postfix}]'
進度:100%|████████████████████|3/3[ 1.00s/it]
批量數(shù)據(jù)進度條
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庫的使用的詳細內(nèi)容,更多關(guān)于python tqdm庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在Python中處理字符串之isdecimal()方法的使用
這篇文章主要介紹了在Python中處理字符串之isdecimal()方法的使用,是Python入門學(xué)習(xí)的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù)
這篇文章主要介紹了PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù),在本次的分類任務(wù)當(dāng)中,我們使用的數(shù)據(jù)集是 Mnist 數(shù)據(jù)集,這個數(shù)據(jù)集大家都比較熟悉,需要的朋友可以參考下2023-03-03python實現(xiàn)類之間的方法互相調(diào)用
下面小編就為大家分享一篇python實現(xiàn)類之間的方法互相調(diào)用,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python?pip安裝的包目錄(site-packages目錄的位置)
這篇文章主要介紹了python?pip安裝的包放在哪里(site-packages目錄的位置),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03