本文主要是介绍深度学习进度显示神器:tqdm详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 tqdm介绍
tqdm是Python的一个进度条库,可以在 Python长循环中添加一个进度提示信息。用户只需要封装任意的迭代器,是一个快速、扩展性强的进度条工具库。
tqdm的优点如下所述:
- 易于使用:只需在 Python 循环中包裹你的迭代器,一行代码就能产生一个精美的进度条。
- 灵活:它可以和 for 循环、pandas dataframe的 apply 函数以及 Python 的 map 函数等等配合使用。
- 高效:tqdm 使用了智能算法,即使在数据流非常快的情况下,也不会拖慢你的代码速度。
tqdm可以用在网络文件下载、数据预处理任务、深度学习模型训练等方面,将任务处理进度可视化显示。
2 tqdm使用示例
2.1 tqdm直接传入可迭代对象
import time
from tqdm import tqdmfor i in tqdm(range(60)):print('step i:', i)time.sleep(0.01)
运行结果显示如下:(默认情况下,0.1s打印一次进度信息)
0%| | 0/60 [00:00<?, ?it/s]step i: 0
step i: 1
step i: 2
step i: 3
step i: 4
step i: 5
step i: 610%|█ | 6/60 [00:00<00:00, 58.99it/s]step i: 7
step i: 8
step i: 9
step i: 10
step i: 11
step i: 1222%|██▏ | 13/60 [00:00<00:00, 61.25it/s]step i: 13
step i: 14
step i: 15
step i: 16
step i: 17
step i: 18
step i: 19
step i: 2033%|███▎ | 20/60 [00:00<00:00, 62.46it/s]step i: 21
step i: 22
step i: 23
step i: 24
step i: 25
step i: 2645%|████▌ | 27/60 [00:00<00:00, 63.34it/s]step i: 27
step i: 28
step i: 29
step i: 30
step i: 31
step i: 32
step i: 3357%|█████▋ | 34/60 [00:00<00:00, 63.18it/s]step i: 34
step i: 35
step i: 36
step i: 37
step i: 38
step i: 39
step i: 40
step i: 4168%|██████▊ | 41/60 [00:00<00:00, 63.71it/s]step i: 42
step i: 43
step i: 44
step i: 45
step i: 46
step i: 47
step i: 4880%|████████ | 48/60 [00:00<00:00, 63.53it/s]step i: 49
step i: 50
step i: 51
step i: 52
step i: 53
step i: 5492%|█████████▏| 55/60 [00:00<00:00, 63.35it/s]step i: 55
step i: 56
step i: 57
step i: 58
step i: 59
100%|██████████| 60/60 [00:00<00:00, 63.08it/s]
2.2 使用trange(i)
相当于tqdm(range(i))
的简单写法
from tqdm import trange
import timefor t in trange(60):print('step i:', t)time.sleep(0.01)
运行结果显示如下:
0%| | 0/60 [00:00<?, ?it/s]step i: 0
step i: 1
step i: 2
step i: 3
step i: 4
step i: 5
step i: 610%|█ | 6/60 [00:00<00:00, 57.70it/s]step i: 7
step i: 8
step i: 9
step i: 10
step i: 11
step i: 12
step i: 1322%|██▏ | 13/60 [00:00<00:00, 61.68it/s]step i: 14
step i: 15
step i: 16
step i: 17
step i: 18
step i: 19
step i: 2033%|███▎ | 20/60 [00:00<00:00, 62.57it/s]step i: 21
step i: 22
step i: 23
step i: 24
step i: 25
step i: 26
step i: 2745%|████▌ | 27/60 [00:00<00:00, 63.50it/s]step i: 28
step i: 29
step i: 30
step i: 31
step i: 32
step i: 33
step i: 3457%|█████▋ | 34/60 [00:00<00:00, 63.57it/s]step i: 35
step i: 36
step i: 37
step i: 38
step i: 39
step i: 40
step i: 4168%|██████▊ | 41/60 [00:00<00:00, 63.65it/s]step i: 42
step i: 43
step i: 44
step i: 45
step i: 46
step i: 4780%|████████ | 48/60 [00:00<00:00, 63.89it/s]step i: 48
step i: 49
step i: 50
step i: 51
step i: 52
step i: 53
step i: 5492%|█████████▏| 55/60 [00:00<00:00, 63.78it/s]step i: 55
step i: 56
step i: 57
step i: 58
step i: 59
100%|██████████| 60/60 [00:00<00:00, 63.41it/s]
2.3 使用update
手动控制进度条更新的进度
from tqdm import tqdm
import timepbar = tqdm(total=100)
for i in range(10): # 总共更新 10 次print('step i:', i)pbar.update(10) # 每次更新步长为 10time.sleep(1)
运行结果显示如下:
step i: 020%|██ | 20/100 [00:01<00:04, 19.93it/s]step i: 130%|███ | 30/100 [00:02<00:04, 14.03it/s]step i: 2
step i: 350%|█████ | 50/100 [00:04<00:04, 11.26it/s]step i: 460%|██████ | 60/100 [00:05<00:03, 10.77it/s]step i: 5
step i: 680%|████████ | 80/100 [00:07<00:01, 10.29it/s]step i: 7
step i: 8
100%|██████████| 100/100 [00:09<00:00, 10.09it/s]step i: 9
100%|██████████| 100/100 [00:10<00:00, 9.91it/s]
2.4 使用write更新进度信息
from tqdm import trange, tqdm
import timepbar = trange(10)
for i in pbar:time.sleep(1)if not (i % 2):tqdm.write('finish task %i' % i)
运行代码显示如下:
10%|█ | 1/10 [00:01<00:09, 1.01s/it]finish task 030%|███ | 3/10 [00:03<00:07, 1.01s/it]finish task 250%|█████ | 5/10 [00:05<00:05, 1.01s/it]finish task 470%|███████ | 7/10 [00:07<00:03, 1.01s/it]finish task 690%|█████████ | 9/10 [00:09<00:01, 1.01s/it]finish task 8
100%|██████████| 10/10 [00:10<00:00, 1.01s/it]
2.5 通过set_description
和set_postfix
设置进度条显示信息
from random import random, randint
from tqdm import trange
import timewith trange(10) as t:for i in t:print('step i:', i)t.set_description("GEN %i" % i) # 进度条左边显示信息t.set_postfix(loss=random(), gen=randint(1, 999), str="h", lst=[1, 2]) # 进度条右边显示信息time.sleep(0.1)
运行代码显示如下:
GEN 0: 0%| | 0/10 [00:00<?, ?it/s, gen=853, loss=0.984, lst=[1, 2], str=h]step i: 0
step i: 1
GEN 2: 20%|██ | 2/10 [00:00<00:00, 8.97it/s, gen=542, loss=0.782, lst=[1, 2], str=h]step i: 2
GEN 3: 30%|███ | 3/10 [00:00<00:00, 9.11it/s, gen=399, loss=0.923, lst=[1, 2], str=h]step i: 3
step i: 4
GEN 5: 50%|█████ | 5/10 [00:00<00:00, 9.09it/s, gen=460, loss=0.824, lst=[1, 2], str=h]step i: 5
GEN 6: 60%|██████ | 6/10 [00:00<00:00, 9.11it/s, gen=657, loss=0.657, lst=[1, 2], str=h]step i: 6
GEN 7: 70%|███████ | 7/10 [00:00<00:00, 9.14it/s, gen=340, loss=0.791, lst=[1, 2], str=h]step i: 7
step i: 8
GEN 9: 90%|█████████ | 9/10 [00:00<00:00, 9.13it/s, gen=104, loss=0.578, lst=[1, 2], str=h]step i: 9
GEN 9: 100%|██████████| 10/10 [00:01<00:00, 9.13it/s, gen=104, loss=0.578, lst=[1, 2], str=h]
这篇关于深度学习进度显示神器:tqdm详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!