Keras LSTM predicted timeseries squashed and shifted
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When LSTM predictions appear "squashed" (compressed into a narrow range) and "shifted" (lagging behind the actual values), the root causes are almost always improper data scaling, the model learning to predict the previous timestep, or insufficient model complexity. The squashed output means the model is predicting near the mean of the target values, and the shift means the model has learned that the best low-loss strategy is to output the last seen input value. Both issues indicate the model is not learning meaningful temporal patterns.
The Symptom
The predictions track the general trend but are always one or more steps behind, and the peaks and valleys are much smaller than the actual data.
Cause 1: Model Predicting the Previous Value
The easiest way for an LSTM to minimize loss on a time series is to output the previous input value. This produces a prediction that looks like the actual data shifted by one timestep:
Fix: Use a longer sequence length so the model has enough context to learn patterns, not just copy:
Cause 2: Improper Scaling
LSTMs are sensitive to input scale. Without normalization, the model struggles to learn and defaults to predicting near the mean:
A common mistake is fitting the scaler on the entire dataset (including test data), which leaks future information:
Cause 3: Insufficient Model Complexity
A single LSTM layer with few units may not have enough capacity:
Cause 4: Wrong Loss Function or Metric
Using MSE loss causes the model to predict the mean when it cannot learn the pattern:
Cause 5: Incorrect Inverse Transform
Forgetting to reverse the scaling makes predictions look squashed:
Correct End-to-End Example
Diagnosing the Problem
Common Pitfalls
- Data leakage through scaling: Fitting the scaler on the entire dataset (train + test) leaks information about the test set's distribution. Always fit the scaler on training data only and use
transform()on test data. - Sequence length too short: With
seq_length=1, the LSTM has no temporal context and simply copies the input. Use at least 20-60 timesteps depending on the data's periodicity. - Forgetting inverse transform: Predictions in the scaled range (0 to 1) look squashed when plotted against original data. Always call
scaler.inverse_transform()before comparing or plotting. - Not using
return_sequences=Truefor stacked LSTMs: When stacking multiple LSTM layers, all layers except the last must usereturn_sequences=Trueto pass the full sequence to the next layer. Without it, only the last timestep is passed. - Evaluating on training data: If predictions look good on training data but squashed on test data, the model is overfitting. Use early stopping, dropout, and a separate validation set to detect overfitting.
Summary
- Squashed predictions mean the model predicts near the mean — usually caused by bad scaling or insufficient complexity
- Shifted predictions mean the model copies the previous value — increase sequence length and model capacity
- Always scale data with
MinMaxScalerorStandardScaler, fitting only on training data - Use stacked LSTM layers with dropout for better temporal pattern learning
- Always inverse transform predictions before comparing with actual values
Related reading
- Keras LSTM Time Series
- Keras Making a neural network to find a number's modulus
- Keras Maxpooling2d layer gives ValueError
- Keras misinterprets training data shape
- Keras Making a neural network to find a number's modulus
- Keras Masking and Flattening
- Keras ML library how to do weight clipping after gradient updates? TensorFlow backend
- Keras Model Accuracy differs after loading the same saved model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.