profile
viewpoint

Ask questionstf.while example is not working in eager mode

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes, see below
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
  • TensorFlow installed from (source or binary): pip
  • TensorFlow version (use command below): 1.7, release
  • Python version: 3.6
  • Bazel version (if compiling from source): NA
  • GCC/Compiler version (if compiling from source): NA
  • CUDA/cuDNN version: 9.0 / 7.0
  • GPU model and memory: GTX TITAN, 6GB
  • Exact command to reproduce: see below

Describe the problem

tf.while example is not working in eager mode

Source code / logs

Here is the code

import tensorflow as tf
tf.enable_eager_execution() # the only line added to the example

i = tf.constant(0)
c = lambda i: tf.less(i, 10)
b = lambda i: tf.add(i, 1)
r = tf.while_loop(c, b, [i])

result: TypeError: Cannot iterate over a scalar tensor.

Reason

b = lambda i: tf.add(i, 1)       # original
b = lambda i: (tf.add(i, 1), )   # fixed, working 
  • Body code (b) returns a single scalar, which code later tries to unpack.
  • Eager code doesn't support auto-wrapping single item to tuple

Possible solutions:

  • require body function to always return tuple
  • always check if body output is a single variable
tensorflow/tensorflow

Answer questions akshaym

I can reproduce this on 2.0. @saxenasaurabh can you please help take a look?

useful!

Related questions

ModuleNotFoundError: No module named 'tensorflow.contrib'
Error occurred when finalizing GeneratorDataset iterator
ModuleNotFoundError: No module named 'tensorflow.contrib'
When importing TensorFlow, error loading Hadoop hot 4
The flag 'log_dir' is defined twice. hot 3
[TF 2.0] Dataset has no attribute 'make_one_shot_iterator' hot 3
Lossy conversion from float32 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning. hot 3
TF2.0 AutoGraph issue hot 3
Error loading tensorflow hot 3
AttributeError: module 'tensorflow' has no attribute 'set_random_seed' hot 3
AttributeError: module 'tensorflow' has no attribute 'Session' hot 3
No tf.lite.experimental.nn.bidirectional_dynamic_rnn ops is finded hot 3
AttributeError: module 'tensorflow' has no attribute 'app' hot 3
Incorrect Error TypeError: padded_batch() missing 1 required positional argument: 'padded_shapes' hot 3
tensorflow2.0 detected 'xla_gpu' , but 'gpu' expected hot 2
Github User Rank List