tf.compat.v1.data.Iterator

Represents the state of iterating through a Dataset.

iterator_resource A tf.resource scalar tf.Tensor representing the iterator.
initializer A tf.Operation that should be run to initialize this iterator.
output_types A (nested) structure of tf.DType objects corresponding to each component of an element of this iterator.
output_shapes A (nested) structure of tf.TensorShape objects corresponding to each component of an element of this iterator.
output_classes A (nested) structure of Python type objects corresponding to each component of an element of this iterator.

element_spec The type specification of an element of this iterator.

For more information, read this guide.

initializer A tf.Operation that should be run to initialize this iterator.
output_classes Returns the class of each component of an element of this iterator. (deprecated)

The expected values are tf.Tensor and tf.sparse.SparseTensor.

output_shapes Returns the shape of each component of an element of this iterator. (deprecated)

output_types Returns the type of each component of an element of this iterator. (deprecated)

Methods

from_string_handle

View source

Creates a new, uninitialized Iterator based on the given handle.

This method allows you to define a "feedable" iterator where you can choose between concrete iterators by feeding a value in a tf.Session.run call. In that case, string_handle would be a tf.compat.v1.placeholder, and you would feed it with the value of tf.data.Iterator.string_handle in each step.

For example, if you had two iterators that marked the current position in a training dataset and a test dataset, you could choose which to use in each step as follows:

train_iterator = tf.data.Dataset(...).make_one_shot_iterator()
train_iterator_handle = sess.run(train_iterator.string_handle())

test_iterator = tf.data.Dataset(...).make_one_shot_iterator()
test_iterator_handle = sess.run(test_iterator.string_handle())

handle = tf.compat.v1.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(
    handle, train_iterator.output_types)

next_element = iterator.get_next()
loss = f(next_element)

train_loss = sess.run(loss, feed_dict={handle: train_iterator_handle})
test_loss = sess.run(loss, feed_dict={handle: test_iterator_handle})

Args
string_handle A scalar tf.Tensor of type tf.string that evaluates to a handle produced by the Iterator.string_handle() method.
output_types A (nested) structure of tf.DType objects corresponding to each component of an element of this dataset.
output_shapes (Optional.) A (nested) structure of tf.TensorShape objects corresponding to each component of an element of this dataset. If omitted, each component will have an unconstrainted shape.
output_classes (Optional.) A (nested) structure of Python type objects corresponding to each component of an element of this iterator. If omitted, each component is assumed to be of type tf.Tensor.

Returns
An Iterator.

from_structure

View source

Creates a new, uninitialized Iterator with the given structure.

This iterator-constructing method can be used to create an iterator that is reusable with many different datasets.

The returned iterator is not bound to a particular dataset, and it has no initializer. To initialize the iterator, run the operation returned by Iterator.make_initializer(dataset).

The following is an example

iterator = Iterator.from_structure(tf.int64, tf.TensorShape([]))

dataset_range = Dataset.range(10)
range_initializer = iterator.make_initializer(dataset_range)

dataset_evens = dataset_range.filter(lambda x: x % 2 == 0)
evens_initializer = iterator.make_initializer(dataset_evens)

# Define a model based on the iterator; in this example, the model_fn
# is expected to take scalar tf.int64 Tensors as input (see
# the definition of 'iterator' above).
prediction, loss = model_fn(iterator.get_ne