• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from tensorflow.python.data.util import structure
22from tensorflow.python.ops import gen_experimental_dataset_ops
23
24
25def at(dataset, index):
26  """Returns the element at a specific index in a datasest.
27
28  Args:
29    dataset: A `tf.data.Dataset` to determine whether it supports random access.
30    index: The index at which to fetch the element.
31
32  Returns:
33      A (nested) structure of values matching `tf.data.Dataset.element_spec`.
34
35   Raises:
36     UnimplementedError: If random access is not yet supported for a dataset.
37  """
38  # pylint: disable=protected-access
39  return structure.from_tensor_list(
40      dataset.element_spec,
41      gen_experimental_dataset_ops.get_element_at_index(
42          dataset,
43          index,
44          output_types=dataset._flat_types,
45          output_shapes=dataset._flat_shapes))
46