How can I predict the following value of time series using Tensorflow predict method?
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
To predict the next value in a time series with TensorFlow, the model does not consume the whole historical series as one giant input each time. Instead, you usually train on sliding windows: a fixed number of past observations becomes the input, and the following value becomes the target.
Once the model is trained, predict simply runs inference on the most recent window. The important part is building the training windows correctly and preserving the same input shape at prediction time.
Turn the Series into Input Windows
Suppose you want to use the previous three values to predict the next one. You can build training examples like this:
Now each row in X is a past window, and each value in y is the next point the model should learn to predict.
Train a Small TensorFlow Model
A simple dense model is enough to demonstrate the pattern.
This is not a production forecasting model, but it shows the correct workflow clearly.
Use predict on the Latest Window
To predict the following value, pass the latest window with the same shape used during training.
The np.newaxis part matters because Keras expects a batch dimension. Even one example must be shaped like (1, window_size) for this model.
Keep Training Shape and Inference Shape Consistent
If you use an LSTM or GRU, the input will usually be three-dimensional, often (samples, timesteps, features). The same principle still applies: your prediction input must match the training shape exactly.
For a univariate series with one feature per step, that shape is often (1, window_size, 1) at inference time. Many “predict the next value” errors are just shape mismatches rather than forecasting logic errors.
predict Does Not Create the Forecasting Strategy
Another common misconception is that predict itself is the forecasting method. It is only the inference call. The forecasting strategy comes from:
- how you construct windows
- which features you include
- how far ahead you forecast
- how you scale the series
- what model architecture you train
predict is just the final step that runs the trained model on new input.
Common Pitfalls
- Feeding the raw series directly into
predictwithout matching the training window shape. - Forgetting the batch dimension at inference time.
- Training on scaled data and then predicting on unscaled data.
- Expecting one-step forecasting code to automatically handle multi-step forecasting.
- Assuming
predictitself understands time series without explicit window construction.
Summary
- Build time-series training data as sliding input windows and next-step targets.
- Train the TensorFlow model on those windows.
- Use
predicton the most recent window, not on the entire raw series arbitrarily. - Keep training and inference shapes consistent.
- In time-series forecasting, window construction and preprocessing matter at least as much as the call to
predict.
Related reading
- How can I print the intermediate variables in the loss function in TensorFlow and Keras?
- How can I print the Learning Rate at each epoch with Adam optimizer in Keras?
- How can I reduce the number of CPUs used by Tensorlfow/Keras?
- How can I remove or omit data using map method for tf.data.Dataset objects?
- How can I print all eli5.explain_weights results without ellipsis?
- How can i programmatically generate descriptors for an arbitrary data set?
- How can I run a loop with a tensor as its range? in tensorflow
- How can I run Tensorboard on a remote server?
System design interview problems on this topic
- Design an E-commerce Recommendation System: solution and walkthrough
- Design an LLM Inference Service: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough
- Design ChatGPT: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.