Returns a `tf.Operation` that initializes this iterator on `dataset`. Args: dataset: A `Dataset` with compatible structure to this iterator. name: (Optional.) A name for the created operation. Returns: A `tf.Operation` that can be run to initialize this iterator on the gi
(self, dataset, name=None)
| 315 | raise ValueError("Iterator does not have an initializer.") |
| 316 | |
| 317 | def make_initializer(self, dataset, name=None): |
| 318 | """Returns a `tf.Operation` that initializes this iterator on `dataset`. |
| 319 | |
| 320 | Args: |
| 321 | dataset: A `Dataset` with compatible structure to this iterator. |
| 322 | name: (Optional.) A name for the created operation. |
| 323 | |
| 324 | Returns: |
| 325 | A `tf.Operation` that can be run to initialize this iterator on the given |
| 326 | `dataset`. |
| 327 | |
| 328 | Raises: |
| 329 | TypeError: If `dataset` and this iterator do not have a compatible |
| 330 | element structure. |
| 331 | """ |
| 332 | with ops.name_scope(name, "make_initializer") as name: |
| 333 | # NOTE(mrry): Cannot depend on `dataset_ops.get_legacy_output*()` due |
| 334 | # to that creating a circular dependency. |
| 335 | # pylint: disable=protected-access |
| 336 | dataset_output_types = nest.map_structure( |
| 337 | lambda component_spec: component_spec._to_legacy_output_types(), |
| 338 | dataset.element_spec) |
| 339 | dataset_output_shapes = nest.map_structure( |
| 340 | lambda component_spec: component_spec._to_legacy_output_shapes(), |
| 341 | dataset.element_spec) |
| 342 | dataset_output_classes = nest.map_structure( |
| 343 | lambda component_spec: component_spec._to_legacy_output_classes(), |
| 344 | dataset.element_spec) |
| 345 | # pylint: enable=protected-access |
| 346 | |
| 347 | nest.assert_same_structure(self.output_types, dataset_output_types) |
| 348 | nest.assert_same_structure(self.output_shapes, dataset_output_shapes) |
| 349 | for iterator_class, dataset_class in zip( |
| 350 | nest.flatten(self.output_classes), |
| 351 | nest.flatten(dataset_output_classes)): |
| 352 | if iterator_class is not dataset_class: |
| 353 | raise TypeError( |
| 354 | "Expected output classes %r but got dataset with output class %r." |
| 355 | % (self.output_classes, dataset_output_classes)) |
| 356 | for iterator_dtype, dataset_dtype in zip( |
| 357 | nest.flatten(self.output_types), nest.flatten(dataset_output_types)): |
| 358 | if iterator_dtype != dataset_dtype: |
| 359 | raise TypeError( |
| 360 | "Expected output types %r but got dataset with output types %r." % |
| 361 | (self.output_types, dataset_output_types)) |
| 362 | for iterator_shape, dataset_shape in zip( |
| 363 | nest.flatten(self.output_shapes), nest.flatten( |
| 364 | dataset_output_shapes)): |
| 365 | if not iterator_shape.is_compatible_with(dataset_shape): |
| 366 | raise TypeError("Expected output shapes compatible with %r but got " |
| 367 | "dataset with output shapes %r." % |
| 368 | (self.output_shapes, dataset_output_shapes)) |
| 369 | |
| 370 | with ops.device(self._iterator_resource.device): |
| 371 | # pylint: disable=protected-access |
| 372 | return gen_dataset_ops.make_iterator( |
| 373 | dataset._variant_tensor, self._iterator_resource, name=name) |
| 374 |