Implement early stopping to enhance model accuracy and prevent overfitting

- Add EarlyStopping callback to LSTM and GRU model training
- Monitor validation loss with patience of 10 epochs
- Automatically stop training when performance plateaus
- Restore best weights found during training
This commit is contained in:
tcsenpai 2024-09-11 00:48:56 +02:00
parent 7bcc3556ad
commit 65ce172f3f

View File

@ -139,7 +139,9 @@ def walk_forward_validation(X, y, model, n_splits=5):
model.fit(X_train_2d, y_train) model.fit(X_train_2d, y_train)
predictions = model.predict(X_test_2d) predictions = model.predict(X_test_2d)
elif isinstance(model, Sequential): elif isinstance(model, Sequential):
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0) early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0,
validation_split=0.2, callbacks=[early_stopping])
predictions = model.predict(X_test).flatten() predictions = model.predict(X_test).flatten()
all_predictions.extend(predictions) all_predictions.extend(predictions)