tensorflow: tensorflow.python.keras.testing_utils.layer_test breaks when a (custom) layer is returning a list/tuple of tensor

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

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): gLinux
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
  • TensorFlow installed from (source or binary): pip3 installed
  • TensorFlow version (use command below): 2.0.0
  • Python version: 3.6

Describe the current behavior When we create a custom tensorflow.keras.layer.Layer, said MyCustomLayer, that returns a list/tuple of tensors in it’s call method, calling layer_test(MyCustomLayer, input_shape=expected_input_shape) will break as layer_test does not expect list / tuple as outputs of the custom layer’s call method, and by tensorflow’s keras layer documentation here, list / tuple of tensors are valid output.

Code to reproduce the issue

import tensorflow as tf
from tensorflow.python.keras.testing_utils import layer_test

# a dummy layer that just returns a list of input
class MyCustomLayer(tf.keras.layers.Layer):
  def call(self, input):
    return [input, input]

layer_test(MyCustomLayer, input_shape=(1,2))

Describe the expected behavior The expected behavior is that the above snippet will not return errors (log attached below)

Potential Solution: Check the input x in dtype() method in tensorflow_core/python/keras/backend.py.

Happy to contribute if this is helpful 😃

Other info / logs

Error Log:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-0eea06e136e5> in <module>()
----> 1 layer_test(MyCustomLayer, input_shape=(1,2))

2 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/framework/test_util.py in decorated(self, *args, **kwargs)
   1583       original_var = os.environ.get("TF_CUDNN_DETERMINISTIC", "")
   1584       os.environ["TF_CUDNN_DETERMINISTIC"] = "true"
-> 1585       result = f(self, *args, **kwargs)
   1586       os.environ["TF_CUDNN_DETERMINISTIC"] = original_var
   1587       return result

/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/testing_utils.py in layer_test(layer_cls, kwargs, input_shape, input_dtype, input_data, expected_output, expected_output_dtype, expected_output_shape, validate_training, adapt_data)
    140   x = keras.layers.Input(shape=input_shape[1:], dtype=input_dtype)
    141   y = layer(x)
--> 142   if keras.backend.dtype(y) != expected_output_dtype:
    143     raise AssertionError('When testing layer %s, for input %s, found output '
    144                          'dtype=%s but expected to find %s.\nFull kwargs: %s' %

/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/backend.py in dtype(x)
   1247   ```
   1248   """
-> 1249   return x.dtype.base_dtype.name
   1250 
   1251 

AttributeError: 'list' object has no attribute 'dtype'

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@Squadrick stay safe 😃

@Squadrick yes may you pick this one up? I didn’t have too much time recently to dive deeper into good solution, thanks!

Chatted with @shun-lin and Shun is happy to contribute. Thanks Shun!