0

KerasRegressor在 sklearn 管道中使用并用于GridSearchCV超参数调整。现在我想添加提前停止但是我找不到方法。我读了几篇较旧的帖子,但没有成功。我的代码如下,它可以正常工作,但似乎提前停止不起作用:

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)

详细看起来像这样:

[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
...

有人知道如何解决这个问题吗?

4

0 回答 0