0

我正在计算 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
4

1 回答 1

0

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

于 2018-12-21T11:20:18.723 回答