本文记录一下在学习深度强化学习过程中遇到tqdm
库显示进度条的用法,以供大家交流。
注意本文使用的tqdm
均是使用的tqdm
库中的同名tqdm
方法,应该按照如下方式导入
from tqdm import tqdm
该种用法比较简单,直接使用tqdm
包裹可迭代对象即可,例如
from tqdm import tqdm
import time
for _ in tqdm(range(30)):
time.sleep(0.1)
结果如下
100%|██████████| 30/30 [00:03<00:00, 9.20it/s]
循环外部的实例化允许手动控制 tqdm()
from tqdm import tqdm
import time
pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
time.sleep(0.25)
pbar.set_description("Processing %s" % char)
结果如下
Processing d: 100%|██████████| 4/4 [00:01<00:00, 3.83it/s]
利用python
中的上下文管理器with
来手动使用tqdm
,其中初始化参数如下
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=False,
lock_args=None, nrows=None, colour=None, delay=0, gui=False,
**kwargs):
"""
Parameters
----------
iterable : iterable, optional
Iterable to decorate with a progressbar.
Leave blank to manually manage the updates.
desc : str, optional
Prefix for the progressbar. 设置进度条的描述
total : int or float, optional 进度条总长
The number of expected iterations. If unspecified,
len(iterable) is used if possible. If float("inf") or as a last
resort, only basic progress statistics are displayed
(no ETA, no progressbar).
If `gui` is True and this parameter needs subsequent updating,
specify an initial arbitrary large positive number,
e.g. 9e9.
leave : bool, optional
If [default: True], keeps all traces of the progressbar
upon termination of iteration.
If `None`, will leave only if `position` is `0`.
例如
from tqdm import tqdm
import time
with tqdm(total=100) as pbar: #进度条总长100%
for i in range(10):
time.sleep(0.1)
pbar.update(10) #每次更新进度条10%
结果如下
100%|██████████| 100/100 [00:01<00:00, 91.47it/s]
再例如,同时保留多个进度条,其中在使用 tqdm
库时,set_postfix
方法用于在进度条中显示附加信息。它允许在进度条的右侧显示一些额外的信息,比如当前迭代的一些指标或状态。
set_postfix
方法可以接受一个字典作为参数,其中键值对表示要显示的附加信息。调用此方法后,进度条会更新并在右侧显示传入的信息。
from tqdm import tqdm
import time
num_episodes = 100 #最大的迭代次数
num_tqdm = 10 #进度条的数量
for i in range(num_tqdm):
with tqdm(total= num_episodes/num_tqdm, desc="Iteration %d" %i) as pbar:
for i_episode in range(int(num_episodes/num_tqdm)):
time.sleep(0.1)
pbar.update(1) # 更新一次tqdm
if (i_episode+1) % 10 == 0:
# tqdm控制输出
pbar.set_postfix({
"episode": "%d"%(num_episodes / num_tqdm * i + i_episode + 1)
})
结果如下
Iteration 0: 100%|██████████| 10/10.0 [00:01<00:00, 9.15it/s, episode=10]
Iteration 1: 100%|██████████| 10/10.0 [00:01<00:00, 9.17it/s, episode=20]
Iteration 2: 100%|██████████| 10/10.0 [00:01<00:00, 9.19it/s, episode=30]
Iteration 3: 100%|██████████| 10/10.0 [00:01<00:00, 9.18it/s, episode=40]
Iteration 4: 100%|██████████| 10/10.0 [00:01<00:00, 9.19it/s, episode=50]
Iteration 5: 100%|██████████| 10/10.0 [00:01<00:00, 9.23it/s, episode=60]
Iteration 6: 100%|██████████| 10/10.0 [00:01<00:00, 9.23it/s, episode=70]
Iteration 7: 100%|██████████| 10/10.0 [00:01<00:00, 9.23it/s, episode=80]
Iteration 8: 100%|██████████| 10/10.0 [00:01<00:00, 9.24it/s, episode=90]
Iteration 9: 100%|██████████| 10/10.0 [00:01<00:00, 9.19it/s, episode=100]
tqdm官方github