Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/random_access.py: 86%
7 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Python API for random indexing into a dataset."""
17from tensorflow.python.data.util import structure
18from tensorflow.python.ops import gen_experimental_dataset_ops
19from tensorflow.python.util.tf_export import tf_export
22@tf_export("data.experimental.at", v1=[])
23def at(dataset, index):
24 """Returns the element at a specific index in a datasest.
26 Currently, random access is supported for the following tf.data operations:
28 - `tf.data.Dataset.from_tensor_slices`,
29 - `tf.data.Dataset.from_tensors`,
30 - `tf.data.Dataset.shuffle`,
31 - `tf.data.Dataset.batch`,
32 - `tf.data.Dataset.shard`,
33 - `tf.data.Dataset.map`,
34 - `tf.data.Dataset.range`,
35 - `tf.data.Dataset.zip`,
36 - `tf.data.Dataset.skip`,
37 - `tf.data.Dataset.repeat`,
38 - `tf.data.Dataset.list_files`,
39 - `tf.data.Dataset.SSTableDataset`,
40 - `tf.data.Dataset.concatenate`,
41 - `tf.data.Dataset.enumerate`,
42 - `tf.data.Dataset.parallel_map`,
43 - `tf.data.Dataset.prefetch`,
44 - `tf.data.Dataset.take`,
45 - `tf.data.Dataset.cache` (in-memory only)
47 Users can use the cache operation to enable random access for any dataset,
48 even one comprised of transformations which are not on this list.
49 E.g., to get the third element of a TFDS dataset:
51 ```python
52 ds = tfds.load("mnist", split="train").cache()
53 elem = tf.data.Dataset.experimental.at(ds, 3)
54 ```
56 Args:
57 dataset: A `tf.data.Dataset` to determine whether it supports random access.
58 index: The index at which to fetch the element.
60 Returns:
61 A (nested) structure of values matching `tf.data.Dataset.element_spec`.
63 Raises:
64 UnimplementedError: If random access is not yet supported for a dataset.
65 """
66 # pylint: disable=protected-access
67 return structure.from_tensor_list(
68 dataset.element_spec,
69 gen_experimental_dataset_ops.get_element_at_index(
70 dataset._variant_tensor,
71 index,
72 output_types=structure.get_flat_tensor_types(dataset.element_spec),
73 output_shapes=structure.get_flat_tensor_shapes(dataset.element_spec)))