mirror of
https://github.com/tensorflow/tensorflow.git
synced 2026-10-26 00:09:51 +08:00
Merge pull request #129174 from MaddipatlaChetan24:patch-70
PiperOrigin-RevId: 1005387779
This commit is contained in:
commit
e6f770ac7e
2 files changed
+51
-3
No files matched your search
@@ -831,7 +831,7 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=None): # pylint: d
|
||||
)
|
||||
|
||||
if axis is None:
|
||||
n = array_ops.size(input_tensor)
|
||||
n = array_ops.size(input_tensor, out_type=dtypes.int64)
|
||||
else:
|
||||
# axis can be an int or a sequence of ints.
|
||||
if isinstance(axis, (int, np.integer)):
|
||||
@@ -854,9 +854,25 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=None): # pylint: d
|
||||
axis < 0, axis + array_ops.rank(input_tensor), axis
|
||||
)
|
||||
n = math_ops.reduce_prod(
|
||||
array_ops.gather(array_ops.shape(input_tensor), axis)
|
||||
array_ops.gather(
|
||||
array_ops.shape(input_tensor, out_type=dtypes.int64), axis
|
||||
)
|
||||
)
|
||||
n = math_ops.cast(n - ddof, squared_deviations.dtype)
|
||||
# NumPy clamps the degrees-of-freedom denominator at 0 (and emits a
|
||||
# "Degrees of freedom <= 0" RuntimeWarning) when ddof >= n, rather than
|
||||
# letting it go negative, and it also allows a non-integer `ddof`
|
||||
# (e.g. 1.5). Casting both `n` and `ddof` to `squared_deviations.dtype`
|
||||
# before subtracting avoids a TypeError from mixing an integer `n`
|
||||
# with a float `ddof`, and the `maximum(..., 0)` clamp turns a
|
||||
# would-be negative denominator into 0 (giving NumPy's `inf` on
|
||||
# divide) instead of a finite, wrong, negative "variance". `n` is
|
||||
# computed as int64 above so it doesn't silently overflow on tensors
|
||||
# with more than 2**31 - 1 elements.
|
||||
n = math_ops.cast(n, squared_deviations.dtype)
|
||||
ddof_cast = math_ops.cast(ddof, squared_deviations.dtype)
|
||||
n = math_ops.maximum(
|
||||
n - ddof_cast, math_ops.cast(0, squared_deviations.dtype)
|
||||
)
|
||||
|
||||
return math_ops.divide(squared_deviations, n)
|
||||
|
||||
|
||||
@@ -1011,6 +1011,38 @@ class ArrayMethodsTest(test.TestCase):
|
||||
run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
|
||||
run_test(
|
||||
np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
|
||||
|
||||
# Regression test: ddof >= n (the number of elements reduced over) must
|
||||
# clamp the degrees-of-freedom denominator at 0 and match NumPy's `inf`,
|
||||
# not silently divide by a negative count and return a wrong, finite,
|
||||
# negative "variance". Also covers a non-integer ddof, which NumPy
|
||||
# allows but which requires casting n to a float dtype before
|
||||
# subtracting.
|
||||
run_test([1.0, 2.0, 3.0], ddof=3)
|
||||
run_test([1.0, 2.0, 3.0], ddof=4)
|
||||
run_test([1.0, 2.0, 3.0], ddof=1.5)
|
||||
run_test([], ddof=1)
|
||||
run_test([5.0], ddof=1)
|
||||
run_test([1.0, 2.0, 3.0], ddof=-1)
|
||||
run_test(np.array([1.0 + 2.0j, 3.0 + 4.0j], dtype=np.complex64), ddof=2)
|
||||
run_test([[1.0, 2.0], [3.0, 4.0]], axis=-1, ddof=2, keepdims=True)
|
||||
run_test(np.arange(8.0).reshape((2, 2, 2)).tolist(), axis=0, ddof=2)
|
||||
run_test(np.arange(8.0).reshape((2, 2, 2)).tolist(), axis=(0, 2), ddof=4)
|
||||
|
||||
# The same clamp must also apply under graph tracing with a dynamically
|
||||
# shaped input, where `n` is computed by `array_ops.shape` at runtime
|
||||
# rather than resolved statically.
|
||||
@def_function.function(
|
||||
input_signature=[
|
||||
tensor_spec.TensorSpec(shape=[None], dtype=dtypes.float32)
|
||||
]
|
||||
)
|
||||
def dynamic_var(x):
|
||||
return np_array_ops.var(x, ddof=3)
|
||||
|
||||
x_dyn = np.array([1.0, 2.0, 3.0], dtype=np.float32)
|
||||
self.match(dynamic_var(x_dyn), np.var(x_dyn, ddof=3))
|
||||
|
||||
self.assertRaises(ValueError, np_array_ops.var, np.ones([2, 2]), out=[])
|
||||
out_of_bounds = 'out of bounds|not in|Invalid reduction dimension'
|
||||
for axis in (-3, (-3, 0), 3, (0, 3)):
|
||||
|
||||
Reference in new issue
Block a user