如何获取基于Keras的LSTM时间序列模型的“平均”时间序列?
Hey Carson, great question! Since you're still getting up to speed with ML, LSTMs, and time series modeling, and you've already built a Keras Sequential LSTM on your time series data, let's break down the best ways to get that "average" time sequence derived from your model and training samples.
First, a quick clarification: when you say "average" time sequence based on the model, we're not just talking about averaging your raw training data (though that's a useful baseline). We want a sequence that reflects the typical patterns your LSTM has learned from all training samples. Here are two practical, easy-to-implement approaches:
If your LSTM is set up for single-step or multi-step prediction (e.g., input a history window, predict future time steps), this method averages the model's predictions for every training sample at each time step. This gives you a sense of what the model "expects" on average for any given step.
Step-by-Step Implementation
- Generate predictions for every training sample using your trained model.
- Stack all these predictions into a 2D array (rows = samples, columns = time steps).
- Compute the mean across all samples for each time step.
- Optional: Compare this to the raw training data's time-step average to see how your model's learned patterns differ from the raw data.
Example Code
Assuming your training data is formatted as X_train (shape: (number_of_samples, time_steps, number_of_features)), and your trained model is stored as trained_lstm_model:
import numpy as np # 1. Generate predictions for all training samples all_predictions = [] for sample in X_train: # Reshape sample to match model input shape (add batch dimension) sample_input = sample.reshape(1, *sample.shape) # Get prediction for this sample (verbose=0 to avoid clutter) pred = trained_lstm_model.predict(sample_input, verbose=0) # Remove extra dimensions and store all_predictions.append(pred.squeeze()) # 2. Convert to numpy array and compute time-step average predictions_array = np.array(all_predictions) model_average_sequence = np.mean(predictions_array, axis=0) # 3. Optional: Compute raw training data's time-step average (baseline) raw_average_sequence = np.mean(X_train, axis=0).squeeze() # Print results print("Model-derived average time sequence:\n", model_average_sequence) print("\nRaw training data average time sequence:\n", raw_average_sequence)
If you want to dig deeper into what your model has learned (not just average predictions), you can use the average hidden state of your LSTM layer. LSTM hidden states capture the model's "memory" of patterns in the data—averaging these gives you a generic "memory" that you can use to generate a typical sequence.
Step-by-Step Implementation
- Create a helper model to extract the LSTM layer's hidden states.
- Collect hidden states from all training samples.
- Compute the average hidden state across all samples.
- Use this average state as the initial memory for your model to generate a new, average sequence.
Example Code
from tensorflow.keras.models import Model # 1. Create a model that outputs the LSTM layer's hidden states # Replace `0` with the index of your LSTM layer (check model.summary() if unsure) lstm_layer = trained_lstm_model.layers[0] hidden_state_extractor = Model(inputs=trained_lstm_model.input, outputs=lstm_layer.output) # 2. Collect hidden states for all training samples all_hidden_states = [] for sample in X_train: sample_input = sample.reshape(1, *sample.shape) h_state = hidden_state_extractor.predict(sample_input, verbose=0) all_hidden_states.append(h_state.squeeze()) # 3. Compute the average hidden state average_hidden_state = np.mean(np.array(all_hidden_states), axis=0) # 4. Generate a sequence using the average hidden state # Initialize with a starting window (we'll use the raw average's first time steps) time_steps = X_train.shape[1] current_input = raw_average_sequence[:time_steps].reshape(1, time_steps, 1) # Assumes 1 feature generated_average_sequence = [] # Number of steps to generate (adjust based on your needs) num_generate_steps = 20 for _ in range(num_generate_steps): # Predict next step using the average hidden state as initial memory # For LSTMs, initial_state takes [hidden_state, cell_state] pred = trained_lstm_model.predict( current_input, initial_state=[average_hidden_state, average_hidden_state], verbose=0 ) generated_average_sequence.append(pred[0][0]) # Update input window: shift left, add new prediction to the end current_input = np.roll(current_input, shift=-1, axis=1) current_input[0, -1, 0] = pred[0][0] print("\nGenerated average sequence from LSTM hidden states:\n", generated_average_sequence)
- If your model is for single-step prediction (only predicts one future step per input), you'll need to loop prediction for each sample to build a full sequence before averaging (similar to how we generated the sequence in Approach 2).
- Your attached sample data is 1-dimensional, so the code assumes 1 feature—if you have multiple features, just adjust the
number_of_featuresdimension in the reshape steps. - Always visualize these sequences (e.g., with Matplotlib) to see how they align with your raw data—this helps you validate if the model's learned patterns make sense.
内容的提问来源于stack exchange,提问作者Carson P

