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