tqdm of python module: a simple tutorial

python module------tqdm

tqdm lets your program display a smart progress meter that can wrap any iterable. In addition to the low-overhead feature, tqdman intelligent algorithm is used to predict the remaining time and skip unnecessary iterative displays, and its overhead is negligible in most cases.

usage

As follows:

from tqdm import tqdm

for i in tqdm(range(10000)):
    pass

for i in trange(10):
    pass

Effect:
insert image description here
trange()is tqdm(range(i))a special optimization instance of

tqdm object

Decorates an iterable, returning an iterator that behaves exactly like the original iterable, but prints a dynamically updated progress bar each time a value is requested.

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):

Description of common parameters:

iterable=None: 一个可迭代对象
desc=None: str类型,进度条的前缀
total=None: int or float, 预期的迭代次数。如果未指定,如果可能,使用 len(iterable)
file=None: `io.TextIOWrapper` or `io.StringIO`, optional
			Specifies where to output the progress messages
			(default: sys.stderr). Uses `file.write(str)` and `file.flush()`  
			methods.  For encoding, see `write_bytes`.
ncols=None: int, optional  
		    整个输出消息的宽度。
mininterval=0.1: float, optional  
		最小进度显示更新间隔 [默认值:0.1] 秒。
maxinterval  : float, optional  
	    最大进度显示更新间隔 [默认:10] 秒。
disable  : bool, optional  
	    是否禁用整个进度条包装器

application

Neural network training progress display:

with tqdm(initial=self.step, total=self.train_num_steps, disable=not accelerator.is_main_process) as pbar:
	...
	# 设置
	pbar.set_description(f'loss: {
      
      total_loss:.4f}')
	# 手动更新进度
	pbar.update(1)

Effect:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45248370/article/details/130784507