What's the difference between the two? tqdm wraps around any iterable. But I am not sure how tqdm functions when it's given two arguments.
# train_ids = list
elements = ('a', 'b', 'c')
for count, ele in tqdm(enumerate(elements)):
print(count, i)
# two arguments
for count, ele in tqdm(enumerate(elements), total=len(train_ids)):
print(count, i)
tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. tqdm got its name from the Arabic name taqaddum which means 'progress'.
In Arabic, tqdm (taqadum) means progress, and it is used to create a smart progress bar for the loops. You just need to wrap tqdm on any iterable - tqdm(iterable).
trange(i) is a special optimised instance of tqdm(range(i)): from tqdm import trange for i in trange(100): sleep(0.01)
tqdm is the default iterator. It takes an iterator object as argument and displays a progress bar as it iterates over it. You can see the nice output with 9.90it/s meaning an average speed of 9.90 iterations per second.
Straight from the documentation:
If the optional variable total (or an iterable with len()) is provided, predictive stats are displayed.
Also from the documentation:
total
:int
, optional .The number of expected iterations. If (default: None), len(iterable) is used if possible. 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 integer, e.g. int(9e9).
When you provide total
as a parameter to tqdm
, you are giving it an estimate for how many iterations the code should take to run, so it will provide you with predictive information (even if the iterable you have provided does not have a length).
Example
If we provide a generator (something without a __len__
) to tqdm
without a total
argument, we don't get a progress bar, we just get elapsed time:
no_len = (i for i in range(50))
for i in tqdm(no_len):
time.sleep(0.1)
# Result
19it [00:01, 9.68it/s]
However, if we use the total
parameter to give expected iterations, tqdm
will now estimate progress:
for i in tqdm(no_len, total=49):
time.sleep(0.1)
# Result
94%|████████████████████████████████████████▎ | 46/49 [00:04<00:00, 9.72it/s
In addition to the total
parameter, tqdm
has a whole set of additional parameters that you can find here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With