In this tutorial, we will build a TensorFlow RNN model for Time Series Prediction. This model is used to predict future values based on previously observed values. This model will try to predict the next value in a short sequence based on historical data.

Time series are widely used for non-stationary data, like predict stock markets, temperatures, traffic or sales data based on past patterns. They can also be adapted to generate text.

Simulate Time Series Data


It’s easier to learn with a small toy dataset that you can generate as much as you want. Let’s generate a bunch of time-series data.

Dataset Input Function


We will give it a sequence of number and ask it to predict the next number in the sequence using GRU cells. We unroll it a  certain number of times. We put our sequence number on the inputs. It will produce some kind of number on the output. To teach it we force a sequence on the outputs which is the same sequence shifted by one number. This is what we will be teaching. Let’s write this in TensoFlow.

RNN Time series input
In this tutorial, we will use Dataset API to input data in model and Estimator API to train model. You need to provide an input_fn to read your data. You provide a function that returns inputs and labels. The inputs are a dictionary of all your inputs and the labels is a tensor.

Create RNN Model


There is a full API for working with RNN in TensorFlow. It starts with a GRU cell. This defines all the weights and biases. The only parameter you need to specify is this internal size of the vectors in this cell.

Softmax Layer

Deep NN

If you want to deep neural network. Let’s stack it, there is a function for that it’s called MultiRNNCell you pass it a cell and how many times you want this cell and it creates a new cell which is a stacked cell.

The last thing we need to do is to unroll it. In TensorFlow you do this using the dynamic RNN function you give it a stacked cell that you just produced. How many times do you unroll? That will depend on the shape of X. X here my input is a sequence of the number. If I feed it with sequences of 16 numbers my network will be unrolled 16 times. what I get as an output are results? It’s all the ages on the bottom.

I will need to apply an activation layer on those to obtain my result sequence and I get also the last state H which is the state that will need to pass back in the beginning as I continue my training.

Create the Estimator


Estimator is a TensorFlow class for performing high-level model training, evaluation, and inference for our model.

The model_fn argument specifies the model function to use for training, evaluation, and prediction; we pass it the model_rnn_fn. The model_dir argument specifies the directory where model data and checkpoints will be saved.

Train RNN Model

Now we’re ready to train our model, which we can do by calling train() on estimator.

train output

Predictions (inferring) from Trained Model

We now have a trained model. We can now use the trained model to predict time series data based on some unlabeled data. As with training, we make predictions using a single function call.

Time series predict model

Leave a Reply