0

I used KerasRegressor within a sklearn pipeline and used GridSearchCV for hyperparam tuning. Now I want to add early stopping however I couldn't find a way. I read few older posts and no success. My code is the following which works without error but it seems early stopping doesn't work:

X_train, X_test, y_train, y_test = train_test_split()

def create_model(optimizer='adam',
                 kernel_initializer='glorot_uniform', 
                 dropout=0.2):
    model = Sequential()
    model.add(Dense(64, activation='relu', kernel_initializer=kernel_initializer))
    model.add(Dropout(dropout))
    model.add(Dense(1, kernel_initializer=kernel_initializer))

    model.compile(loss='mse',optimizer=optimizer)

    return model


clf = KerasRegressor(build_fn=create_model,verbose=0)
scaler = StandardScaler()

pipeline = Pipeline([
    ('preprocess',scaler),
    ('clf',clf)
])

callback = tf.keras.callbacks.EarlyStopping(monitor="loss", patience=1, restore_best_weights=True)

param_grid = {
    'clf__epochs':[4,8],
    'clf__dropout':[0.1,0.2],
    'clf__kernel_initializer':['glorot_uniform','normal','uniform'],
    'clf__callbacks':[callback],

}

grid = GridSearchCV(pipeline, cv=5, param_grid=param_grid)
grid.fit(X_train, y_train)

The verbose looks like this:

[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
Epoch 1/100
WARNING:tensorflow:Early stopping conditioned on metric `val_loss` which is not available. Available metrics are: loss
Epoch 2/100
WARNING:tensorflow:Early stopping conditioned on metric `val_loss` which is not available. Available metrics are: loss
Epoch 3/100
...

Does anybody know how to fix this?

mas
  • 339
  • 7
  • 22
  • 2
    Does this answer your question? [Early stopping with Keras and sklearn GridSearchCV cross-validation](https://stackoverflow.com/questions/48127550/early-stopping-with-keras-and-sklearn-gridsearchcv-cross-validation) – felice Feb 04 '21 at 08:58
  • thanks @felice! Makes total sense and I realized that I already tune epochs in my CV. – mas Feb 04 '21 at 23:00

0 Answers0