Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow 2,渐变是一个空列表_Python_Tensorflow_Gradienttape - Fatal编程技术网

Python tensorflow 2,渐变是一个空列表

Python tensorflow 2,渐变是一个空列表,python,tensorflow,gradienttape,Python,Tensorflow,Gradienttape,我正在重新组织代码,使其易于阅读,但在编译时,它会说: ValueError:没有为任何变量提供渐变:['enc_conv_4/kernel:0'…。我知道我的损失函数是可微的,因为代码在触摸它之前就工作过,但现在缺少我模型的渐变 @tf.function def train_disc(self,real_imgs,gen_imgs): with tf.GradientTape() as disc_tape: d_loss = self.wassers

我正在重新组织代码,使其易于阅读,但在编译时,它会说:
ValueError:没有为任何变量提供渐变:['enc_conv_4/kernel:0'…
。我知道我的损失函数是可微的,因为代码在触摸它之前就工作过,但现在缺少我模型的渐变

    @tf.function
    def train_disc(self,real_imgs,gen_imgs):
      with tf.GradientTape() as disc_tape:
        d_loss = self.wasserstein_loss(real_imgs,gen_imgs)
      gradients_d = disc_tape.gradient(d_loss, self.discriminator.trainable_variables)
      self.d_optimizer.apply_gradients(zip(gradients_d, self.discriminator.trainable_variables))
      return d_loss

    @tf.function
    def train_gen(self,real_img,gen_imgs,mask,img_feat,rot_feat_mean):
      with tf.GradientTape() as gen_tape:
        g_loss_param = self.generator_loss(mask,img_feat,rot_feat_mean)
        g_loss = g_loss_param(real_img, gen_imgs)
      gradients_g = gen_tape.gradient(g_loss, self.generator.trainable_variables)
      print(gradients_g)
      self.g_optimizer.apply_gradients(zip(gradients_g, self.generator.trainable_variables))
如您所见,当我对鉴别器和生成器执行相同的操作时,生成器会给我一个空的渐变列表

gen_imgs = self.generator([real_img, mask], training=True)


d_loss = self.train_disc(real_img,gen_imgs[:,:,:,:-1])

if step%self.n_critic == 0:
  masked_images = real_img * mask
  idx = 3  # index of desired layer
  layer_input = Input(shape=(self.img_shape))  #
  x = layer_input
  for layer in self.generator.layers[idx:idx+12]:
      x = layer(x)
  model_feat = Model(inputs=layer_input,outputs=x)
  model_feat.trainable = False
  img_feat = model_feat(masked_images,training=False)
  rot_feat_mean = []
  for i in range(self.batch_size):
      rot = []
      for an in [180, 155, 130, 105, 80, 55, 20, 10]:
          r = tf.keras.preprocessing.image.random_rotation(masked_images[i], an, row_axis=0, col_axis=1,
                                                           channel_axis=2)
          rot.append(r)
      rot = np.array(rot)
      rot_feat_mean.append(np.mean(model_feat(rot,training=False),axis=0))
  rot_feat_mean = np.array(rot_feat_mean)
  g_loss = self.train_gen(real_img,gen_imgs[:,:,:,:-1],mask,img_feat,rot_feat_mean)
最后一段代码的最后一行给了我一个错误。我不知道这个错误是否是由于语义错误造成的