我正在尝试构建双方向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>), ...]