Returns a nested structure of `tf.Tensor`s representing the next element. In graph mode, you should typically call this method *once* and use its result as the input to another computation. A typical loop will then call `tf.Session.run` on the result of that computation. The loop will t
(self, name=None)
| 373 | dataset._variant_tensor, self._iterator_resource, name=name) |
| 374 | |
| 375 | def get_next(self, name=None): |
| 376 | """Returns a nested structure of `tf.Tensor`s representing the next element. |
| 377 | |
| 378 | In graph mode, you should typically call this method *once* and use its |
| 379 | result as the input to another computation. A typical loop will then call |
| 380 | `tf.Session.run` on the result of that computation. The loop will terminate |
| 381 | when the `Iterator.get_next()` operation raises |
| 382 | `tf.errors.OutOfRangeError`. The following skeleton shows how to use |
| 383 | this method when building a training loop: |
| 384 | |
| 385 | ```python |
| 386 | dataset = ... # A `tf.data.Dataset` object. |
| 387 | iterator = dataset.make_initializable_iterator() |
| 388 | next_element = iterator.get_next() |
| 389 | |
| 390 | # Build a TensorFlow graph that does something with each element. |
| 391 | loss = model_function(next_element) |
| 392 | optimizer = ... # A `tf.compat.v1.train.Optimizer` object. |
| 393 | train_op = optimizer.minimize(loss) |
| 394 | |
| 395 | with tf.compat.v1.Session() as sess: |
| 396 | try: |
| 397 | while True: |
| 398 | sess.run(train_op) |
| 399 | except tf.errors.OutOfRangeError: |
| 400 | pass |
| 401 | ``` |
| 402 | |
| 403 | NOTE: It is legitimate to call `Iterator.get_next()` multiple times, e.g. |
| 404 | when you are distributing different elements to multiple devices in a single |
| 405 | step. However, a common pitfall arises when users call `Iterator.get_next()` |
| 406 | in each iteration of their training loop. `Iterator.get_next()` adds ops to |
| 407 | the graph, and executing each op allocates resources (including threads); as |
| 408 | a consequence, invoking it in every iteration of a training loop causes |
| 409 | slowdown and eventual resource exhaustion. To guard against this outcome, we |
| 410 | log a warning when the number of uses crosses a fixed threshold of |
| 411 | suspiciousness. |
| 412 | |
| 413 | Args: |
| 414 | name: (Optional.) A name for the created operation. |
| 415 | |
| 416 | Returns: |
| 417 | A nested structure of `tf.Tensor` objects. |
| 418 | """ |
| 419 | self._get_next_call_count += 1 |
| 420 | if self._get_next_call_count > GET_NEXT_CALL_WARNING_THRESHOLD: |
| 421 | warnings.warn(GET_NEXT_CALL_WARNING_MESSAGE) |
| 422 | |
| 423 | with ops.device(self._iterator_resource.device): |
| 424 | # pylint: disable=protected-access |
| 425 | flat_ret = gen_dataset_ops.iterator_get_next( |
| 426 | self._iterator_resource, |
| 427 | output_types=self._flat_tensor_types, |
| 428 | output_shapes=self._flat_tensor_shapes, |
| 429 | name=name) |
| 430 | return structure.from_tensor_list(self._element_spec, flat_ret) |
| 431 | |
| 432 | def get_next_as_optional(self): |