title: In TensorFlow 2.0 with eager-execution, how to compute the gradients of a network output wrt a specific layer?
body: <p>I have a network made with InceptionNet, and for an input sample <code>bx</code>, I want to compute the gradients of the model output w.r.t. the hidden layer. I have the following code: </p>
  
  <pre class="lang-py prettyprint-override"><code>bx = tf.reshape(x_batch[0, :, :, :], (1, 299, 299, 3))
  
  
  with tf.GradientTape() as gtape:
      #gtape.watch(x)
      preds = model(bx)
      print(preds.shape, end='  ')
  
      class_idx = np.argmax(preds[0])
      print(class_idx, end='   ')
  
      class_output = model.output[:, class_idx]
      print(class_output, end='   ')
  
      last_conv_layer = model.get_layer('inception_v3').get_layer('mixed10')
      #gtape.watch(last_conv_layer)
      print(last_conv_layer)
  
  
  grads = gtape.gradient(class_output, last_conv_layer.output)#[0]
  print(grads)
  
  </code></pre>
  
  <p>But, this will give <code>None</code>. I tried <code>gtape.watch(bx)</code> as well, but it still gives <code>None</code>.</p>
  
  <p>Before trying GradientTape, I tried using <code>tf.keras.backend.gradient</code> but that gave an error as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
  </code></pre>
  
  <p>My model is as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>model.summary()
  
  Model: "sequential_4"
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  inception_v3 (Model)         (None, 1000)              23851784  
  _________________________________________________________________
  dense_5 (Dense)              (None, 2)                 2002      
  =================================================================
  Total params: 23,853,786
  Trainable params: 23,819,354
  Non-trainable params: 34,432
  _________________________________________________________________
  </code></pre>
  
  <p>Any solution is appreciated. It doesn't have to be GradientTape, if there is any other way to compute these gradients.</p>