|
|
|
@ -1,91 +1,88 @@
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
from gradient_checker import GradientChecker, create_op
|
|
|
|
|
from op_test_util import OpTestMeta
|
|
|
|
|
from paddle.v2.framework.op import Operator
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSumOp(unittest.TestCase):
|
|
|
|
|
__metaclass__ = OpTestMeta
|
|
|
|
|
|
|
|
|
|
class TestSumOp(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.type = "reduce_sum"
|
|
|
|
|
self.op_type = "reduce_sum"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': -2}
|
|
|
|
|
out = self.inputs['X'].sum(axis=self.attrs['dim'])
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
class TestSumGradOp(GradientChecker):
|
|
|
|
|
def test_normal(self):
|
|
|
|
|
op = Operator("reduce_sum", X="X", Out="Out", dim=-2)
|
|
|
|
|
# use small size to decrease the error of numerical calculation
|
|
|
|
|
inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.check_grad(op, inputs, set(["X"]), "Out")
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
def test_1d_tensor(self):
|
|
|
|
|
op = Operator("reduce_sum", X="X", Out="Out", dim=0)
|
|
|
|
|
# use small size to decrease the error of numerical calculation
|
|
|
|
|
inputs = {'X': np.random.random(10).astype("float32")}
|
|
|
|
|
self.check_grad(op, inputs, set(["X"]), "Out")
|
|
|
|
|
|
|
|
|
|
class TestMeanOp(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_mean"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': 1}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].mean(axis=self.attrs['dim'])}
|
|
|
|
|
|
|
|
|
|
class TestKeepdimSumOp(unittest.TestCase):
|
|
|
|
|
__metaclass__ = OpTestMeta
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.type = "reduce_sum"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': -2}
|
|
|
|
|
out = self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMeanOp(unittest.TestCase):
|
|
|
|
|
__metaclass__ = OpTestMeta
|
|
|
|
|
class TestMaxOp(OpTest):
|
|
|
|
|
"""Remove Max with subgradient from gradient check to confirm the success of CI."""
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.type = "reduce_mean"
|
|
|
|
|
self.op_type = "reduce_max"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': -1}
|
|
|
|
|
out = self.inputs['X'].mean(axis=self.attrs['dim'])
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].max(axis=self.attrs['dim'])}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMeanGradOp(GradientChecker):
|
|
|
|
|
def test_normal(self):
|
|
|
|
|
op = Operator("reduce_mean", X="X", Out="Out", dim=-2)
|
|
|
|
|
# use small size to decrease the error of numerical calculation
|
|
|
|
|
inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.check_grad(op, inputs, set(["X"]), "Out")
|
|
|
|
|
class TestMinOp(OpTest):
|
|
|
|
|
"""Remove Min with subgradient from gradient check to confirm the success of CI."""
|
|
|
|
|
|
|
|
|
|
def test_1d_tensor(self):
|
|
|
|
|
op = Operator("reduce_mean", X="X", Out="Out", dim=0)
|
|
|
|
|
# use small size to decrease the error of numerical calculation
|
|
|
|
|
inputs = {'X': np.random.random(10).astype("float32")}
|
|
|
|
|
self.check_grad(op, inputs, set(["X"]), "Out")
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op_type = "reduce_min"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': 2}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].min(axis=self.attrs['dim'])}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
class TestMaxOp(unittest.TestCase):
|
|
|
|
|
__metaclass__ = OpTestMeta
|
|
|
|
|
|
|
|
|
|
class TestKeepDimReduce(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.type = "reduce_max"
|
|
|
|
|
self.op_type = "reduce_sum"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': -1}
|
|
|
|
|
out = self.inputs['X'].max(axis=self.attrs['dim'])
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
self.attrs = {'dim': -2, 'keep_dim': 1}
|
|
|
|
|
self.outputs = {
|
|
|
|
|
'Out': self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
class TestMinOp(unittest.TestCase):
|
|
|
|
|
__metaclass__ = OpTestMeta
|
|
|
|
|
|
|
|
|
|
class Test1DReduce(OpTest):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.type = "reduce_max"
|
|
|
|
|
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
|
|
|
|
|
self.attrs = {'dim': -2}
|
|
|
|
|
out = self.inputs['X'].min(axis=self.attrs['dim'])
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
self.op_type = "reduce_sum"
|
|
|
|
|
self.inputs = {'X': np.random.random(20).astype("float32")}
|
|
|
|
|
self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|