tf.data.experimental.Counter
Stay organized with collections
Save and categorize content based on your preferences.
Creates a Dataset
that counts from start
in steps of size step
.
tf.data.experimental.Counter(
start=0, step=1, dtype=tf.dtypes.int64
)
Unlike tf.data.Dataset.range
which will stop at some ending number,
Counter
will produce elements indefinitely.
dataset = tf.data.experimental.Counter().take(5)
list(dataset.as_numpy_iterator())
[0, 1, 2, 3, 4]
dataset.element_spec
TensorSpec(shape=(), dtype=tf.int64, name=None)
dataset = tf.data.experimental.Counter(dtype=tf.int32)
dataset.element_spec
TensorSpec(shape=(), dtype=tf.int32, name=None)
dataset = tf.data.experimental.Counter(start=2).take(5)
list(dataset.as_numpy_iterator())
[2, 3, 4, 5, 6]
dataset = tf.data.experimental.Counter(start=2, step=5).take(5)
list(dataset.as_numpy_iterator())
[2, 7, 12, 17, 22]
dataset = tf.data.experimental.Counter(start=10, step=-1).take(5)
list(dataset.as_numpy_iterator())
[10, 9, 8, 7, 6]
Args |
start
|
(Optional.) The starting value for the counter. Defaults to 0.
|
step
|
(Optional.) The step size for the counter. Defaults to 1.
|
dtype
|
(Optional.) The data type for counter elements. Defaults to
tf.int64 .
|
Returns |
A Dataset of scalar dtype elements.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2021-08-16 UTC.
[null,null,["Last updated 2021-08-16 UTC."],[],[],null,["# tf.data.experimental.Counter\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/data/experimental/Counter) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.6.0/tensorflow/python/data/experimental/ops/counter.py#L28-L66) |\n\nCreates a `Dataset` that counts from `start` in steps of size `step`. \n\n tf.data.experimental.Counter(\n start=0, step=1, dtype=tf.dtypes.int64\n )\n\nUnlike [`tf.data.Dataset.range`](../../../tf/data/Dataset#range) which will stop at some ending number,\n`Counter` will produce elements indefinitely. \n\n dataset = tf.data.experimental.Counter().take(5)\n list(dataset.as_numpy_iterator())\n [0, 1, 2, 3, 4]\n dataset.element_spec\n TensorSpec(shape=(), dtype=tf.int64, name=None)\n dataset = tf.data.experimental.Counter(dtype=tf.int32)\n dataset.element_spec\n TensorSpec(shape=(), dtype=tf.int32, name=None)\n dataset = tf.data.experimental.Counter(start=2).take(5)\n list(dataset.as_numpy_iterator())\n [2, 3, 4, 5, 6]\n dataset = tf.data.experimental.Counter(start=2, step=5).take(5)\n list(dataset.as_numpy_iterator())\n [2, 7, 12, 17, 22]\n dataset = tf.data.experimental.Counter(start=10, step=-1).take(5)\n list(dataset.as_numpy_iterator())\n [10, 9, 8, 7, 6]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|----------------------------------------------------------------------------------------------|\n| `start` | (Optional.) The starting value for the counter. Defaults to 0. |\n| `step` | (Optional.) The step size for the counter. Defaults to 1. |\n| `dtype` | (Optional.) The data type for counter elements. Defaults to [`tf.int64`](../../../tf#int64). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Dataset` of scalar `dtype` elements. ||\n\n\u003cbr /\u003e"]]