Search code examples
pythonpython-3.xtensorflowkerashyperparameters

Estimate times for hyperparameter tuning


I'm trying to get python to output the estimated time to train my model during hyperparameter tuning. I've tried using tqdm in a for loop to show the progress but the bar is cleared after the first completed trial.

Thanks

for i in tqdm(range(0,256)):

  def build_model(hp):
    model = keras.models.Sequential()

    model.add(Conv2D(hp.Int("input_units", min_value=32, max_value=256, step=32), kernel_size=(3, 3), input_shape=(img_size,img_size,3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    for i in range(hp.Int("number_layers", 4, 5, 1)):
      model.add(Conv2D(hp.Int(f"conv_{i}_units", min_value=32, max_value=256, step=32), (3, 3)))
      model.add(Activation('relu'))
      model.add(Dropout(0.2))

    model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors

    model.add(Dense(2))
    model.add(Activation("softmax"))

    model.compile(optimizer="adam",
                  loss="sparse_categorical_crossentropy",
                  metrics=["accuracy"])
      
    return model

  tuner = RandomSearch(
        build_model,
        objective='val_accuracy',
        max_trials=10,
        executions_per_trial=3
  )

  tuner.search(train_ds, validation_data=(val_ds), epochs=10, batch_size=64, callbacks=[tensorboard_callback])

Solution

  • You've got for i in range(...) in two nested loops. The inner one will be overwriting the value of i, confusing tdqm. Change one of them to use a different variable name.