“没有为任何变量提供梯度”在尝试训练我的编码时

2021-05-16 07:46:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图构建一个双向GAN(一个同时学习到潜在空间逆映射的GAN)。由于某些原因,当我试图计算我的编码器梯度,我得到的错误是'没有梯度提供任何变量'。但奇怪的是,它适用于我的生成器和鉴别器,尽管它们都基于相同的体系结构。你知道吗

def _add_optims(self, optims: dict):
    """
    Adds optims to model that are to be used by optimizers or during training
    Parameters
    ----------
    optim: dict
        dictionary containing all optimizers, optimizers should be of Type[tf.train.Optimizer]
    """
    if self._optims is not None and len(optims) != 0:
        logging.warning('Change of optims is not yet supported')
        pass
        #raise NotImplementedError()
    elif self._optims is not None and len(optims) == 0:
        pass
    else:
        self._optims = optims

            optim_gen = self._optims['gen']
            grads_gen = optim_gen.compute_gradients(self._losses['total_gen'], var_list=self.gen.trainable_variables)
            step_gen = optim_gen.apply_gradients(grads_gen)

            optim_discr = self._optims['discr']
            grads_discr = optim_discr.compute_gradients(self._losses['total_discr'], var_list=self.discr.trainable_variables)
            step_discr = optim_discr.apply_gradients(grads_discr)

            optim_enc = self._optims['enc']
            grads_enc = optim_enc.compute_gradients(self._losses['total_enc'], var_list=self.enc.trainable_variables)
            step_enc = optim_enc.apply_gradients(grads_enc)


            steps = tf.group([step_gen, step_enc, step_discr])

            self.outputs_train.append(steps)

我的调试输出: 从我的编码器

[(None, <tf.Variable 'conv2d...e=float32>), (None, <tf.Variable 'batch_...e=float32>), (None, <tf.Variable 'batch_...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), (None, <tf.Variable 'sequen...e=float32>), ...]

从我的发电机

[(<tf.Tensor 'gradient...e=float32>, <tf.Variable 'dense/...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'dense/...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), (<tf.Tensor 'gradient...e=float32>, <tf.Variable 'sequen...e=float32>), ...]