这是我的问题:如何在我的代码中使用提前停止?我应该把它放在哪个部分?
callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10,mode="auto")]
我的代码:
numpy.random import seed
seed(1)
def create_model(optimizer='rmsprop'):
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse',optimizer = optimizer)
return model
clf = KerasRegressor(build_fn=create_model,epochs = 500,callbacks=[tf.keras.callbacks.EarlyStopping( patience=10)])
param_grid = {
'clf__optimizer' : ['adam','rmsprop'],
'clf__batch_size' : [500,45,77]
}
pipeline = Pipeline([
('clf',clf)
])
from sklearn.model_selection import TimeSeriesSplit, GridSearchCV
tscv = TimeSeriesSplit(n_splits=5)
grid = GridSearchCV(pipeline, cv=tscv,param_grid=param_grid,return_train_score=True,verbose=10,
scoring = 'neg_mean_squared_error')
grid.fit(Xtrain2,ytrain.values)
grid.cv_results_
我把回调放在'grid.fit'和'param_grid'中,但我出错了!!!