title: Keras: No gradients provided for any variable
body: <p>I am working on a GAN based problem and I am getting this issue where the gradients are not visible. I was hoping if someone can help me here. I have two main piece of codes that might be able to help you identify the issue. First piece of code loads the data in the variables.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>training_images = []
  target_images = []
  
  for image in images:
      a_img = cv2.resize(cv2.imread( os.path.join(Augment_img_dir, image),0), (128,128))/255
      
      lsri_img = cv2.resize(cv2.imread( os.path.join(LSRI_img_dir, image),0), (128,128), cv2.INTER_NEAREST)/255
      
      hsri_img = cv2.resize(cv2.imread( os.path.join(HSRI_img_dir, image),0), (128,128))/255
      
      img_train = [a_img, lsri_img]
      img_train = np.asarray(img_train)
      img_train = np.moveaxis(img_train,0, -1)
      
      training_images.append(img_train)
      
      target_images.append(hsri_img)
  
  training_images = np.asarray(training_images)
  
  target_images = np.asarray(target_images)
  
  train_imgs, test_imgs, train_targets, test_targets = train_test_split(training_images, target_images, 
                                                                       test_size=.20, random_state=42)
  
  batch_size = 8
  train_img_batch = []
  target_img_batch = []
  len_imgs = len(train_imgs)
  start = 0
  temp_train = []
  temp_target = []
  for i in range(len_imgs+1):
      if i%batch_size == 0 and i&gt;0:
          train_img_batch.append(np.asarray(temp_train))
          target_img_batch.append(np.asarray(temp_target))
          temp_train = []
          temp_target = []
      if i != len_imgs:
          temp_train.append(train_imgs[i])
          temp_target.append(train_targets[i])
  
  train_img_batch = np.asarray(train_img_batch)
  target_img_batch = np.asarray(target_img_batch)</code></pre>
  </div>
  </div>
  </p>
  <p>The second piece of code is the actual train function where I am getting the error.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>batch_size = 8
  lr = 0.0001
  G_optimizer = Adam(learning_rate=lr)
  n_epoch  = 100
  iterations = 13500
  def train():
      G = Generator((128,128,2)).generator()
      D = Discriminator((128,128,1)).discriminator()
      
      g_optimizer_init = tf.optimizers.Adam(learning_rate=lr)
      g_optimizer = tf.optimizers.Adam(learning_rate=lr)
      d_optimizer = tf.optimizers.Adam(learning_rate=lr)
      mse_loss = keras.losses.MeanSquaredError()
      n_step_epoch = round(n_epoch // batch_size)    
      
      G_Loss_file = open("g_loss.txt",'w')
      
      
      for epoch in range(n_epoch):
          step_time = time.time()
          for step, lr_patchs in enumerate(tf.data.Dataset.from_tensor_slices(train_img_batch)):
              if lr_patchs.shape[0] != batch_size: # if the remaining data in this epoch &lt; batch_size
                  break
              
              hr_patchs = target_img_batch[step]
              with tf.GradientTape() as tape:
                  #tape.watch(G.trainable_weights)
                  fake_hr_patchs = G(lr_patchs)
                  fake_hr_patchs = np.reshape(fake_hr_patchs, (8,128,128))
                  mse_loss = mse_loss(fake_hr_patchs, hr_patchs)
                  
              grad = tape.gradient(mse_loss, G.trainable_variables)
              g_optimizer_init.apply_gradients(zip(grad, G.trainable_variables))
  
          print("Epoch: [{}/{}], time: {:.2f}s, mse: {:.2f} ".format(
                  epoch, n_epoch, time.time() - step_time, mse_loss))
              
          G_Loss_file.write("Epoch: [{}/{}], time: {:.2f}s, mse: {:.2f} \n".format(
              epoch, n_epoch, time.time() - step_time, mse_loss))
      G_Loss_file.close()
      
      ## adversarial learning (G, D)
      Loss_file = open("loss.txt",'w')
      n_step_epoch = round(n_epoch // batch_size)
      for epoch in range(n_epoch):
          step_time = time.time()
          for step, lr_patchs in enumerate(train_img_batch):
              if lr_patchs.shape[0] != batch_size: # if the remaining data in this epoch &lt; batch_size
                  break
                  
              hr_patchs = target_img_batch[step]
              with tf.GradientTape(persistent=True) as tape:
                  fake_patchs = G(lr_patchs)
                  fake_patchs = np.reshape(fake_patchs, (8,128,128))
                  logits_fake = D(fake_patchs)
                  logits_real = D(hr_patchs)
                  
                  
                  #d_Loss_int = Intensity_Loss(logits_fake, logits_real)
  
                  d_loss1 = tl.cost.sigmoid_cross_entropy(logits_fake, tf.zeros_like(logits_fake))
                  
                  d_loss = -np.log(d_loss1) #+ 0.1*d_Loss_int
                  
                  g_gan_loss = 1e-3 * tl.cost.sigmoid_cross_entropy(logits_fake, tf.ones_like(logits_fake))
                  mse_loss = tl.cost.mean_squared_error(fake_patchs, hr_patchs, is_mean=True)
                  
                  
                  g_loss = mse_loss + g_gan_loss
              
              grad = tape.gradient(g_loss, G.trainable_weights)
              #print(grad, len(G.trainable_weights))
              g_optimizer.apply_gradients(zip(grad, G.trainable_weights))
              grad = tape.gradient(d_loss, D.trainable_weights)
              d_optimizer.apply_gradients(zip(grad, D.trainable_weights))
          
          print("Epoch: [{}/{}], time: {:.3f}s, g_loss(mse:{:.3f},  adv:{:.3f}) d_loss: {:.3f}".format(
              epoch, n_epoch, time.time() - step_time, mse_loss, g_gan_loss, d_loss))
              
          Loss_file.write("Epoch: [{}/{}], time: {:.3f}s, g_loss(mse:{:.3f},  adv:{:.3f}) d_loss: {:.3f}".format(
              epoch, n_epoch, time.time() - step_time, mse_loss, g_gan_loss, d_loss))
          
          if epoch!=0 and ((epoch%10 == 0) or (epoch == n_epoch-1)):
              
              G.save_weights("Gan_Weights/g_training_20_noise_no_contrast.h5")
              D.save_weights("Gan_Weights/d_training_20_noise_no_contrast.h5")
          
      Loss_file.close()</code></pre>
  </div>
  </div>
  </p>
  <p>The line where I get the error is following:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>g_optimizer_init.apply_gradients(zip(grad, G.trainable_variables))</code></pre>
  </div>
  </div>
  </p>
  <p>and the error is following:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>No gradients provided for any variable: ['conv2d_392/kernel:0', 'conv2d_392/bias:0', 'batch_normalization_312/gamma:0', 'batch_normalization_312/beta:0', 'conv2d_393/kernel:0', 'conv2d_393/bias:0', 'batch_normalization_313/gamma:0', 'batch_normalization_313/beta:0', 'conv2d_394/kernel:0', 'conv2d_394/bias:0', 'batch_normalization_314/gamma:0', 'batch_normalization_314/beta:0', 'conv2d_395/kernel:0', 'conv2d_395/bias:0', 'batch_normalization_315/gamma:0', 'batch_normalization_315/beta:0', 'conv2d_396/kernel:0', 'conv2d_396/bias:0', 'conv2d_397/kernel:0', 'conv2d_397/bias:0', 'batch_normalization_316/gamma:0', 'batch_normalization_316/beta:0', 'conv2d_398/kernel:0', 'conv2d_398/bias:0', 'batch_normalization_317/gamma:0', 'batch_normalization_317/beta:0', 'conv2d_399/kernel:0', 'conv2d_399/bias:0', 'batch_normalization_318/gamma:0', 'batch_normalization_318/beta:0', 'conv2d_400/kernel:0', 'conv2d_400/bias:0', 'batch_normalization_319/gamma:0', 'batch_normalization_</code></pre>
  </div>
  </div>
  </p>