In machine learning, a recurrent neural network (RNN or LSTM) is a class of neural networks that have successfully been applied to Natural Language Processing. In this tutorial, I will explain how to build an RNN model with LSTM or GRU cell to predict the prices of the New York Stock Exchange.

The implementation of the network has been made using TensorFlow High-level API like Dataset API to feed data into model and Estimators API to train and predict model. I will describe the following steps: dataset creation, RNN training and prediction.

Download Data


The dataset can be downloaded from Kaggle. Data found on Kaggle is a collection of CSV files. You don’t have to do any preprocessing. You can directly load the data into a Pandas DataFrame.Stock prices come in several different flavours. They are, Open, Close, High and Low.

Data scaling


Most common activation functions of the network’s neurons such as tanh or sigmoid are defined on the [-1, 1] or [0, 1] interval respectively. Scaling can be easily accomplished in Python using sklearn’s MinMaxScaler.

Target Data


We will give it a sequence of stock prices and ask it to predict the next day price using GRU cells. We put our sequence of stock prices 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.

RNN Time series input

Split Data

The dataset was split into training and test data. The training data contained 80% of the total dataset. The data was not shuffled but sequentially sliced.

Dataset Input Function


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 LSTM 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.

RNN Time series input

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.

In TensorFlow you can unroll cell 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.

you 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

The 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 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 TensorFlow Model using Estimetor API

Predictions 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.

Tensorflow stock prices prediction

Leave a Reply