W3cubDocs

/TensorFlow 2.3

tf.data.Iterator

View source on GitHub

Represents an iterator of a tf.data.Dataset.

tf.data.Iterator is the primary mechanism for enumerating elements of a tf.data.Dataset. It supports the Python Iterator protocol, which means it can be iterated over using a for-loop:

dataset = tf.data.Dataset.range(2)
for element in dataset:
  print(element)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)

or by fetching individual elements explicitly via get_next():

dataset = tf.data.Dataset.range(2)
iterator = iter(dataset)
print(iterator.get_next())
tf.Tensor(0, shape=(), dtype=int64)
print(iterator.get_next())
tf.Tensor(1, shape=(), dtype=int64)

In addition, non-raising iteration is supported via get_next_as_optional(), which returns the next element (if available) wrapped in a tf.experimental.Optional.

dataset = tf.data.Dataset.from_tensors(42)
iterator = iter(dataset)
optional = iterator.get_next_as_optional()
print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
optional = iterator.get_next_as_optional()
print(optional.has_value())
tf.Tensor(False, shape=(), dtype=bool)
Attributes
element_spec The type specification of an element of this iterator.
dataset = tf.data.Dataset.from_tensors(42)
iterator = iter(dataset)
iterator.element_spec
tf.TensorSpec(shape=(), dtype=tf.int32, name=None)

Methods

get_next

View source

Returns a nested structure of tf.Tensors containing the next element.

dataset = tf.data.Dataset.from_tensors(42)
iterator = iter(dataset)
print(iterator.get_next())
tf.Tensor(42, shape=(), dtype=int32)
Returns
A nested structure of tf.Tensor objects.
Raises
tf.errors.OutOfRangeError: If the end of the iterator has been reached.

get_next_as_optional

View source

Returns a tf.experimental.Optional which contains the next element.

If the iterator has reached the end of the sequence, the returned tf.experimental.Optional will have no value.

dataset = tf.data.Dataset.from_tensors(42)
iterator = iter(dataset)
optional = iterator.get_next_as_optional()
print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
print(optional.get_value())
tf.Tensor(42, shape=(), dtype=int32)
optional = iterator.get_next_as_optional()
print(optional.has_value())
tf.Tensor(False, shape=(), dtype=bool)
Returns
A tf.experimental.Optional object representing the next element.

__iter__

© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/data/Iterator