早上好,
我正在尝试按照这篇论文描述的方式,实现改进的WGAN来处理1D数据: https://arxiv.org/pdf/1704.00028.pdf
keras-contrib github中有一个示例实现: https://github.com/keras-team/keras-contrib/blob/master/examples/improved_wgan.py 然而,使用tf2时梯度惩罚损失的这个实现已经不再有效。K.gradients()返回[None]。
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:505 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:467 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
<ipython-input-7-4f0896d0107b>:104 gradient_penalty_loss
gradients_sqr = K.square(gradients)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:2189 square
return math_ops.square(x)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py:9964 square
"Square", x=x, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:488 _apply_op_helper
(input_name, err))
ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported.
这里是问题的完整示例: https://colab.research.google.com/drive/11dcMKoiCigTnEn7QvmjqLNrJdmFztByT 有人知道发生了什么变化吗?有什么解决方法吗?
更新:忽略构建计算图时出现的错误,然后它似乎可以运行。
def gradient_penalty_loss(y_true, y_pred, averaged_samples):
gradients = K.gradients(y_pred, averaged_samples)[0]
try:
gradients_sqr = K.square(gradients)
except ValueError:
print("Gradients returned None")
return 0
gradients_sqr_sum = K.sum(gradients_sqr, axis=np.arange(1, len(gradients_sqr.shape)))
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
gradient_penalty = K.square(1 - gradient_l2_norm)
return K.mean(gradient_penalty)
然而,我的损失函数越来越高,是否忽略了gradient_penalty_loss?
![损失函数](https://istack.dev59.com/On9Cq.webp)
tensorflow.compat.v1.disable_eager_execution()
)成功消除了相关错误。如果您确实需要急切执行,则需要使用gradienttape。我暂时不会将此写成答案,但是我想如果这仍然阻止您,那么您可能更喜欢一些即将可行而不是稍后可行的东西。 - Josiah