Implementing gradient penalty loss with tensorflow 2

Viewed 5784

Good morning,

I am trying to implement the improved WGAN for 1D data as described on this paper: https://arxiv.org/pdf/1704.00028.pdf

It has been implemented as an example in the keras-contrib github: https://github.com/keras-team/keras-contrib/blob/master/examples/improved_wgan.py Nevertheless, this implementation of the gradient penalty loss is not working anymore with tf2. K.gradients() returns [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.

Here it is the complete example of the issue: https://colab.research.google.com/drive/11dcMKoiCigTnEn7QvmjqLNrJdmFztByT

Does anyone know what has changed? Any idea of how to solve this?

UPDATE: This ignores the error while constructing de computational graph. Then it seems to run

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)

Nevertheless, I get incresingly high loss functions, is gradient_penalty_loss being ignored? Loss

1 Answers

If you do what is proposed in the UPDATE, tf will just ignore the loss function

With Tensorflow 2, it seems imposible to to this the old way. I finally change the code to adapt it to this way of creating models. What I suggest?

  1. Create the gen/disc models with keras
  2. Join them extending tf.keras.Model class like the WGAN of : https://github.com/timsainb/tensorflow2-generative-models
Related