tf.data.Iterator
Stay organized with collections
Save and categorize content based on your preferences.
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)
For more information,
read this guide.
|
Methods
get_next
View source
@abc.abstractmethod
get_next()
Returns the next element.
dataset = tf.data.Dataset.from_tensors(42)
iterator = iter(dataset)
print(iterator.get_next())
tf.Tensor(42, shape=(), dtype=int32)
get_next_as_optional
View source
@abc.abstractmethod
get_next_as_optional()
Returns the next element wrapped in tf.experimental.Optional
.
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)
__iter__
__iter__()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.data.Iterator\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/ops/iterator_ops.py#L556-L653) |\n\nRepresents an iterator of a [`tf.data.Dataset`](../../tf/data/Dataset).\n\n[`tf.data.Iterator`](../../tf/data/Iterator) is the primary mechanism for enumerating elements of a\n[`tf.data.Dataset`](../../tf/data/Dataset). It supports the Python Iterator protocol, which means\nit can be iterated over using a for-loop: \n\n dataset = tf.data.Dataset.range(2)\n for element in dataset:\n print(element)\n tf.Tensor(0, shape=(), dtype=int64)\n tf.Tensor(1, shape=(), dtype=int64)\n\nor by fetching individual elements explicitly via `get_next()`: \n\n dataset = tf.data.Dataset.range(2)\n iterator = iter(dataset)\n print(iterator.get_next())\n tf.Tensor(0, shape=(), dtype=int64)\n print(iterator.get_next())\n tf.Tensor(1, shape=(), dtype=int64)\n\nIn addition, non-raising iteration is supported via `get_next_as_optional()`,\nwhich returns the next element (if available) wrapped in a\n[`tf.experimental.Optional`](../../tf/experimental/Optional). \n\n dataset = tf.data.Dataset.from_tensors(42)\n iterator = iter(dataset)\n optional = iterator.get_next_as_optional()\n print(optional.has_value())\n tf.Tensor(True, shape=(), dtype=bool)\n optional = iterator.get_next_as_optional()\n print(optional.has_value())\n tf.Tensor(False, shape=(), dtype=bool)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `element_spec` | The type specification of an element of this iterator. \u003cbr /\u003e dataset = tf.data.Dataset.from_tensors(42) iterator = iter(dataset) iterator.element_spec tf.TensorSpec(shape=(), dtype=tf.int32, name=None) For more information, read [this guide](https://www.tensorflow.org/guide/data#dataset_structure). |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `get_next`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/ops/iterator_ops.py#L615-L630) \n\n @abc.abstractmethod\n get_next()\n\nReturns the next element. \n\n dataset = tf.data.Dataset.from_tensors(42)\n iterator = iter(dataset)\n print(iterator.get_next())\n tf.Tensor(42, shape=(), dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A (nested) structure of values matching [`tf.data.Iterator.element_spec`](../../tf/data/Iterator#element_spec). ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|---|---|\n| [`tf.errors.OutOfRangeError`](../../tf/errors/OutOfRangeError): If the end of the iterator has been reached. ||\n\n\u003cbr /\u003e\n\n### `get_next_as_optional`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/ops/iterator_ops.py#L632-L653) \n\n @abc.abstractmethod\n get_next_as_optional()\n\nReturns the next element wrapped in [`tf.experimental.Optional`](../../tf/experimental/Optional).\n\nIf the iterator has reached the end of the sequence, the returned\n[`tf.experimental.Optional`](../../tf/experimental/Optional) will have no value. \n\n dataset = tf.data.Dataset.from_tensors(42)\n iterator = iter(dataset)\n optional = iterator.get_next_as_optional()\n print(optional.has_value())\n tf.Tensor(True, shape=(), dtype=bool)\n print(optional.get_value())\n tf.Tensor(42, shape=(), dtype=int32)\n optional = iterator.get_next_as_optional()\n print(optional.has_value())\n tf.Tensor(False, shape=(), dtype=bool)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A [`tf.experimental.Optional`](../../tf/experimental/Optional) object representing the next element. ||\n\n\u003cbr /\u003e\n\n### `__iter__`\n\n __iter__()"]]