Defining BPTT

Now, we will perform the BPTT, with Adam as our optimizer. We will also perform gradient clipping to avoid the exploding gradients problem:

  1. Initialize the Adam optimizer:
minimizer = tf.train.AdamOptimizer()
  1. Compute the gradients of the loss with the Adam optimizer:
gradients = minimizer.compute_gradients(loss)
  1. Set the threshold for the gradient clipping:
threshold = tf.constant(5.0, name="grad_clipping")
  1. Clip the gradients that exceed the threshold and bring it to the range:
clipped_gradients = []
for grad, var in gradients:
clipped_grad = tf.clip_by_value(grad, -threshold, threshold)
clipped_gradients.append((clipped_grad, var))
  1. Update the gradients with the clipped gradients:
updated_gradients = minimizer.apply_gradients(clipped_gradients)
..................Content has been hidden....................

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