WARNING:tensorflow:Early stopping conditioned on metric `val_loss` which is not available. Available metrics are: loss,recall

Question:

I have the error in the title when running the code below:

def create_model(learning_rate, dropout, l2_lambda):
    model = Sequential()
    model.add(Dense(128,input_dim=X_train.shape[1], activation='relu', kernel_regularizer=regularizers.l2(l2_lambda)))
    model.add(BatchNormalization())
    model.add(Dropout(dropout))
    model.add(Dense(64,  activation='relu', kernel_regularizer=regularizers.l2(l2_lambda)))
    model.add(Dropout(dropout))
    model.add(Dense(1, activation='sigmoid'))
    optimizer = Adam(learning_rate=learning_rate)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['Recall'])
    return model


early_stopping = EarlyStopping(monitor='val_loss', patience=10)


for i in range (2):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)

    
    steps = [('transformer', PowerTransformer(method='yeo-johnson')),
     ('scaler', RobustScaler()), 
     ('smote', SMOTE()),
     ('model', KerasClassifier(build_fn=create_model, verbose=2, callbacks=[early_stopping]))]


    # Create the pipeline object
    pipeline = Pipeline(steps)

    # Define the k-fold cross-validation
    cv = StratifiedKFold(n_splits=2)


    # Create the grid search object
    grid = GridSearchCV(pipeline, param_grid, cv=cv, return_train_score=True, error_score='raise', scoring='recall')

    # Fit the grid search to the data
    grid_result = grid.fit(X_train, y_train, model__validation_split=0.2)

    grbe=grid.best_estimator_.fit(X_train, y_train)

Any ideas on how to solve it? I have search for similar error but I cannot apply the solutions in my case

Asked By: xavi

||

Answers:

I think you need to add the validation data to the final fit call

grbe=grid.best_estimator_.fit(X_train, y_train, validation_data=(X_test, y_test))

otherwise, the metric val_loss can not be calculated, because there is no validation data.

Answered By: Znerual