LSTM预测中以上一步预测结果作为输入特征的Python实现问题求助
Hey there! I totally get your dilemma—when training, you have the luxury of using actual past target values, but in real-world prediction, you can't access those, so you need to feed in your model's own prior predictions instead. Let's walk through how to make this work in Python.
First, Let's Clarify the Setup
Your current training process is totally valid: you're using t's 34 features plus t-1's 35 features (including the real t-1 target) to train the model to predict t's target. The problem comes at inference time, where you don't have the real t-1 target—so we need to replace that with the model's prediction for t-1.
Step 1: Keep Your Training Pipeline As-Is
First off, don't change how you train the model. Training with ground-truth historical data is the right approach because it gives your model the best possible signal to learn from. So keep using the real t-1 target values during training.
Step 2: Implement Recursive (Rolling) Prediction at Inference
This is where the magic happens. For each time step t where you need a prediction, you'll:
- Take the known t-time features (34 variables)
- Combine them with the t-1-time features—but replace the t-1 target with the prediction you made for t-1
- Feed this combined feature set into your trained LSTM to get the t-time prediction
- Save this t-time prediction to use as the "target" in the next step's input
Let's put this into code. I'll assume you're using Keras/TensorFlow, and that your input data is structured such that each sample has:
current_features: 34 features from time tprev_features: 35 features from time t-1 (with the last element being the target value)
Example Code
import numpy as np from tensorflow.keras.models import load_model # Load your pre-trained LSTM model model = load_model('your_trained_lstm.h5') # Let's say we have a test sequence with 100 time steps # We'll assume we have: # - known_current_features: shape (100, 34) → 34 features for each t from 1 to 100 # - initial_prev_features: shape (35,) → t=0's 35 features (including real target) # - known_prev_non_target_features: shape (100, 34) → t's 34 non-target features for t from 1 to 100 # Initialize variables to track predictions predictions = [] current_prev_features = initial_prev_features.copy() # Loop through each time step to make recursive predictions for t in range(len(known_current_features)): # Get current t's 34 features curr_feat = known_current_features[t] # Combine current features with previous features (with predicted target) input_features = np.concatenate([curr_feat, current_prev_features]) # Reshape for LSTM (match your model's expected input shape) # Example: if your model expects (samples, time_steps, features), adjust accordingly input_data = input_features.reshape(1, 1, -1) # Make prediction for t pred_t = model.predict(input_data, verbose=0)[0][0] predictions.append(pred_t) # Update previous features for the next time step (t+1) # Replace t-1's target with our prediction, and use t's non-target features as the new "previous" non-target set current_prev_features = np.concatenate([known_prev_non_target_features[t], [pred_t]]) # Convert predictions to a numpy array for easy use predictions = np.array(predictions)
Key Notes to Keep in Mind
- Input Shape Matching: Double-check that the reshaped input data exactly matches what your LSTM was trained on. If your model expects sequences longer than 1 time step, adjust the
reshapestep to maintain a sliding window of features. - Error Accumulation: Recursive prediction will accumulate errors over time—each prediction relies on the previous one, so small mistakes can compound. For this reason, it's extra important to have a well-trained model with strong generalization.
- Real-Time Adaptation: If you're doing real-time prediction (one step at a time), the logic stays the same—just keep track of the last prediction and the last set of non-target features to build your next input.
内容的提问来源于stack exchange,提问作者Virginie Gautier

