TimeSeries: Using Tensorflow for time series modelling and forecasting

From OnnoWiki
Jump to: navigation, search


This is a quick example on using TensorFlow to learn about a time series and perform forecasting. This can be used as an entry point for detecting anomalies.

Module import

import numpy as np
import tensorflow as tf
from tensorflow.contrib.timeseries.python.timeseries import NumpyReader
tf.logging.set_verbosity(tf.logging.WARN)
tf.__version__

1.4.0

Plots customisation

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
sns.set_context("notebook", font_scale=1.2, rc={"lines.linewidth": 1.5})

Create time series

In this section we create the time series that we are going to use for training, fitting and prediction.

steps = 200
x = np.array(range(steps))
noise = np.random.uniform(-0.25, 0.25, steps)
y = np.sin(np.pi * x / 10) + np.cos(np.pi * x / 10) + x / 200. + noise
plt.figure(figsize=(12,6))
plt.xlabel('time step')
plt.ylabel('f')
_ = plt.plot(x, y)

Example image Train network

data = {
    tf.contrib.timeseries.TrainEvalFeatures.TIMES: x,
    tf.contrib.timeseries.TrainEvalFeatures.VALUES: y
}

reader = NumpyReader(data) 

train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(
        reader, batch_size=20, window_size=30)

ar = tf.contrib.timeseries.ARRegressor(
        periodicities=20, input_window_size=20, output_window_size=10,
        num_features=1,
        loss=tf.contrib.timeseries.ARModel.NORMAL_LIKELIHOOD_LOSS) 
_ = ar.train(input_fn=train_input_fn, steps=600)

Evaluate network

evaluation_input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)
evaluation = ar.evaluate(input_fn=evaluation_input_fn, steps=10)

Predictions

(predictions,) = tuple(ar.predict(
        input_fn=tf.contrib.timeseries.predict_continuation_input_fn(
evaluation, steps=40)))

Plotting

plt.figure(figsize=(12,6))
x = data['times'].reshape(-1)
y = data['values'].reshape(-1)
plt.plot(x, data['values'].reshape(-1), ':', label='origin')

x = evaluation['times'].reshape(-1)
y = evaluation['mean'].reshape(-1)
s = np.sqrt(evaluation['covariance'].reshape(-1))
plt.plot(x, evaluation['mean'].reshape(-1), label='evaluation', color='green')
plt.fill_between(x, y-5*s,y+5*s, alpha=0.1, color='blue')
plt.fill_between(x, y-3*s,y+3*s, alpha=0.1, color='blue')

x = predictions['times'].reshape(-1)
y = predictions['mean'].reshape(-1)
s = np.sqrt(predictions['covariance'].reshape(-1))
plt.plot(x, y, label='prediction',color='orange')
plt.fill_between(x, y-5*s,y+5*s, alpha=0.1, color='orange')
plt.fill_between(x, y-3*s,y+3*s, alpha=0.1, color='orange')

plt.xlabel('time_step')
plt.ylabel('values')
_ = plt.legend()

Example image