0
$\begingroup$

I am building CNN algorithm that will output some values. I am using GridSearchCV for parameters tuning and I want to implement progress bar for handling with large datasets but I do not know how to.

Input:

def create_model(units):
    model = Sequential() # defining model
    model.add(Conv1D(filters = 64, kernel_size = 5, activation = 'relu', input_shape = (max_sensor_length, SENSORS))) # first convolution layer
    model.add(MaxPool1D(pool_size = 8)) # first pooling layer
    model.add(Conv1D(filters = 16, kernel_size = 5, activation = 'relu')) # 2. layer
    model.add(MaxPool1D(pool_size = 4))
    model.add(Conv1D(filters = 16, kernel_size = 5, activation = 'relu')) # 3. layer
    model.add(MaxPool1D(pool_size = 4))
    model.add(Flatten()) # adding fully connected layer
    model.add(Dense(units = units, activation = 'relu'))
    model.add(Dense(y_ohe.shape[1], activation = 'softmax')) # adding output layer
    model.compile(loss = 'categorical_crossentropy', 
                  optimizer = 'adam', 
                  metrics = ['accuracy'], 
                  ) # compiling the model
    return model

model = KerasRegressor(model = create_model, units = 5)

batch_size = [5, 10, 30, 50, 100]
epochs = [5, 10, 30, 50, 100]
units = [5, 10, 15, 30, 50, 100]

param_grid = dict(batch_size = batch_size, 
                    epochs = epochs,
                    units = units)

grid = GridSearchCV(estimator = model, 
                    param_grid = param_grid, 
                    n_jobs = -1, 
                    cv = 3, 
                    verbose = 10
                    )

grid_result = grid.fit(X_train, 
                       Y_train, 
                       validation_data = (X_test, Y_test)
                       )
$\endgroup$
1
  • $\begingroup$ Try to remove "n_jobs = -1" from the parameters $\endgroup$
    – laffrent
    Commented Nov 9, 2022 at 12:16

2 Answers 2

1
$\begingroup$

From the sklearn documentation on gridsearchCV

verbose (int) Controls the verbosity: the higher, the more messages.

1 : the computation time for each fold and parameter candidate is displayed;

2 : the score is also displayed;

3 : the fold and candidate parameter indexes are also displayed together with the starting time of the computation.

Just set verbose = 1 in the gridsearch parameters (not sure if 10 works?)

Probably you will regret this later as you will end up with a wall of text.

If you are talking about a progress bar for the CNN I would look at tqdm

$\endgroup$
1
$\begingroup$

Use this function, and instead of doing grid.fit(X_train, Y_train, validation_data=(X_test,Y_test)), you simply do fit(grid, X_train, Y_train, validation_data=(X_test,Y_test))

import sys
from tqdm.notebook import tqdm
# from tqdm import tqdm if not in notebook

def fit(model, *args, **kwargs):
    class BarStdout:
        def write(self, text):
            if "totalling" in text and "fits" in text:
                self.bar_size = int(text.split("totalling")[1].split("fits")[0][1:-1])
                self.bar = tqdm(range(self.bar_size))
                self.count = 0
                return
            if "CV" in text and hasattr(self,"bar"):
                self.count += 1
                self.bar.update(n=self.count-self.bar.n)
                if self.count%(self.bar_size//10)==0:
                    time.sleep(0.1)
        def flush(self, text=None):
            pass
    default_stdout= sys.stdout
    sys.stdout = BarStdout()
    model.verbose = 10
    model.fit(*args, **kwargs)
    sys.stdout = default_stdout
    return model
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.