|
|
|
|
@ -67,22 +67,6 @@ class TestSumOp6D(OpTest):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMeanOp(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_mean"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float64")}
|
|
|
|
|
self.attrs = {'dim': [1]}
|
|
|
|
|
self.outputs = {
|
|
|
|
|
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@skip_check_grad_ci(
|
|
|
|
|
reason="reduce_max is discontinuous non-derivable function,"
|
|
|
|
|
" its gradient check is not supported by unittest framework.")
|
|
|
|
|
@ -318,21 +302,6 @@ class TestReduceAll(Test1DReduce):
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].sum()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## reduction in multi dims
|
|
|
|
|
class TestReduceMeanOpMultiAxises(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_mean"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float64")}
|
|
|
|
|
self.attrs = {'dim': [1, 2]}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].mean(axis=(1, 2))}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@skip_check_grad_ci(
|
|
|
|
|
reason="reduce_max is discontinuous non-derivable function,"
|
|
|
|
|
" its gradient check is not supported by unittest framework.")
|
|
|
|
|
@ -420,40 +389,6 @@ class TestReduceSumWithNumelOne(OpTest):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReduceMeanWithDimOne(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_mean"
|
|
|
|
|
self.inputs = {'X': np.random.random((100, 1, 1)).astype("float64")}
|
|
|
|
|
self.attrs = {'dim': [1], 'keep_dim': False}
|
|
|
|
|
self.outputs = {
|
|
|
|
|
'Out': self.inputs['X'].mean(
|
|
|
|
|
axis=tuple(self.attrs['dim']), keepdims=False)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReduceMeanWithNumelOne(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_mean"
|
|
|
|
|
self.inputs = {'X': np.random.random((100, 1)).astype("float64")}
|
|
|
|
|
self.attrs = {'dim': [1], 'keep_dim': True}
|
|
|
|
|
self.outputs = {
|
|
|
|
|
'Out': self.inputs['X'].mean(
|
|
|
|
|
axis=tuple(self.attrs['dim']), keepdims=True)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReduceAll(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_sum"
|
|
|
|
|
@ -536,18 +471,6 @@ class TestReduceSumOpError(unittest.TestCase):
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.reduce_sum, x2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReduceMeanOpError(unittest.TestCase):
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
|
# The input type of reduce_mean_op must be Variable.
|
|
|
|
|
x1 = fluid.create_lod_tensor(
|
|
|
|
|
np.array([[-1]]), [[1]], fluid.CPUPlace())
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.reduce_mean, x1)
|
|
|
|
|
# The input dtype of reduce_mean_op must be float32 or float64 or int32 or int64.
|
|
|
|
|
x2 = fluid.layers.data(name='x2', shape=[4], dtype="uint8")
|
|
|
|
|
self.assertRaises(TypeError, fluid.layers.reduce_mean, x2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class API_TestSumOpError(unittest.TestCase):
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
def test_dtype1():
|
|
|
|
|
@ -649,24 +572,5 @@ class API_TestSumOp(unittest.TestCase):
|
|
|
|
|
self.assertTrue((out3 == np.sum(np_x, axis=(0, 1, 2))).all())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class API_TestReduceMeanOp(unittest.TestCase):
|
|
|
|
|
def test_static(self):
|
|
|
|
|
with fluid.program_guard(fluid.Program(), fluid.Program()):
|
|
|
|
|
x = fluid.data("x", shape=[10, 10], dtype="float32")
|
|
|
|
|
out = fluid.layers.reduce_mean(input=x, dim=1)
|
|
|
|
|
place = fluid.CPUPlace()
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
x_np = np.random.rand(10, 10).astype(np.float32)
|
|
|
|
|
res = exe.run(feed={"x": x_np}, fetch_list=[out])
|
|
|
|
|
self.assertEqual(np.allclose(res[0], np.mean(x_np, axis=1)), True)
|
|
|
|
|
|
|
|
|
|
def test_dygraph(self):
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
x_np = np.random.rand(10, 10).astype(np.float32)
|
|
|
|
|
x = fluid.dygraph.to_variable(x_np)
|
|
|
|
|
out = fluid.layers.reduce_mean(input=x, dim=1)
|
|
|
|
|
self.assertEqual(np.allclose(out.numpy(), np.mean(x_np, axis=1)), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|
|
|
|
|
|