在计算wgan-gp的梯度惩罚时出现错误

我在计算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的加速,但至少你可以执行你的代码。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注