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 2 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? Continue reading...