|
|
@ -77,68 +77,5 @@ class TestXPUMeanOp(TestMeanOp):
|
|
|
|
self.check_grad_with_place(place, ['X'], 'Out')
|
|
|
|
self.check_grad_with_place(place, ['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMeanAPI(unittest.TestCase):
|
|
|
|
|
|
|
|
# test paddle.tensor.stat.mean
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
self.x_shape = [2, 3, 4, 5]
|
|
|
|
|
|
|
|
self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
|
|
|
|
|
|
|
|
self.place = paddle.XPUPlace(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_api_static(self):
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
with paddle.static.program_guard(paddle.static.Program()):
|
|
|
|
|
|
|
|
x = paddle.fluid.data('X', self.x_shape)
|
|
|
|
|
|
|
|
out1 = paddle.mean(x)
|
|
|
|
|
|
|
|
out2 = paddle.tensor.mean(x)
|
|
|
|
|
|
|
|
out3 = paddle.tensor.stat.mean(x)
|
|
|
|
|
|
|
|
axis = np.arange(len(self.x_shape)).tolist()
|
|
|
|
|
|
|
|
out4 = paddle.mean(x, axis)
|
|
|
|
|
|
|
|
out5 = paddle.mean(x, tuple(axis))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exe = paddle.static.Executor(self.place)
|
|
|
|
|
|
|
|
res = exe.run(feed={'X': self.x},
|
|
|
|
|
|
|
|
fetch_list=[out1, out2, out3, out4, out5])
|
|
|
|
|
|
|
|
out_ref = np.mean(self.x)
|
|
|
|
|
|
|
|
for out in res:
|
|
|
|
|
|
|
|
self.assertEqual(np.allclose(out, out_ref, rtol=1e-04), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_api_dygraph(self):
|
|
|
|
|
|
|
|
paddle.disable_static(self.place)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_case(x, axis=None, keepdim=False):
|
|
|
|
|
|
|
|
x_tensor = paddle.to_tensor(x)
|
|
|
|
|
|
|
|
out = paddle.mean(x_tensor, axis, keepdim)
|
|
|
|
|
|
|
|
if isinstance(axis, list):
|
|
|
|
|
|
|
|
axis = tuple(axis)
|
|
|
|
|
|
|
|
if len(axis) == 0:
|
|
|
|
|
|
|
|
axis = None
|
|
|
|
|
|
|
|
out_ref = np.mean(x, axis, keepdims=keepdim)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
np.allclose(
|
|
|
|
|
|
|
|
out.numpy(), out_ref, rtol=1e-04), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_case(self.x)
|
|
|
|
|
|
|
|
test_case(self.x, [])
|
|
|
|
|
|
|
|
test_case(self.x, -1)
|
|
|
|
|
|
|
|
test_case(self.x, keepdim=True)
|
|
|
|
|
|
|
|
test_case(self.x, 2, keepdim=True)
|
|
|
|
|
|
|
|
test_case(self.x, [0, 2])
|
|
|
|
|
|
|
|
test_case(self.x, (0, 2))
|
|
|
|
|
|
|
|
test_case(self.x, [0, 1, 2, 3])
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
|
|
|
x = np.random.uniform(-1, 1, [10, 12]).astype('float32')
|
|
|
|
|
|
|
|
x = paddle.to_tensor(x)
|
|
|
|
|
|
|
|
self.assertRaises(Exception, paddle.mean, x, -3)
|
|
|
|
|
|
|
|
self.assertRaises(Exception, paddle.mean, x, 2)
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
with paddle.static.program_guard(paddle.static.Program()):
|
|
|
|
|
|
|
|
x = paddle.fluid.data('X', [10, 12], 'int32')
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, paddle.mean, x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|
|
|
|
unittest.main()
|
|
|
|