Building the network

Now, we can put all the pieces together and build a class for the network. To actually run data through the LSTM cells, we will use tf.nn.dynamic_rnn (link: https://www.tensorflow.org/versions/r1.0/api_docs/python/tf/nn/dynamic_rnn). This function will pass the hidden and cell states across LSTM cells appropriately for us. It returns the outputs for each LSTM cell at each step for each sequence in the mini-batch. It also gives us the final LSTM state. We want to save this state as final_state, so we can pass it to the first LSTM cell in the the next mini-batch run. For tf.nn.dynamic_rnn, we pass in the cell and initial state we get from build_lstm, as well as our input sequences. Also, we need to one-hot encode the inputs before going into the RNN:

class CharLSTM:

def __init__(self, num_classes, batch_size=64, num_steps=50,
lstm_size=128, num_layers=2, learning_rate=0.001,
grad_clip=5, sampling=False):

# When we're using this network for generating text by sampling, we'll be providing the network with
# one character at a time, so providing an option for it.
if sampling == True:
batch_size, num_steps = 1, 1
else:
batch_size, num_steps = batch_size, num_steps

tf.reset_default_graph()

# Build the model inputs placeholders of the input and target variables
self.inputs, self.targets, self.keep_prob = build_model_inputs(batch_size, num_steps)

# Building the LSTM cell
lstm_cell, self.initial_state = build_lstm_cell(lstm_size, num_layers, batch_size, self.keep_prob)

### Run the data through the LSTM layers
# one_hot encode the input
input_x_one_hot = tf.one_hot(self.inputs, num_classes)

# Runing each sequence step through the LSTM architecture and finally collecting the outputs
outputs, state = tf.nn.dynamic_rnn(lstm_cell, input_x_one_hot, initial_state=self.initial_state)
self.final_state = state

# Get softmax predictions and logits
self.prediction, self.logits = build_model_output(outputs, lstm_size, num_classes)

# Loss and optimizer (with gradient clipping)
self.loss = model_loss(self.logits, self.targets, lstm_size, num_classes)
self.optimizer = build_model_optimizer(self.loss, learning_rate, grad_clip)
..................Content has been hidden....................

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