Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/ops/histogram_ops.py: 53%

32 statements  

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

1# Copyright 2015 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# pylint: disable=g-short-docstring-punctuation 

16"""Histograms. 

17""" 

18 

19from tensorflow.python.framework import dtypes 

20from tensorflow.python.framework import ops 

21from tensorflow.python.ops import array_ops 

22from tensorflow.python.ops import clip_ops 

23from tensorflow.python.ops import control_flow_assert 

24from tensorflow.python.ops import control_flow_ops 

25from tensorflow.python.ops import gen_math_ops 

26from tensorflow.python.ops import math_ops 

27from tensorflow.python.util import dispatch 

28from tensorflow.python.util.tf_export import tf_export 

29 

30 

31@tf_export('histogram_fixed_width_bins') 

32@dispatch.add_dispatch_support 

33def histogram_fixed_width_bins(values, 

34 value_range, 

35 nbins=100, 

36 dtype=dtypes.int32, 

37 name=None): 

38 """Bins the given values for use in a histogram. 

39 

40 Given the tensor `values`, this operation returns a rank 1 `Tensor` 

41 representing the indices of a histogram into which each element 

42 of `values` would be binned. The bins are equal width and 

43 determined by the arguments `value_range` and `nbins`. 

44 

45 Args: 

46 values: Numeric `Tensor`. 

47 value_range: Shape [2] `Tensor` of same `dtype` as `values`. 

48 values <= value_range[0] will be mapped to hist[0], 

49 values >= value_range[1] will be mapped to hist[-1]. 

50 nbins: Scalar `int32 Tensor`. Number of histogram bins. 

51 dtype: dtype for returned histogram. 

52 name: A name for this operation (defaults to 'histogram_fixed_width'). 

53 

54 Returns: 

55 A `Tensor` holding the indices of the binned values whose shape matches 

56 `values`. 

57 

58 Raises: 

59 TypeError: If any unsupported dtype is provided. 

60 tf.errors.InvalidArgumentError: If value_range does not 

61 satisfy value_range[0] < value_range[1]. 

62 

63 Examples: 

64 

65 >>> # Bins will be: (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) 

66 ... 

67 >>> nbins = 5 

68 >>> value_range = [0.0, 5.0] 

69 >>> new_values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] 

70 >>> indices = tf.histogram_fixed_width_bins(new_values, value_range, nbins=5) 

71 >>> indices.numpy() 

72 array([0, 0, 1, 2, 4, 4], dtype=int32) 

73 """ 

74 with ops.name_scope(name, 'histogram_fixed_width_bins', 

75 [values, value_range, nbins]): 

76 values = ops.convert_to_tensor(values, name='values') 

77 shape = array_ops.shape(values) 

78 

79 values = array_ops.reshape(values, [-1]) 

80 value_range = ops.convert_to_tensor(value_range, name='value_range') 

81 nbins = ops.convert_to_tensor(nbins, dtype=dtypes.int32, name='nbins') 

82 check = control_flow_assert.Assert( 

83 math_ops.greater(nbins, 0), ['nbins %s must > 0' % nbins]) 

84 nbins = control_flow_ops.with_dependencies([check], nbins) 

85 nbins_float = math_ops.cast(nbins, values.dtype) 

86 

87 # Map tensor values that fall within value_range to [0, 1]. 

88 scaled_values = math_ops.truediv( 

89 values - value_range[0], 

90 value_range[1] - value_range[0], 

91 name='scaled_values') 

92 

93 # map tensor values within the open interval value_range to {0,.., nbins-1}, 

94 # values outside the open interval will be zero or less, or nbins or more. 

95 indices = math_ops.floor(nbins_float * scaled_values, name='indices') 

96 

97 # Clip edge cases (e.g. value = value_range[1]) or "outliers." 

98 indices = math_ops.cast( 

99 clip_ops.clip_by_value(indices, 0, nbins_float - 1), dtypes.int32) 

100 return array_ops.reshape(indices, shape) 

101 

102 

103@tf_export('histogram_fixed_width') 

104@dispatch.add_dispatch_support 

105def histogram_fixed_width(values, 

106 value_range, 

107 nbins=100, 

108 dtype=dtypes.int32, 

109 name=None): 

110 """Return histogram of values. 

111 

112 Given the tensor `values`, this operation returns a rank 1 histogram counting 

113 the number of entries in `values` that fell into every bin. The bins are 

114 equal width and determined by the arguments `value_range` and `nbins`. 

115 

116 Args: 

117 values: Numeric `Tensor`. 

118 value_range: Shape [2] `Tensor` of same `dtype` as `values`. 

119 values <= value_range[0] will be mapped to hist[0], 

120 values >= value_range[1] will be mapped to hist[-1]. 

121 nbins: Scalar `int32 Tensor`. Number of histogram bins. 

122 dtype: dtype for returned histogram. 

123 name: A name for this operation (defaults to 'histogram_fixed_width'). 

124 

125 Returns: 

126 A 1-D `Tensor` holding histogram of values. 

127 

128 Raises: 

129 TypeError: If any unsupported dtype is provided. 

130 tf.errors.InvalidArgumentError: If value_range does not 

131 satisfy value_range[0] < value_range[1]. 

132 

133 Examples: 

134 

135 >>> # Bins will be: (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) 

136 ... 

137 >>> nbins = 5 

138 >>> value_range = [0.0, 5.0] 

139 >>> new_values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] 

140 >>> hist = tf.histogram_fixed_width(new_values, value_range, nbins=5) 

141 >>> hist.numpy() 

142 array([2, 1, 1, 0, 2], dtype=int32) 

143 """ 

144 with ops.name_scope(name, 'histogram_fixed_width', 

145 [values, value_range, nbins]) as name: 

146 # pylint: disable=protected-access 

147 return gen_math_ops._histogram_fixed_width( 

148 values, value_range, nbins, dtype=dtype, name=name) 

149 # pylint: enable=protected-access