• 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.data.ops import dataset_ops
21from tensorflow.python.framework import dtypes
22from tensorflow.python.framework import ops
23from tensorflow.python.ops import gen_dataset_ops
24from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
25from tensorflow.python.util.tf_export import tf_export
26
27
28INFINITE = -1
29UNKNOWN = -2
30tf_export("data.experimental.INFINITE_CARDINALITY").export_constant(
31    __name__, "INFINITE")
32tf_export("data.experimental.UNKNOWN_CARDINALITY").export_constant(
33    __name__, "UNKNOWN")
34
35
36# TODO(b/157691652): Deprecate this method after migrating users to the new API.
37@tf_export("data.experimental.cardinality")
38def cardinality(dataset):
39  """Returns the cardinality of `dataset`, if known.
40
41  The operation returns the cardinality of `dataset`. The operation may return
42  `tf.data.experimental.INFINITE_CARDINALITY` if `dataset` contains an infinite
43  number of elements or `tf.data.experimental.UNKNOWN_CARDINALITY` if the
44  analysis fails to determine the number of elements in `dataset` (e.g. when the
45  dataset source is a file).
46
47  >>> dataset = tf.data.Dataset.range(42)
48  >>> print(tf.data.experimental.cardinality(dataset).numpy())
49  42
50  >>> dataset = dataset.repeat()
51  >>> cardinality = tf.data.experimental.cardinality(dataset)
52  >>> print((cardinality == tf.data.experimental.INFINITE_CARDINALITY).numpy())
53  True
54  >>> dataset = dataset.filter(lambda x: True)
55  >>> cardinality = tf.data.experimental.cardinality(dataset)
56  >>> print((cardinality == tf.data.experimental.UNKNOWN_CARDINALITY).numpy())
57  True
58
59  Args:
60    dataset: A `tf.data.Dataset` for which to determine cardinality.
61
62  Returns:
63    A scalar `tf.int64` `Tensor` representing the cardinality of `dataset`. If
64    the cardinality is infinite or unknown, the operation returns the named
65    constant `INFINITE_CARDINALITY` and `UNKNOWN_CARDINALITY` respectively.
66  """
67
68  return gen_dataset_ops.dataset_cardinality(dataset._variant_tensor)  # pylint: disable=protected-access
69
70
71@tf_export("data.experimental.assert_cardinality")
72def assert_cardinality(expected_cardinality):
73  """Asserts the cardinality of the input dataset.
74
75  NOTE: The following assumes that "examples.tfrecord" contains 42 records.
76
77  >>> dataset = tf.data.TFRecordDataset("examples.tfrecord")
78  >>> cardinality = tf.data.experimental.cardinality(dataset)
79  >>> print((cardinality == tf.data.experimental.UNKNOWN_CARDINALITY).numpy())
80  True
81  >>> dataset = dataset.apply(tf.data.experimental.assert_cardinality(42))
82  >>> print(tf.data.experimental.cardinality(dataset).numpy())
83  42
84
85  Args:
86    expected_cardinality: The expected cardinality of the input dataset.
87
88  Returns:
89    A `Dataset` transformation function, which can be passed to
90    `tf.data.Dataset.apply`.
91
92  Raises:
93    FailedPreconditionError: The assertion is checked at runtime (when iterating
94      the dataset) and an error is raised if the actual and expected cardinality
95      differ.
96  """
97  def _apply_fn(dataset):
98    return _AssertCardinalityDataset(dataset, expected_cardinality)
99
100  return _apply_fn
101
102
103class _AssertCardinalityDataset(dataset_ops.UnaryUnchangedStructureDataset):
104  """A `Dataset` that assert the cardinality of its input."""
105
106  def __init__(self, input_dataset, expected_cardinality):
107    self._input_dataset = input_dataset
108    self._expected_cardinality = ops.convert_to_tensor(
109        expected_cardinality, dtype=dtypes.int64, name="expected_cardinality")
110
111    # pylint: enable=protected-access
112    variant_tensor = ged_ops.assert_cardinality_dataset(
113        self._input_dataset._variant_tensor,  # pylint: disable=protected-access
114        self._expected_cardinality,
115        **self._flat_structure)
116    super(_AssertCardinalityDataset, self).__init__(input_dataset,
117                                                    variant_tensor)
118