contrib.data.Iterator

tf.contrib.data.Iterator

class tf.contrib.data.Iterator

Defined in tensorflow/contrib/data/python/ops/dataset_ops.py.

Represents the state of iterating through a Dataset.

Properties

initializer

A tf.Operation that should be run to initialize this iterator.

Returns:

A tf.Operation that should be run to initialize this iterator

Raises:

  • ValueError: If this iterator initializes itself automatically.

output_shapes

Returns the shape of each component of an element of this iterator.

Returns:

A nested structure of tf.TensorShape objects corresponding to each component of an element of this iterator.

output_types

Returns the type of each component of an element of this iterator.

Returns:

A nested structure of tf.DType objects corresponding to each component of an element of this iterator.

Methods

__init__

__init__(
    iterator_resource,
    initializer,
    output_types,
    output_shapes
)

Creates a new iterator from the given iterator resource.

NOTE(mrry): Most users will not call this initializer directly, and will instead use Iterator.from_dataset() or Dataset.make_one_shot_iterator().

Args:

  • 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 dataset.

dispose_op

dispose_op(name=None)

Returns a tf.Operation that destroys this iterator.

The returned operation may be used to release any resources consumed by this iterator without closing the session.

Args:

  • name: (Optional.) A name for the created operation.

Returns:

A tf.Operation.

from_dataset

from_dataset(
    dataset,
    shared_name=None
)

Creates a new, uninitialized Iterator from the given Dataset.

To initialize this iterator, you must run its initializer:

dataset = ...
iterator = Iterator.from_dataset(dataset)
# ...
sess.run(iterator.initializer)

Args:

  • dataset: A Dataset object.
  • shared_name: (Optional.) If non-empty, this iterator will be shared under the given name across multiple sessions that share the same devices (e.g. when using a remote server).

Returns:

An Iterator.

from_structure

from_structure(
    output_types,
    output_shapes=None,
    shared_name=None
)

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_next())

# Train for `num_epochs`, where for each epoch, we first iterate over
# dataset_range, and then iterate over dataset_evens.
for _ in range(num_epochs):
  # Initialize the iterator to `dataset_range`
  sess.run(range_initializer)
  while True:
    try:
      pred, loss_val = sess.run([prediction, loss])
    except tf.errors.OutOfRangeError:
      break

  # Initialize the iterator to `dataset_evens`
  sess.run(evens_initializer)
  while True:
    try:
      pred, loss_val = sess.run([prediction, loss])
    except tf.errors.OutOfRangeError:
      break

Args:

  • output_types: A nested structure of tf.DType objects corresponding to each component of an element of this iterator.
  • 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.
  • shared_name: (Optional.) If non-empty, this iterator will be shared under the given name across multiple sessions that share the same devices (e.g. when using a remote server).

Returns:

An Iterator.

Raises:

  • TypeError: If the structures of output_shapes and output_types are not the same.

get_next

get_next(name=None)

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

Args:

  • name: (Optional.) A name for the created operation.

Returns:

A nested structure of tf.Tensor objects.

make_initializer

make_initializer(dataset)

Returns a tf.Operation that initializes this iterator on dataset.

Args:

  • dataset: A Dataset with compatible structure to this iterator.

Returns:

A tf.Operation that can be run to initialize this iterator on the given dataset.

Raises:

  • TypeError: If dataset and this iterator do not have a compatible element structure.

© 2017 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/api_docs/python/tf/contrib/data/Iterator

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部