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

Calling next on an iterator over TF datasets should not have Python side-effects #279

Open
khatchad opened this issue Nov 1, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@khatchad
Copy link
Member

khatchad commented Nov 1, 2023

Consider the following code:

# From  https://www.tensorflow.org/guide/function#using_python_iterators_and_generators

import tensorflow as tf


@tf.function
def good_consume_next(iterator):
  # This is ok, iterator is a tf.data.Iterator
  tf.print("Value:", next(iterator))


ds = tf.data.Dataset.from_tensor_slices([1, 2, 3])
iterator = iter(ds)
good_consume_next(iterator)
good_consume_next(iterator)
good_consume_next(iterator)

Above, iterator is over a TF dataset. Calling next() on the iterator moves its cursor over the container. Thus, that would not be considered a Python side-effect, but only because the underlying container is a TF dataset container.

Regression

I believe is a type inferencing problem. The function call to iter() above will return a certain kind of iterator depending on its argument's type:

>>> type(iter(tf.data.Dataset.from_tensor_slices([1, 2, 3])))
<class 'tensorflow.python.data.ops.iterator_ops.OwnedIterator'>
@khatchad khatchad added the bug Something isn't working label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant