WWW Tensorflow Org Tutorials Structured Data Time Series
WWW Tensorflow Org Tutorials Structured Data Time Series
WWW Tensorflow Org Tutorials Structured Data Time Series
TensorFlow Core
TensorFlow World is now underway. Join us for the livestream now. Watch now
This tutorial is an introduction to time series forecasting using Recurrent Neural Networks (RNNs). This is covered in two
parts: first, you will forecast a univariate time series, then you will forecast a multivariate time series.
mpl.rcParams['figure.figsize'] = (8, 6)
mpl.rcParams['axes.grid'] = False
This tutorial uses a weather time series dataset recorded by the Max-Planck-Institute for Biogeochemistry.
This dataset contains 14 different features such as air temperature, atmospheric pressure, and humidity. These were
collected every 10 minutes, beginning in 2003. For efficiency, you will use only the data collected between 2009 and 2016.
This section of the dataset was prepared by François Chollet for his book Deep Learning with Python.
zip_path = tf.keras.utils.get_file(
origin='https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip'
fname='jena_climate_2009_2016.csv.zip',
extract=True)
csv_path, _ = os.path.splitext(zip_path)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_201
13574144/13568290 [==============================] - 0s 0us/step
df = pd.read_csv(csv_path)
df.head()
As you can see above, an observation is recorded every 10 mintues. This means that, for a single hour, you will have 6
observations. Similarly, a single day will contain 144 (6x24) observations.
Given a specific time, let's say you want to predict the temperature 6 hours in the future. In order to make this prediction,
you choose to use 5 days of observations. Thus, you would create a window containing the last 720(5x144) observations to
train the model. Many such configurations are possible, making this dataset a good one to experiment with.
The function below returns the above described windows of time for the model to train on. The parameter history_size is
the size of the past window of information. The target_size is how far in the future does the model need to learn to
predict. The target_size is the label that needs to be predicted.
TRAIN_SPLIT = 300000
tf.random.set_seed(13)
First, you will train a model using only a single feature (temperature), and use it to make predictions for that value in the
future.
Date Time
01.01.2009 00:10:00 -8.02
01.01.2009 00:20:00 -8.41
01.01.2009 00:30:00 -8.51
01.01.2009 00:40:00 -8.31
01.01.2009 00:50:00 -8.27
Name: T (degC), dtype: float64
uni_data.plot(subplots=True)
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7fc3abf384e0>],
dtype=object)
uni_data = uni_data.values
It is important to normalize features before training a neural network. A common way to do so is by subtracting the mean
and dividing by the standard deviation of each feature.
Note: The mean and standard deviation should only be computed using the training data.
uni_train_mean = uni_data[:TRAIN_SPLIT].mean()
uni_train_std = uni_data[:TRAIN_SPLIT].std()
uni_data = (uni_data-uni_train_mean)/uni_train_std
Let's now create the data for the univariate model. For part 1, the model will be given the last 20 recorded temperature
observations, and needs to learn to predict the temperature at the next time step.
univariate_past_history = 20
univariate_future_target = 0
Now that the data has been created, let's take a look at a single example. The information given to the network is given in
blue, and it must predict the value at the red cross.
def create_time_steps(length):
time_steps = []
for i in range(-length, 0, 1):
time_steps.append(i)
return time_steps
plt.title(title)
for i, x in enumerate(plot_data):
if i:
plt.plot(future, plot_data[i], marker[i], markersize=10,
label=labels[i])
else:
plt.plot(time_steps, plot_data[i].flatten(), marker[i], label=labels[i])
plt.legend()
plt.xlim([time_steps[0], (future+5)*2])
plt.xlabel('Time-Step')
return plt
Before proceeding to train a model, let's first set a simple baseline. Given an input point, the baseline method looks at all the
history and predicts the next point to be the average of the last 20 observations.
def baseline(history):
return np.mean(history)
show_plot([x_train_uni[0], y_train_uni[0], baseline(x_train_uni[0])], 0,
'Baseline Prediction Example')
Let's see if you can beat this baseline using a recurrent neural network.
Recurrent neural network
A Recurrent Neural Network (RNN) is a type of neural network well-suited to time series data. RNNs process a time series
step-by-step, maintaining an internal state summarizing the information they've seen so far. For more details, read the RNN
tutorial. In this tutorial, you will use a specialized RNN layer called Long Short Term Memory (LSTM)
Let's now use tf.data to shuffle, batch, and cache the dataset.
BATCH_SIZE = 256
BUFFER_SIZE = 10000
The following visualisation should help you understand how the data is represented after batching.
You will see the LSTM requires the input shape of the data it is being given.
simple_lstm_model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(8, input_shape=x_train_uni.shape[-2:]),
tf.keras.layers.Dense(1)
])
simple_lstm_model.compile(optimizer='adam', loss='mae')
for x, y in val_univariate.take(1):
print(simple_lstm_model.predict(x).shape)
(256, 1)
Let's train the model now. Due to the large size of the dataset, in the interest of saving time, each epoch will only run for 200
steps, instead of the complete training data as normally done.
EVALUATION_INTERVAL = 200
EPOCHS = 10
simple_lstm_model.fit(train_univariate, epochs=EPOCHS,
steps_per_epoch=EVALUATION_INTERVAL,
validation_data=val_univariate, validation_steps=50)
Now that you have trained your simple LSTM, let's try and make a few predictions.
for x, y in val_univariate.take(3):
plot = show_plot([x[0].numpy(), y[0].numpy(),
simple_lstm_model.predict(x)[0]], 0, 'Simple LSTM model')
plot.show()
This looks better than the baseline. Now that you have seen the basics, let's move on to part two, where you will work with a
multivariate time series.
Pa 2: Forecast a multivariate time series
The original dataset contains fourteen features. For simplicity, this section considers only three of the original fourteen. The
features used are air temperature, atmospheric pressure, and air density.
features = df[features_considered]
features.index = df['Date Time']
features.head()
Let's have a look at how each of these features vary across time.
features.plot(subplots=True)
dataset = features.values
data_mean = dataset[:TRAIN_SPLIT].mean(axis=0)
data_std = dataset[:TRAIN_SPLIT].std(axis=0)
dataset = (dataset-data_mean)/data_std
Single step model
In a single step setup, the model learns to predict a single point in the future based on some history provided.
The below function performs the same windowing task as below, however, here it samples the past observation based on the
step size given.
if single_step:
labels.append(target[i+target_size])
else:
labels.append(target[i:i+target_size])
return np.array(data), np.array(labels)
In this tutorial, the network is shown data from the last five (5) days, i.e. 720 observations that are sampled every hour. The
sampling is done every one hour since a drastic change is not expected within 60 minutes. Thus, 120 observation represent
history of the last five days. For the single step prediction model, the label for a datapoint is the temperature 12 hours into
the future. In order to create a label for this, the temperature after 72(12*6) observations is used.
past_history = 720
future_target = 72
STEP = 6
single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(32,
input_shape=x_train_single.shape[-2:]))
single_step_model.add(tf.keras.layers.Dense(1))
single_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss='mae')
Let's check out a sample prediction.
for x, y in val_data_single.take(1):
print(single_step_model.predict(x).shape)
(256, 1)
epochs = range(len(loss))
plt.figure()
plot_train_history(single_step_history,
'Single Step Training and validation loss')
for x, y in val_data_single.take(3):
plot = show_plot([x[0][:, 1].numpy(), y[0].numpy(),
single_step_model.predict(x)[0]], 12,
'Single Step Prediction')
plot.show()
Multi-Step model
In a multi-step prediction model, given a past history, the model needs to learn to predict a range of future values. Thus,
unlike a single step model, where only a single future point is predicted, a multi-step model predict a sequence of the future.
For the multi-step model, the training data again consists of recordings over the past five days sampled every hour.
However, here, the model needs to learn to predict the temperature for the next 12 hours. Since an obversation is taken
every 10 minutes, the output is 72 predictions. For this task, the dataset needs to be prepared accordingly, thus the first
step is just to create it again, but with a different target window.
future_target = 72
x_train_multi, y_train_multi = multivariate_data(dataset, dataset[:, 1], 0,
TRAIN_SPLIT, past_history,
future_target, STEP)
x_val_multi, y_val_multi = multivariate_data(dataset, dataset[:, 1],
TRAIN_SPLIT, None, past_history,
future_target, STEP)
In this plot and subsequent similar plots, the history and the future data are sampled every hour.
for x, y in train_data_multi.take(1):
multi_step_plot(x[0], y[0], np.array([0]))
Since the task here is a bit more complicated than the previous task, the model now consists of two LSTM layers. Finally,
since 72 predictions are made, the dense layer outputs 72 predictions.
multi_step_model = tf.keras.models.Sequential()
multi_step_model.add(tf.keras.layers.LSTM(32,
return_sequences=True,
input_shape=x_train_multi.shape[-2:]))
multi_step_model.add(tf.keras.layers.LSTM(16, activation='relu'))
multi_step_model.add(tf.keras.layers.Dense(72))
multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')
for x, y in val_data_multi.take(1):
print (multi_step_model.predict(x).shape)
(256, 72)
Let's now have a look at how well your network has learnt to predict the future.
for x, y in val_data_multi.take(3):
multi_step_plot(x[0], y[0], multi_step_model.predict(x)[0])
Next steps
This tutorial was a quick introduction to time series forecasting using an RNN. You may now try to predict the stock market
and become a billionaire.
In addition, you may also write a generator to yield data (instead of the uni/multivariate_data function), which would be more
memory efficient. You may also check out this time series windowing guide and use it in this tutorial.
For further understanding, you may read Chapter 15 of Hands-on Machine Learning with Scikit-Learn, Keras, and
TensorFlow, 2nd Edition and Chapter 6 of Deep Learning with Python.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed
under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Terms | Privacy Sign up for the TensorFlow monthly newsletter Subscribe Language