title: GradientTape returns None
body: <p>I am trying to use grad-CAM (I'm following this <a href="https://www.pyimagesearch.com/2020/03/09/grad-cam-visualize-class-activation-maps-with-keras-tensorflow-and-deep-learning/" rel="nofollow noreferrer">https://www.pyimagesearch.com/2020/03/09/grad-cam-visualize-class-activation-maps-with-keras-tensorflow-and-deep-learning/</a> from PyImageSearch) on a CNN I'm using transfer learning on.</p>
  <p>In particular, I am using a simple CNN for a regression problem. I used MobileNetV2 with an Average Pooling layer and a Dense layer with one unit on top, as shown below:</p>
  <pre><code>base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3), weights='imagenet')
  base_model.trainable = False
  inputs = keras.Input(shape=(224, 224, 3))
  x = base_model(inputs)
  x = keras.layers.GlobalAveragePooling2D()(x)
  outputs = keras.layers.Dense(1, activation=&quot;linear&quot;)(x)
  model = keras.Model(inputs, outputs)
  </code></pre>
  <p>and the summary is:</p>
  <pre><code>Model: &quot;model&quot;
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  input_2 (InputLayer)         [(None, 224, 224, 3)]     0         
  _________________________________________________________________
  mobilenetv2_1.00_224 (Model) (None, 7, 7, 1280)        2257984   
  _________________________________________________________________
  global_average_pooling2d (Gl (None, 1280)              0         
  _________________________________________________________________
  dense (Dense)                (None, 1)                 1281      
  =================================================================
  Total params: 2,259,265
  Trainable params: 1,281
  Non-trainable params: 2,257,984
  _________________________________________________________________
  </code></pre>
  <p>I initialize the CAM object with:</p>
  <pre><code>pred = 0.35
  cam = GradCAM(model, pred, layerName='input_2')
  </code></pre>
  <p>where pred is the predicted output on which I want to inspect the CAM and I also specify the layer name in order to refer to the input layer. Then I compute the heatmap on a sample image &quot;img&quot;:</p>
  <pre><code>heatmap = cam.compute_heatmap(img)
  </code></pre>
  <p>Now, let's focus on a part of the implementation of the function compute_heatmap from PyImageSearch:</p>
  <pre><code># record operations for automatic differentiation
  with tf.GradientTape() as tape:
      # cast the image tensor to a float-32 data type, pass the
      # image through the gradient model, and grab the loss
      # associated with the specific class index
      inputs = tf.cast(image, tf.float32)
      (convOutputs, predictions) = gradModel(inputs)
      # loss = predictions[:, self.classIdx]  # original from PyImageSearch
      loss = predictions[:]  # modified by me as I have only 1 output unit
  
  # use automatic differentiation to compute the gradients
  grads = tape.gradient(loss, convOutputs)
  </code></pre>
  <p>The problem here is that the gradient grads is None.</p>
  <p>I thought that maybe the problem could lie in the network structure (all goes fine when reproducing the example of the classification task from the website), but I can't figure out where is the problem with this network used for regression!</p>
  <p>Could you please help me?</p>