Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf.while example is not working in eager mode #18257

Closed
arogozhnikov opened this issue Apr 5, 2018 · 9 comments
Closed

tf.while example is not working in eager mode #18257

arogozhnikov opened this issue Apr 5, 2018 · 9 comments
Assignees
Labels
comp:ops OPs related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.7 Issues related to TF 2.7.0

Comments

@arogozhnikov
Copy link

  • 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
@poxvoculi poxvoculi assigned asimshankar and unassigned poxvoculi Apr 6, 2018
@poxvoculi poxvoculi added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Apr 6, 2018
@asimshankar asimshankar assigned akshaym and unassigned asimshankar Apr 6, 2018
@asimshankar
Copy link
Contributor

Thanks for the report, @akshaym is looking into fixing that.

@tensorflowbutler tensorflowbutler removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Apr 7, 2018
@danmou
Copy link

danmou commented Oct 24, 2019

This is fixed on TF 1.15 but broken on TF 2.0.

@akshaym
Copy link
Contributor

akshaym commented Oct 24, 2019

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

@datdinhquoc
Copy link

datdinhquoc commented Feb 12, 2020

On Colab, TF 2, api: https://www.tensorflow.org/api_docs/python/tf/while_loop

Example in api doc:

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])

TF 2 on Eager mode by default:

TypeError: Cannot iterate over a scalar tensor.

My work-around (wrap in autograph @tf.function):

@tf.function
def f():
  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])
  return r

@yaowang1111
Copy link

Has this bug been solved? I run tf2.1 and the problem is still there.

@mohantym mohantym added the comp:ops OPs related issues label Jan 20, 2022
@mohantym
Copy link
Contributor

mohantym commented Jan 20, 2022

I was able to replicate and resolve this issue in 2.7

@mohantym mohantym added the TF 2.7 Issues related to TF 2.7.0 label Jan 20, 2022
@mohantym mohantym self-assigned this Jan 20, 2022
@mohantym mohantym added the stat:awaiting response Status - Awaiting response from author label Jan 20, 2022
@Engineero
Copy link

It turns out this has been causing a headache for me through several versions of TF 2.x. I anxiously await a resolution.

@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

@google-ml-butler google-ml-butler bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Jan 28, 2022
@google-ml-butler
Copy link

Closing as stale. Please reopen if you'd like to work on this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.7 Issues related to TF 2.7.0
Projects
None yet
Development

No branches or pull requests