diff --git a/python/paddle/fluid/tests/unittests/test_median.py b/python/paddle/fluid/tests/unittests/test_median.py index a62e722dd049695d34d3d1b1f1cb32ec16535415..1f90faeac018b22d0e7d66379144e75231501375 100644 --- a/python/paddle/fluid/tests/unittests/test_median.py +++ b/python/paddle/fluid/tests/unittests/test_median.py @@ -86,6 +86,7 @@ class TestMedian(unittest.TestCase): x = paddle.arange(12).reshape([3, 4]) self.assertRaises(ValueError, paddle.median, x, 1.0) self.assertRaises(ValueError, paddle.median, x, 2) + self.assertRaises(ValueError, paddle.median, paddle.to_tensor([])) if __name__ == '__main__': diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index e23f28aa76b1b365fe496dfcc54338f40b05500c..cc94aee415541bd92b1c0e565e3a23d3e09a8369 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -406,6 +406,9 @@ def median(x, axis=None, keepdim=False, name=None): if not isinstance(x, Variable): raise TypeError("In median, the input x should be a Tensor.") + if x.size == 0: + raise ValueError("In median, the size of input x should not be 0.") + if len(x.shape) == 0: return x.clone()