Search code examples
pythontqdm

Progress bar in my custom grid search using tqdm


I am implementing the grid search from scratch. This is the code:

def custom_grid_search(x_train, latent_dimensions):
    param_grid = {
        'epochs': [1, 3, 5],
        'l_rate': [10 ** -4],
        'batch_size': [32, 64],
        'patience': [30]
    }

    grid_search = CustomGridSearchCV(param_grid)
    grid_search.fit(x_train, latent_dimensions)

    return grid_search

I want to print a progress bar and I often use tqdm for showing progress bar over loops.

This is my code:

from tqdm import tqdm

class CustomGridSearchCV:
    def __init__(self, param_grid):
        self.param_grid = param_grid
        self.best_params_ = {}
        self.best_score_ = None
        self.grid_ = []

    def fit(self, x_train, latent_dimensions):
        ssim_scorer = my_ssim
        param_combinations = product(*self.param_grid.values())

        for params in param_combinations:
            print("CIAO")  # Never printed!
            # other code omitted...

        return self

But unfortunately "CIAO" is never printed.

I debugged those variables that is:

>>> param_combinations
<itertools.product object at 0x289618300>
>>> len(list(param_combinations))
6

If I remove tqdm, that is for params in param_combinations: it works properly. How is that?

I want to print something like

Combination 1/6
Combination 2/6
...

Solution

  • I fixed as Andrej Kesey noticed in their comment:

    Replace

    param_combinations = product(*self.param_grid.values())
    

    with

    param_combinations = list(product(*self.param_grid.values()))  # list