计算wgan-gp的梯度罚分时出错

时间:2018-09-25 11:11:18

标签: python tensorflow machine-learning deep-learning generative-adversarial-network

我当时正在用wgan-gp计算损失函数,我想知道我的代码出了什么问题或者是否需要实施其他方法

with tf.GradientTape() as critic_tape:
     generated_images = generator(tf.random_normal([16, 100]), training=True)
     a = tf.convert_to_tensor(images[:16])
     real_output = critic(a, training=True)
    generated_output = critic(generated_images, training=True)               
    with tf.GradientTape() as gtape:
        epsilon = tf.random_uniform([], 0, 1)
        xhat = epsilon*a + (1-epsilon)*generated_images
        dhat = critic(xhat, training=True)
        gtape.watch(xhat)
   dhat2 = gtape.gradient(dhat, xhat)
   slopes = tf.sqrt(tf.reduce_sum(tf.square(dhat2), reduction_indices=[1]))
   gradient_penalty = 10*tf.reduce_mean((slopes-1.0)**2)
critic_loss = get_critic_loss(real_output, generated_output)
critic_loss+= gradient_penalty                
gradients_of_critic = critic_tape.gradient(critic_loss, critic.variables)

这是错误堆栈,我正在使用tensorflow急切执行,因此我们将不胜感激任何帮助

---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-512-cbc8ebf905ac> in <module>()
     16     critic_loss = get_critic_loss(real_output, generated_output)
     17     critic_loss+= gradient_penalty
---> 18 gradients_of_critic = critic_tape.gradient(critic_loss, critic.variables)
     19 print(gradients_of_critic)

c:\users\vibhu\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\backprop.py in gradient(self, target, sources, output_gradients)
    856     flat_grad = imperative_grad.imperative_grad(
    857         _default_vspace, self._tape, nest.flatten(target), flat_sources,
--> 858         output_gradients=output_gradients)
    859 
    860     if not self._persistent:

c:\users\vibhu\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\imperative_grad.py in imperative_grad(vspace, tape, target, sources, output_gradients)
     61   """
     62   return pywrap_tensorflow.TFE_Py_TapeGradient(
---> 63       tape._tape, vspace, target, sources, output_gradients)  # pylint: disable=protected-access

c:\users\vibhu\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\backprop.py in _gradient_function(op_name, attr_tuple, num_inputs, inputs, outputs, out_grads)
    110   """
    111   mock_op = _MockOp(attr_tuple, inputs, outputs, op_name)
--> 112   grad_fn = ops._gradient_registry.lookup(op_name)  # pylint: disable=protected-access
    113   if grad_fn is None:
    114     return [None] * num_inputs

c:\users\vibhu\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\registry.py in lookup(self, name)
     91     else:
     92       raise LookupError(
---> 93           "%s registry has no entry for: %s" % (self._name, name))

LookupError: gradient registry has no entry for: StatefulPartitionedCall

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,可能与您使用的代码相同。为我的评论者禁用 tf.contrib.eager.defun 解决了我的问题。这当然不是一个好的解决方案,因为您将无法获得加速的defun,但至少可以执行代码。