tensorflow: LookupError when computing nested gradient with UpSampling2D

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • TensorFlow installed from (source or binary): binary (pip)
  • TensorFlow version (use command below): 2.3.1
  • Python version: 3.7.4
  • CUDA/cuDNN version: 10.1
  • GPU model and memory: NVIDIA GeForce GTX 1070 8GB VRAM

Describe the current behavior The provided code fails with the following error (see Colab link for full stacktrace): LookupError: gradient registry has no entry for: ResizeNearestNeighborGrad

Describe the expected behavior The GradientTape.gradient method should be able to compute the gradient.

Standalone code to reproduce the issue Google Colab standalone code

Other info / logs I encountered the error while developing a GAN. I used Conv2DTranspose for upsampling at first but encountered artifacts. After changing Conv2DTranspose to a combination of UpSampling2D and Conv2D (as is common in GAN’s) the inner gradient stopped working.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 18 (7 by maintainers)

Most upvoted comments

I just found a workaround. Instead of using tensorflows UpSampling operation I defined my own version using tf.repeat along both dimensions. It can obviously only perform nearest neighbor upsampling as opposed to the actual UpSampling2D that has the option to use bilinear sampling. But for nearest neighbor it works exactly like UpSampling2D and does not have the gradient bug. It is a little slow though:

class CustomUpSampling2D(tf.keras.layers.Layer):
  def __init__(self, size):
    super(CustomUpSampling2D, self).__init__()
    if type(size) is not tuple and type(size) is not list:
        size = (size, size)
    self.size = size

  def build(self, input_shape):
    pass

  def call(self, input):
      return tf.repeat(tf.repeat(input, self.size[0], axis=1), self.size[1], axis=2)