Defining forward propagation

Let's define the forward propagation involved in the RNN, which is mathematically given as follows:

The and are the biases of the hidden and output layers, respectively. For simplicity, we haven't added them to our equations in the previous sections. Forward propagation can be implemented as follows:

with tf.variable_scope("RNN") as scope:
h_t = init_state
y_hat = []

for t, x_t in enumerate(tf.split(inputs, seq_length, axis=0)):
if t > 0:
scope.reuse_variables()

#input to hidden layer weights
U = tf.get_variable("U", [vocab_size, hidden_size], initializer=initializer)

#hidden to hidden layer weights
W = tf.get_variable("W", [hidden_size, hidden_size], initializer=initializer)

#output to hidden layer weights
V = tf.get_variable("V", [hidden_size, vocab_size], initializer=initializer)

#bias for hidden layer
bh = tf.get_variable("bh", [hidden_size], initializer=initializer)

#bias for output layer
by = tf.get_variable("by", [vocab_size], initializer=initializer)

h_t = tf.tanh(tf.matmul(x_t, U) + tf.matmul(h_t, W) + bh)

y_hat_t = tf.matmul(h_t, V) + by

y_hat.append(y_hat_t)

Apply softmax on the output and get the probabilities:

output_softmax = tf.nn.softmax(y_hat[-1])
outputs = tf.concat(y_hat, axis=0)

Compute the cross-entropy loss:

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=targets, logits=outputs))

Store the final hidden state of the RNN in hprev. We use this final hidden state for making predictions:

hprev = h_t
..................Content has been hidden....................

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