tensorflow: ```tf.stack``` returns different shapes when run in eager and graph mode

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):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary):binary
  • TensorFlow version (use command below):2.3
  • Python version:
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior tf.stack(list(np.ones([2,0,3]))).shape returns EagerTensor with shape [2,0] Describe the expected behavior tf.stack(list(np.ones([2,0,3]))).shape returns EagerTensor with shape [2,0,3] which same as graph mode

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook. Eager mode:

import tensorflow as tf
print(tf.stack(list(np.ones([2,0,3]))).shape)
tf.compat.v1.disable_eager_execution()
print(tf.stack(list(np.ones([2,0,3]))).shape)

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (11 by maintainers)

Most upvoted comments

The issue is really due to tf.convert_to_tensor which does not handling shape correctly:

% python3
Python 3.7.3 (default, Jun  2 2020, 19:48:59) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> import numpy as np
>>> x = list(np.ones([2,0,3]))
>>> x
[array([], shape=(0, 3), dtype=float64), array([], shape=(0, 3), dtype=float64)]
>>> y = tf.convert_to_tensor(x)
>>> y
<tf.Tensor: shape=(2, 0), dtype=float32, numpy=array([], shape=(2, 0), dtype=float32)>

It looks like tf.convert_to_tensor() in eager mode (or more specifically InitEagerTensor), dropped the dimension of 3.

Hi @fsx950223 ! This issue is getting resolved with tf.config.run_functions_eagerly(False) instead of tf.compat.v1.disable_eager_execution(). Attaching gist for reference. Thank you!