Training models with a progress bar | by Adam Oudad | Towards Data Science

tqdm is a Python library for adding progress bar. It lets you configure and display a progress bar with metrics you want to track. Its ease of use and versatility makes it the perfect choice for tracking machine learning experiments.
I organize this tutorial in two parts. I will first introduce tqdm, then show an example for machine learning. For each code fragment in this article, we will import the sleep function from Python's time library as it will let us slow down the program to see the progress bar update.
from time import sleep
You can install tqdm with pip install tqdm. The library comes with various iterators each dedicated to a specific use that I am going to present.
tqdm is the default iterator. It takes an iterator object as argument and displays a progress bar as it iterates over it.
Output is
100%|█████████████████████████████████| 5/5 [00:00<00:00, 9.90it/s]
You can see the nice output with 9.90it/s meaning an average speed of 9.90 iterations per second. "it" for iterations can be configured to something else and this is what we will see on the next example.
trange follows the same template as range in Python. For example, give to trange the number of iterations.
Proving P=NP: 100%|████████████| 20/20 [00:02<00:00, 9.91carrots/s]
You can see in this example we added a (joke) description of what we are doing and the unit for each iteration.
tqdm has two methods that can update what is displayed in the progress bar.
To use these methods, we need to assign the tqdm iterator instance to a variable. This can be done either with the = operator or the with keyword in Python.
We can for example update the postfix with the list of divisors of the number i. Let's use this function to get the list of divisors
And here is our code with progress bar.
If you feel like a distinguished Python programmer, you may use with keyword like so
Using with will automatically call pbar.close() at the end of the block.
Here is the status displayed at i=6.
Testing even number 6: 70%|██████████████▋ | 7/10 [00:03<00:01, 1.76carrots/s, divisors=[1, 2, 3]]
In this section, we use a neural network wrote in PyTorch and train it using tqdm to display the loss and accuracy. Here is the model
This is a simple perceptron model that we can use for processing and classifying images of digits from MNIST dataset. The following code for loading MNIST dataset is inspired from a PyTorch example.
We just loaded our data, and defined our model and settings, we can now run a training experiment.
I am once again using sleep function to pause the program so that we can see the update of the progress bar. As you can see, we just applied what we learned previously here, in particular with tepoch.set_postfix and tepoch.set_description which let you update the information displayed by the progress bar. Here is a capture of the output while the program was running
Epoch 1: 15%|▉ | 142/937 [00:16<01:32, 8.56batch/s, accuracy=89.1, loss=0.341]
This gives us an idea of how tqdm can be used in practical.
You can achieve much more with tqdm, like adapting it to Jupyter notebooks, finely configuring the progress bar updates or nesting progress bars, so I recommend you to read the documentation for more: https://github.com/tqdm/tqdm
Thank you for reading!
Originally published at https://adamoudad.github.io on October 12, 2020.