我在计算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-accessc:\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_inputsc:\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
回答:
我遇到了同样的问题,很可能是基于和你相同的代码。对于我的批评者禁用tf.contrib.eager.defun解决了我的问题。当然,这不是一个好的解决方案,因为你将无法获得defun的加速,但至少你可以执行你的代码。