title: Tensorflow GradientTape Returns None
body: <p>I'm attempting to use Tensorflow's <code>GradientTape</code> method to create a Grad-CAM activation heatmap for my CNN <code>vgg19</code>. I'm attempting to follow along with this <a href="https://medium.com/analytics-vidhya/visualizing-activation-heatmaps-using-tensorflow-5bdba018f759" rel="nofollow noreferrer">blog post</a>, but I'm running into lots of issues. My gradient returns <code>None</code>, no matter what I try.</p>
  <p>Code snippet below:</p>
  <pre><code>x = tensorflow.constant(train_generator[0][0][0][None,:])
  with tensorflow.GradientTape() as tape:
  
    tape.watch(x)
    last_conv_layer = vgg19.get_layer('block5_conv4')
    iterate = tensorflow.keras.models.Model([vgg19.inputs], [vgg19.output, last_conv_layer.output])
    model_out, last_conv_layer = iterate(x)
    class_out = model_out[:, np.argmax(model_out[0])]
    grads = tape.gradient(class_out, last_conv_layer)
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
    
  heatmap = tensorflow.reduce_mean(tf.multiply(pooled_grads, last_conv_layer), axis=-1)
  </code></pre>
  <p>In the above, <code>grads</code> returns as <code>None</code>, which causes this code to break. <code>train_generator</code> function is created using TF's <code>DataImageGenerator</code> method with <code>flow_from_directory</code> method.</p>
  <p>I've tried to use <code>tape.watch</code> methods, and have tried the fixes suggested in <a href="https://www.tensorflow.org/guide/autodiff#getting_a_gradient_of_none" rel="nofollow noreferrer">the documentation</a> but haven't had any success. Any idea what might be going on here?</p>
  <p>Thanks in advance!</p>