time series
TensorFlow
prediction
machine learning
forecasting

How can I predict the following value of time series using Tensorflow predict method?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

python
1import numpy as np
2
3series = np.array([10, 11, 13, 12, 14, 15, 16], dtype=np.float32)
4window_size = 3
5
6X = []
7y = []
8for i in range(len(series) - window_size):
9    X.append(series[i:i + window_size])
10    y.append(series[i + window_size])
11
12X = np.array(X)
13y = np.array(y)
14
15print(X)
16print(y)

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.

python
1import numpy as np
2import tensorflow as tf
3
4series = np.array([10, 11, 13, 12, 14, 15, 16], dtype=np.float32)
5window_size = 3
6
7X = []
8y = []
9for i in range(len(series) - window_size):
10    X.append(series[i:i + window_size])
11    y.append(series[i + window_size])
12
13X = np.array(X)
14y = np.array(y)
15
16model = tf.keras.Sequential([
17    tf.keras.layers.Input(shape=(window_size,)),
18    tf.keras.layers.Dense(16, activation="relu"),
19    tf.keras.layers.Dense(1),
20])
21
22model.compile(optimizer="adam", loss="mse")
23model.fit(X, y, epochs=200, verbose=0)

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.

python
latest_window = series[-window_size:]
next_value = model.predict(latest_window[np.newaxis, :], verbose=0)
print(next_value[0, 0])

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 predict without 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 predict itself 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 predict on 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.

Course illustration
Course illustration

All Rights Reserved.