• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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"""Cardinality analysis of `Dataset` objects."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
21from tensorflow.python.util.tf_export import tf_export
22
23
24INFINITE = -1
25UNKNOWN = -2
26tf_export("data.experimental.INFINITE_CARDINALITY").export_constant(
27    __name__, "INFINITE")
28tf_export("data.experimental.UNKNOWN_CARDINALITY").export_constant(
29    __name__, "UNKNOWN")
30
31
32@tf_export("data.experimental.cardinality")
33def cardinality(dataset):
34  """Returns the cardinality of `dataset`, if known.
35
36  The operation returns the cardinality of `dataset`. The operation may return
37  `tf.data.experimental.INFINITE_CARDINALITY` if `dataset` contains an infinite
38  number of elements or `tf.data.experimental.UNKNOWN_CARDINALITY` if the
39  analysis fails to determine the number of elements in `dataset` (e.g. when the
40  dataset source is a file).
41
42  Args:
43    dataset: A `tf.data.Dataset` for which to determine cardinality.
44
45  Returns:
46    A scalar `tf.int64` `Tensor` representing the cardinality of `dataset`. If
47    the cardinality is infinite or unknown, the operation returns the named
48    constant `INFINITE_CARDINALITY` and `UNKNOWN_CARDINALITY` respectively.
49  """
50
51  return ged_ops.dataset_cardinality(dataset._variant_tensor)  # pylint: disable=protected-access
52