Building an LSTM cell

In this section, we will write a function for creating the LSTM cell, which will be used in the hidden layer. This cell will be the building block for our model. So, we will create this cell using TensorFlow. Let's have a look at how we can use TensorFlow to build a basic LSTM cell.

We call the following line of code to create an LSTM cell with the parameter num_units representing the number of units in the hidden layer:

lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_units)

To prevent overfitting, we can use something called dropout, which is a mechanism for preventing the model from overfitting the data by decreasing the model's complexity:

tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_probability)

As we mentioned before, we will be using the stacked LSTM architecture; it will help us to look at the data from different angles and has been practically found to perform better. In order to define a stacked LSTM in TensorFlow, we can use the tf.contrib.rnn.MultiRNNCell function (link: https://www.tensorflow.org/versions/r1.0/api_docs/python/tf/contrib/rnn/MultiRNNCell):

tf.contrib.rnn.MultiRNNCell([cell]*num_layers)

Initially for the first cell, there will be no previous information, so we need to initialize the cell state to be zeros. We can use the following function to do that:

initial_state = cell.zero_state(batch_size, tf.float32)

So, let's put it all together and create our LSTM cell:

def build_lstm_cell(size, num_layers, batch_size, keep_probability):

### Building the LSTM Cell using the tensorflow function
lstm_cell = tf.contrib.rnn.BasicLSTMCell(size)

# Adding dropout to the layer to prevent overfitting
drop_layer = tf.contrib.rnn.DropoutWrapper(lstm_cell, output_keep_prob=keep_probability)

# Add muliple cells together and stack them up to oprovide a level of more understanding
stakced_cell = tf.contrib.rnn.MultiRNNCell([drop_layer] * num_layers)
initial_cell_state = lstm_cell.zero_state(batch_size, tf.float32)

return lstm_cell, initial_cell_state
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset