Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/counter.py: 83%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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 tensorflow.python import tf2 

17from tensorflow.python.data.ops import dataset_ops 

18from tensorflow.python.framework import dtypes 

19from tensorflow.python.util import deprecation 

20from tensorflow.python.util.tf_export import tf_export 

21 

22 

23@tf_export("data.experimental.Counter", v1=[]) 

24@deprecation.deprecated(None, "Use `tf.data.Dataset.counter(...)` instead.") 

25def CounterV2(start=0, step=1, dtype=dtypes.int64): 

26 """Creates a `Dataset` that counts from `start` in steps of size `step`. 

27 

28 Unlike `tf.data.Dataset.range` which will stop at some ending number, 

29 `Counter` will produce elements indefinitely. 

30 

31 >>> dataset = tf.data.experimental.Counter().take(5) 

32 >>> list(dataset.as_numpy_iterator()) 

33 [0, 1, 2, 3, 4] 

34 >>> dataset.element_spec 

35 TensorSpec(shape=(), dtype=tf.int64, name=None) 

36 >>> dataset = tf.data.experimental.Counter(dtype=tf.int32) 

37 >>> dataset.element_spec 

38 TensorSpec(shape=(), dtype=tf.int32, name=None) 

39 >>> dataset = tf.data.experimental.Counter(start=2).take(5) 

40 >>> list(dataset.as_numpy_iterator()) 

41 [2, 3, 4, 5, 6] 

42 >>> dataset = tf.data.experimental.Counter(start=2, step=5).take(5) 

43 >>> list(dataset.as_numpy_iterator()) 

44 [2, 7, 12, 17, 22] 

45 >>> dataset = tf.data.experimental.Counter(start=10, step=-1).take(5) 

46 >>> list(dataset.as_numpy_iterator()) 

47 [10, 9, 8, 7, 6] 

48 

49 Args: 

50 start: (Optional.) The starting value for the counter. Defaults to 0. 

51 step: (Optional.) The step size for the counter. Defaults to 1. 

52 dtype: (Optional.) The data type for counter elements. Defaults to 

53 `tf.int64`. 

54 

55 Returns: 

56 A `Dataset` of scalar `dtype` elements. 

57 """ 

58 return dataset_ops.Dataset.counter(start, step, dtype) 

59 

60 

61@tf_export(v1=["data.experimental.Counter"]) 

62@deprecation.deprecated(None, "Use `tf.data.Dataset.counter(...)` instead.") 

63def CounterV1(start=0, step=1, dtype=dtypes.int64): 

64 return dataset_ops.DatasetV1Adapter(CounterV2(start, step, dtype)) 

65 

66 

67CounterV1.__doc__ = CounterV2.__doc__ 

68 

69if tf2.enabled(): 

70 Counter = CounterV2 

71else: 

72 Counter = CounterV1