Creates a new iterator over the given dataset. For example: ```python dataset = tf.data.Dataset.range(4) for x in Iterator(dataset): print(x) ``` Tensors produced will be placed on the device on which this iterator object was created. Args: dataset: A `
(self, dataset)
| 33 | """ |
| 34 | |
| 35 | def __init__(self, dataset): |
| 36 | """Creates a new iterator over the given dataset. |
| 37 | |
| 38 | For example: |
| 39 | ```python |
| 40 | dataset = tf.data.Dataset.range(4) |
| 41 | for x in Iterator(dataset): |
| 42 | print(x) |
| 43 | ``` |
| 44 | |
| 45 | Tensors produced will be placed on the device on which this iterator object |
| 46 | was created. |
| 47 | |
| 48 | Args: |
| 49 | dataset: A `tf.data.Dataset` object. |
| 50 | |
| 51 | Raises: |
| 52 | TypeError: If `dataset` is an unsupported type. |
| 53 | RuntimeError: When invoked without eager execution enabled. |
| 54 | """ |
| 55 | if not context.context().device_spec.device_type: |
| 56 | is_remote_device = False |
| 57 | else: |
| 58 | is_remote_device = context.context().device_spec.device_type != "CPU" |
| 59 | if is_remote_device: |
| 60 | with ops.device(None): |
| 61 | # Let the placer figure out where to place the various functions etc. |
| 62 | # created by the CopyToDeviceDataset. |
| 63 | dataset = dataset.apply(prefetching_ops.copy_to_device( |
| 64 | context.context().device_name)) |
| 65 | dataset = dataset.prefetch(1) |
| 66 | super(Iterator, self).__init__(dataset) |
| 67 | |
| 68 | def _next_internal(self): |
| 69 | """Returns a nested structure of `tf.Tensor`s containing the next element. |