Unable to compute gradients efficiently in TF 1.14

Viewed 75

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • TensorFlow version (use command below): 1.14
  • Python version: 3.7.3
  • GPU model: Titan X (Pascal)
  • CUDA Version: 10.1

Problem Statement

So, I am trying to find the gradients of outputs with respect to inputs but my decoder model is a GRU. First, I used the code below, which works but is quite slow since my input is a 56 dimension vector but the output is of (276,77) dimensional matrix:

        def get_gradients(i):
            return tf.gradients(self.decoder_output[:, i], self.decoder_input)[0]

        output_dim = self.decoder_output.shape[1].value
        J = [get_gradients(i) for i in tqdm_notebook(range(output_dim))] 

Then, I looked to change it to while loop but apparently it gave me this error which means I cant have a nested while loop in which I am calculating gradients.

INFO:tensorflow:Cannot use 'while_1/gradients/f_count' as input to 'while_1/gradients/f_count_1' because they are in different while loops.

while_1/gradients/f_count_1 while context: gru_3/while/while_context
while_1/gradients/f_count while context: while_1/while_context
  • Is there way around this? Isn't there any other way in tensorflow to compute gradients?
  • or if there is any other way to make the computation faster?
0 Answers
Related