Creates a new iterator from the given dataset. If `dataset` is not specified, the iterator will be created from the given tensor components and element structure. In particular, the alternative for constructing the iterator is used when the iterator is reconstructed from it `Composi
(self, dataset=None, components=None, element_spec=None)
| 557 | """An iterator producing tf.Tensor objects from a tf.data.Dataset.""" |
| 558 | |
| 559 | def __init__(self, dataset=None, components=None, element_spec=None): |
| 560 | """Creates a new iterator from the given dataset. |
| 561 | |
| 562 | If `dataset` is not specified, the iterator will be created from the given |
| 563 | tensor components and element structure. In particular, the alternative for |
| 564 | constructing the iterator is used when the iterator is reconstructed from |
| 565 | it `CompositeTensor` representation. |
| 566 | |
| 567 | Args: |
| 568 | dataset: A `tf.data.Dataset` object. |
| 569 | components: Tensor components to construct the iterator from. |
| 570 | element_spec: A nested structure of `TypeSpec` objects that |
| 571 | represents the type specification of elements of the iterator. |
| 572 | |
| 573 | Raises: |
| 574 | ValueError: If `dataset` is not provided and either `components` or |
| 575 | `element_spec` is not provided. Or `dataset` is provided and either |
| 576 | `components` and `element_spec` is provided. |
| 577 | """ |
| 578 | |
| 579 | error_message = "Either `dataset` or both `components` and " |
| 580 | "`element_spec` need to be provided." |
| 581 | |
| 582 | self._device = context.context().device_name |
| 583 | |
| 584 | if dataset is None: |
| 585 | if (components is None or element_spec is None): |
| 586 | raise ValueError(error_message) |
| 587 | # pylint: disable=protected-access |
| 588 | self._element_spec = element_spec |
| 589 | self._flat_output_types = structure.get_flat_tensor_types( |
| 590 | self._element_spec) |
| 591 | self._flat_output_shapes = structure.get_flat_tensor_shapes( |
| 592 | self._element_spec) |
| 593 | self._iterator_resource, self._deleter = components |
| 594 | # Delete the resource when this object is deleted |
| 595 | self._resource_deleter = IteratorResourceDeleter( |
| 596 | handle=self._iterator_resource, |
| 597 | device=self._device, |
| 598 | deleter=self._deleter) |
| 599 | else: |
| 600 | if (components is not None or element_spec is not None): |
| 601 | raise ValueError(error_message) |
| 602 | if (_device_stack_is_empty() or |
| 603 | context.context().device_spec.device_type != "CPU"): |
| 604 | with ops.device("/cpu:0"): |
| 605 | self._create_iterator(dataset) |
| 606 | else: |
| 607 | self._create_iterator(dataset) |
| 608 | |
| 609 | def _create_iterator(self, dataset): |
| 610 | # pylint: disable=protected-access |
nothing calls this directly
no test coverage detected