'Bayesian Optimization for hyperparameter tuning

I have a problem with this code. I need to optimize dropout rate and learning rate. My code is reported below but the optimizer = BayesianOptimization() doesn't work. Is there someone that can help? This is my model:

input_shape=(10,256,256,1)
NUM_CLASSES=2

def get_model(input_shape, dropout1_rate=0.5, dense_1_neurons=128):
model = models.Sequential()
model.add(layers.TimeDistributed(getConvModel(verbose), input_shape=input_shape, 
name="conv2d_1")) 
model.add(layers.ConvLSTM2D(filters=5, kernel_size=(3,3),"conv2d_lstm"))   
model.add(layers.Dropout(dropout1_rate,name="dropout_1")) # dropout rate -> to be tuned
model.add(layers.Dense(NUM_CLASSES, activation='softmax',name="dense_2"))
return model

def fit_with(input_shape, verbose, dropout1_rate, dense_1_neurons_x128, lr):

Create the model using a specified hyperparameter.

dense_1_neurons = max(int(dense_1_neurons_x256 * 256), 256)
model = get_model(input_shape, dropout1_rate, dense_1_neurons)

Train the model for a specified number of epochs.

opt = tf.keras.optimizers.SGD(learning_rate=lr) 

model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=opt,
              metrics=['accuracy'])

Train the model with the train dataset.

model.fit(x=X_train,y=Y_train, epochs=1, steps_per_epoch=468,
          batch_size=64, verbose=verbose)

Evaluate the model with the eval dataset.

score = model.evaluate(X_val,Y_val ,steps=10, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Return the accuracy.

return score[1]    

from functools import partial

verbose = 1
fit_with_partial = partial(fit_with, input_shape, verbose)

fit_with_partial(dropout1_rate=0.5, lr=0.001, dense_1_neurons_x256=1)

Optimization using BayesianOptimizaitio

from bayes_opt import BayesianOptimization

pbounds = {
'dropout1_rate': (0.1, 0.5), 
'lr': (1e-4, 1e-2), 
}

optimizer = BayesianOptimization(
f=fit_with_partial,
pbounds=pbounds,
verbose=2, 
random_state=1,)

optimizer.maximize(init_points=10, n_iter=10,)


for x, res in enumerate(optimizer.res):
print("Iteration {}: \n\t{}".format(x, res))

print(optimizer.max)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source