On this page
tf.data.experimental.get_single_element
Returns the single element in dataset as a nested structure of tensors.
tf.data.experimental.get_single_element(
    dataset
)
This function enables you to use a tf.data.Dataset in a stateless "tensor-in tensor-out" expression, without creating an iterator. This can be useful when your preprocessing transformations are expressed as a Dataset, and you want to use the transformation at serving time.
For example:
def preprocessing_fn(input_str):
  # ...
  return image, label
input_batch = ...  # input batch of BATCH_SIZE elements
dataset = (tf.data.Dataset.from_tensor_slices(input_batch)
           .map(preprocessing_fn, num_parallel_calls=BATCH_SIZE)
           .batch(BATCH_SIZE))
image_batch, label_batch = tf.data.experimental.get_single_element(dataset)
| Args | |
|---|---|
| dataset | A tf.data.Datasetobject containing a single element. | 
| Returns | |
|---|---|
| A nested structure of tf.Tensorobjects, corresponding to the single element ofdataset. | 
| Raises | |
|---|---|
| TypeError | if datasetis not atf.data.Datasetobject. InvalidArgumentError (at runtime): ifdatasetdoes not contain exactly one element. | 
© 2020 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/versions/r2.4/api_docs/python/tf/data/experimental/get_single_element