• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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"""The Counter Dataset."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20from tensorflow.python import tf2
21from tensorflow.python.data.ops import dataset_ops
22from tensorflow.python.framework import dtypes
23from tensorflow.python.framework import ops
24from tensorflow.python.util.tf_export import tf_export
25
26
27@tf_export("data.experimental.Counter", v1=[])
28def CounterV2(start=0, step=1, dtype=dtypes.int64):
29  """Creates a `Dataset` that counts from `start` in steps of size `step`.
30
31  Unlike `tf.data.Dataset.range` which will stop at some ending number,
32  `Counter` will produce elements indefinitely.
33
34  >>> dataset = tf.data.experimental.Counter().take(5)
35  >>> list(dataset.as_numpy_iterator())
36  [0, 1, 2, 3, 4]
37  >>> dataset.element_spec
38  TensorSpec(shape=(), dtype=tf.int64, name=None)
39  >>> dataset = tf.data.experimental.Counter(dtype=tf.int32)
40  >>> dataset.element_spec
41  TensorSpec(shape=(), dtype=tf.int32, name=None)
42  >>> dataset = tf.data.experimental.Counter(start=2).take(5)
43  >>> list(dataset.as_numpy_iterator())
44  [2, 3, 4, 5, 6]
45  >>> dataset = tf.data.experimental.Counter(start=2, step=5).take(5)
46  >>> list(dataset.as_numpy_iterator())
47  [2, 7, 12, 17, 22]
48  >>> dataset = tf.data.experimental.Counter(start=10, step=-1).take(5)
49  >>> list(dataset.as_numpy_iterator())
50  [10, 9, 8, 7, 6]
51
52  Args:
53    start: (Optional.) The starting value for the counter. Defaults to 0.
54    step: (Optional.) The step size for the counter. Defaults to 1.
55    dtype: (Optional.) The data type for counter elements. Defaults to
56      `tf.int64`.
57
58  Returns:
59    A `Dataset` of scalar `dtype` elements.
60  """
61  with ops.name_scope("counter"):
62    start = ops.convert_to_tensor(start, dtype=dtype, name="start")
63    step = ops.convert_to_tensor(step, dtype=dtype, name="step")
64    return dataset_ops.Dataset.from_tensors(0).repeat(None).scan(
65        start, lambda state, _: (state + step, state))
66
67
68@tf_export(v1=["data.experimental.Counter"])
69def CounterV1(start=0, step=1, dtype=dtypes.int64):
70  return dataset_ops.DatasetV1Adapter(CounterV2(start, step, dtype))
71
72
73CounterV1.__doc__ = CounterV2.__doc__
74
75if tf2.enabled():
76  Counter = CounterV2
77else:
78  Counter = CounterV1
79