'How to use GridSearchCV for learning rate optimisation (Keras LSTM)

I am tuning Keras Sequential model and have the capability of doing epochs, batch_size, but I'm unsure how to test multiple learning rates, different learners, and different LSTM units. Below is my code:

def build_model():
model = Sequential()

model.add(LSTM(50, input_shape=(5,5),activation = "relu")) #return false
model.add(Dropout(0.2))
          
model.add(Dense(1,activation = "linear"))

model.compile(loss='mse',optimizer="Adam", metrics=["mse"])  
return model

Which needs the KerasRegressor to allow the cross validations

model = KerasRegressor(build_fn= build_model,batch_size = 256, verbose = 1)

Then defining the grid search as

param_grid = dict(epochs = [10,20,30])
grid = GridSearchCV(estimator = model, param_grid = param_grid, n_jobs = -1 , cv = 5)
grid_result = grid.fit(X_train,y_train)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

Note* I have only tested the epochs for a trial, but my main aim is to test learning rates in the param grid, aswell as other varying hyperparameters



Sources

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

Source: Stack Overflow

Solution Source