What does it mean when validation loss increases over several epochs?

What does it mean when validation loss increases over several epochs?

Manage alerts

Loading saved threads...

joesan · External communityPost link
External question — Data Science Stack Exchange Author: joesan Original post: https://datascience.stackexchange.com/questions/134201 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm training an LSTM model to predict a stock price. This is what I do with my model training: def build_and_train_lstm_model(X_train, y_train, X_validate, y_validate, num_layers=4, units=100, dropout_rate=0.2, epochs=200, batch_size=64, model_name="lstm_google_price_predict_model.keras"): """ Builds and trains an LSTM model for time series prediction. Parameters: - X_train, y_train: Training data - X_validate, y_validate: Validation data - num_layers: Number of LSTM layers - units: Number of LSTM units per layer - dropout_rate: Dropout rate for regularization - epochs: Training epochs - batch_size: Batch size - model_name: Name of the model file (stored in _local_config.models_dir) Returns: - history: Training history object """ global _local_config if _local_config is None: raise RuntimeError("Config not loaded yet! Call load_google first.") # Try to get model_location from _local_config if available if hasattr(_local_config, 'models_dir'): print(f"Model will be saved to ${_local_config.models_dir}") else: raise ValueError("Model location not provided and not found in configg (_local_config)") # Ensure the model directory exists model_dir = Path(_local_config.models_dir) model_dir.mkdir(parents=True, exist_ok=True) model_path = model_dir / model_name # Initialize model regressor = Sequential() regressor.add(Input(shape=(X_train.shape[1], X_train.shape[2]))) # Add LSTM + Dropout layers for i in range(num_layers): return_seq = i < (num_layers - 1) regressor.add(LSTM(units=units, return_sequences=return_seq)) regressor.add(Dropout(rate=dropout_rate)) # Add output layer regressor.add(Dense(units=1)) # Compile model regressor.compile(optimizer="adam", loss="mean_squared_error") # Create checkpoint checkpoint_callback = ModelCheckpoint( filepath=str(model_path), monitor="val_loss", save_best_only=True, mode="min", verbose=0 ) # Train the model history = regressor.fit( x=X_train, y=y_train, validation_data=(X_validate, y_validate), epochs=epochs, batch_size=batch_size, callbacks=[checkpoint_callback] ) return history When I ran my training and then plot the loss function from my training and validation dataset, here is what I see: I do not understand two things: How can it be that the training loss is pretty consistent? Why is my validation loss increasing over the epochs? I would kindly request for help and suggestions on how I can improve my model? Here is my run where the first column is using this: early_stop_callback = EarlyStopping( monitor="val_loss", patience=config.patience, restore_best_weights=True, verbose=1 ) EDIT: Here is the plot of the dataset that I'm analyzing:
Quote
Report
Robert Long · External communityPost link
External answer — Data Science Stack Exchange Author: Robert Long Original post: https://datascience.stackexchange.com/a/134204 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. As mentioned by @AdamJ in a comment it seems that the model in the OP is suffering from overfitting . In what follows, we address the issue of overfitting in an LSTM model used for stock price prediction. Overfitting occurs when the model memorises the training data, including noise, leading to poor performance on unseen data. We will diagnose the symptoms, provide strategies for improvement, and offer a revised training function. Diagnosing Overfitting Symptoms The primary symptoms of overfitting here are: Flat Training Loss: The training loss stabilises at a low value, indicating that the model has memorised the training data. Increasing Validation Loss: The validation loss initially decreases but then starts to increase, indicating poor generalisation to unseen data. Causes Several factors contribute to overfitting: Excessive Model Capacity: A model with too many layers and units can memorise the training data, including noise. Insufficient Regularisation: Lack of regularisation techniques allows the model to fit noise in the training data. Suboptimal Training Dynamics: Improper training protocols, such as a high learning rate or lack of early stopping, can lead to overfitting. Strategies for Improvement Simplify Model Architecture To reduce the model's capacity to memorise noise, we can simplify the architecture: Reduce Layers: Decrease the number of LSTM layers from four to one or two. Fewer Units: Reduce the number of units per layer from 100 to 32-64. Enhance Regularisation Regularisation techniques help prevent the model from overfitting by penalising complexity: Dropout: Increase the dropout rate from 0.2 to 0.3-0.4 to prevent neuron co-adaptation. Recurrent Dropout: Apply dropout to the recurrent connections within the LSTM layers. L2 Regularisation: Add L2 weight regularisation to penalise large weights, which can be a sign of overfitting. Optimise Training Dynamics Fine-tuning the training process can lead to more stable convergence and better generalisation: Early Stopping: Use early stopping to halt training when the validation loss stops improving. Learning Rate Scheduling: Implement learning rate scheduling to reduce the learning rate when the validation loss plateaus. Gradient Clipping: Use gradient clipping to prevent exploding gradients, which can destabilise training. Data Preprocessing Proper preprocessing of financial time series data is crucial for model performance: Stationarity Check: Test for stationarity using the Augmented Dickey-Fuller (ADF) test. If the data is non-stationary, apply differencing: $$ \nabla X_t = X_t - X_{t-1} $$ Normalisation: Normalise the input features and target variable to reduce noise sensitivity. Chronological Splits: Ensure strict chronological splits for training and validation sets to avoid lookahead bias. Revised Training Function We provide a revised training function that incorporates the best practices discussed above. The function includes simplified architecture, enhanced regularisation, and optimised training dynamics. See Appendix 1 for Code. Model Evaluation To assess the performance of the improved model, we can use various evaluation metrics and techniques: Cross-Validation: Use time-series cross-validation to validate model robustness. Backtesting: Apply the model to historical data to evaluate its performance in a simulated trading environment. Performance Metrics: Use metrics such as Mean Absolute Error (MAE) and Root Mean Squared Error (RMSE) to quantify model performance. Conclusion By simplifying the model architecture, enhancing regularisation, optimising training dynamics, and properly preprocessing the data, we can improve the generalisation of an LSTM model for stock price prediction. The revised training function and evaluation techniques provided in this guide should help achieve better performance on unseen data. References Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning . MIT Press. MIT Press Hochreiter, S., & Schmidhuber, J. (1997). Long Short-Term Memory. Neural Computation, 9 (8), 1735-1780. DOI:10.1162/neco.1997.9.8.1735 Kingma, D. P., & Ba, J. (2015). Adam: A Method for Stochastic Optimization. arXiv:1412.6980 . arXiv Pascanu, R., Mikolov, T., & Bengio, Y. (2013). On the Difficulty of Training Recurrent Neural Networks. International Conference on Machine Learning (ICML) . arXiv Prechelt, L. (1998). Early Stopping—But When? In Neural Networks: Tricks of the Trade (pp. 55-69). Springer. Springer Srivastava, N., Hinton, G., Krizhevsky, A., Sutskever, I., & Salakhutdinov, R. (2014). Dropout: A Simple Way to Prevent Neural Networks from Overfitting. Journal of Machine Learning Research, 15 (1), 1929-1958. JMLR Appendix 1: Revised Training Function from pathlib import Path from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Dropout, Input from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau from tensorflow.keras.optimizers import Adam from tensorflow.keras.regularizers import l2 def build_and_train_improved_lstm(X_train, y_train, X_validate, y_validate, num_layers=2, units=50, dropout_rate=0.3, l2_lambda=0.001, learning_rate=0.001, epochs=100, batch_size=64, model_name="lstm_improved_model.keras"): """ Builds and trains an improved, regularised LSTM model. """ global _local_config if _local_config is None: raise RuntimeError("Config not loaded yet!") model_dir = Path(_local_config.models_dir) model_dir.mkdir(parents=True, exist_ok=True) model_path = model_dir / model_name # Initialize Model regressor = Sequential() regressor.add(Input(shape=(X_train.shape[1], X_train.shape[2]))) # Add Simplified and Regularised LSTM Layers for i in range(num_layers): return_seq = i < (num_layers - 1) regressor.add(LSTM(units=units, return_sequences=return_seq, recurrent_dropout=dropout_rate, kernel_regularizer=l2(l2_lambda))) regressor.add(Dropout(rate=dropout_rate)) # Add Output Layer regressor.add(Dense(units=1)) # Compile Model with Gradient Clipping optimizer = Adam(learning_rate=learning_rate, clipnorm=1.0) regressor.compile(optimizer=optimizer, loss="mean_squared_error") # Define a More Robust Set of Callbacks checkpoint_callback = ModelCheckpoint( filepath=str(model_path), monitor="val_loss", save_best_only=True, mode="min" ) early_stop_callback = EarlyStopping( monitor="val_loss", patience=15, restore_best_weights=True, verbose=1 ) lr_scheduler_callback = ReduceLROnPlateau( monitor="val_loss", factor=0.2, patience=7, min_lr=1e-6, verbose=1 ) # Train the Model history = regressor.fit( x=X_train, y=y_train, validation_data=(X_validate, y_validate), epochs=epochs, batch_size=batch_size, callbacks=[checkpoint_callback, early_stop_callback, lr_scheduler_callback] ) return history
Quote
Report

Post Reply

Checking account access…