From 06acd6d0cb6d93ce75d91a4fc5d3adccb2f94ff1 Mon Sep 17 00:00:00 2001 From: qijun Date: Fri, 21 Jul 2017 14:36:53 +0800 Subject: [PATCH] add unittest for some basic OpKernels --- paddle/operators/sigmoid_op.cc | 2 +- paddle/pybind/pybind.cc | 4 ++++ .../paddle/v2/framework/tests/CMakeLists.txt | 17 +++++++++++--- .../paddle/v2/framework/tests/op_test_util.py | 5 +++- .../paddle/v2/framework/tests/test_mul_op.py | 17 ++++++++++++++ .../v2/framework/tests/test_rowwise_add_op.py | 17 ++++++++++++++ .../v2/framework/tests/test_sigmoid_op.py | 16 +++++++++++++ .../v2/framework/tests/test_softmax_op.py | 23 +++++++++++++++++++ 8 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 python/paddle/v2/framework/tests/test_mul_op.py create mode 100644 python/paddle/v2/framework/tests/test_rowwise_add_op.py create mode 100644 python/paddle/v2/framework/tests/test_sigmoid_op.py create mode 100644 python/paddle/v2/framework/tests/test_softmax_op.py diff --git a/paddle/operators/sigmoid_op.cc b/paddle/operators/sigmoid_op.cc index 589b48ce803..53bf0a4c287 100644 --- a/paddle/operators/sigmoid_op.cc +++ b/paddle/operators/sigmoid_op.cc @@ -34,7 +34,7 @@ public: framework::OpAttrChecker *op_checker) : framework::OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "sigmoid input"); - AddInput("Y", "sigmoid output"); + AddOutput("Y", "sigmoid output"); AddComment("Sigmoid function"); } }; diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index 4db9cc74465..a689092e7e5 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -30,6 +30,10 @@ USE_OP(add_two); USE_OP(onehot_cross_entropy); USE_OP_WITHOUT_KERNEL(fc); USE_OP(sgd); +USE_OP(mul); +USE_OP(sigmoid); +USE_OP(softmax); +USE_OP(rowwise_add); PYBIND11_PLUGIN(core) { py::module m("core", "C++ core of Paddle Paddle"); diff --git a/python/paddle/v2/framework/tests/CMakeLists.txt b/python/paddle/v2/framework/tests/CMakeLists.txt index 01838b40bd1..aa67792ebc2 100644 --- a/python/paddle/v2/framework/tests/CMakeLists.txt +++ b/python/paddle/v2/framework/tests/CMakeLists.txt @@ -1,3 +1,14 @@ -add_python_test(test_framework test_protobuf.py test_scope.py - test_default_scope_funcs.py test_op_creation_methods.py - test_tensor.py test_fc_op.py test_add_two_op.py test_sgd_op.py test_cross_entropy_op.py) +add_python_test(test_framework + test_protobuf.py + test_scope.py + test_default_scope_funcs.py + test_op_creation_methods.py + test_tensor.py + test_fc_op.py + test_add_two_op.py + test_sgd_op.py + test_cross_entropy_op.py + test_mul_op.py + test_sigmoid_op.py + test_softmax_op.py + test_rowwise_add_op.py) diff --git a/python/paddle/v2/framework/tests/op_test_util.py b/python/paddle/v2/framework/tests/op_test_util.py index b1fa12cc89f..7b62313f8ac 100644 --- a/python/paddle/v2/framework/tests/op_test_util.py +++ b/python/paddle/v2/framework/tests/op_test_util.py @@ -56,7 +56,10 @@ class OpTestMeta(type): for out_name in func.all_output_args: actual = numpy.array(scope.get_var(out_name).get_tensor()) expect = getattr(self, out_name) - numpy.testing.assert_almost_equal(actual, expect) + # TODO(qijun) The default decimal is 7, but numpy.dot and eigen.mul + # has some diff, and could not pass unittest. So I set decimal 3 here. + # And I will check this in future. + numpy.testing.assert_almost_equal(actual, expect, decimal=3) obj.test_all = test_all return obj diff --git a/python/paddle/v2/framework/tests/test_mul_op.py b/python/paddle/v2/framework/tests/test_mul_op.py new file mode 100644 index 00000000000..0a87e66cd03 --- /dev/null +++ b/python/paddle/v2/framework/tests/test_mul_op.py @@ -0,0 +1,17 @@ +import unittest +from op_test_util import OpTestMeta +import numpy as np + + +class TestMulOp(unittest.TestCase): + __metaclass__ = OpTestMeta + + def setUp(self): + self.type = "mul" + self.X = np.random.random((32, 784)).astype("float32") + self.Y = np.random.random((784, 100)).astype("float32") + self.Out = np.dot(self.X, self.Y) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_rowwise_add_op.py b/python/paddle/v2/framework/tests/test_rowwise_add_op.py new file mode 100644 index 00000000000..ef1514983c0 --- /dev/null +++ b/python/paddle/v2/framework/tests/test_rowwise_add_op.py @@ -0,0 +1,17 @@ +import unittest +from op_test_util import OpTestMeta +import numpy as np + + +class TestRowwiseAddOp(unittest.TestCase): + __metaclass__ = OpTestMeta + + def setUp(self): + self.type = "rowwise_add" + self.X = np.random.random((32, 784)).astype("float32") + self.b = np.random.random(784).astype("float32") + self.Out = np.add(self.X, self.b) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_sigmoid_op.py b/python/paddle/v2/framework/tests/test_sigmoid_op.py new file mode 100644 index 00000000000..50044a122f1 --- /dev/null +++ b/python/paddle/v2/framework/tests/test_sigmoid_op.py @@ -0,0 +1,16 @@ +import unittest +from op_test_util import OpTestMeta +import numpy as np + + +class TestSigmoidOp(unittest.TestCase): + __metaclass__ = OpTestMeta + + def setUp(self): + self.type = "sigmoid" + self.X = np.random.random((32, 100)).astype("float32") + self.Y = 1 / (1 + np.exp(-self.X)) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_softmax_op.py b/python/paddle/v2/framework/tests/test_softmax_op.py new file mode 100644 index 00000000000..191b698c1cd --- /dev/null +++ b/python/paddle/v2/framework/tests/test_softmax_op.py @@ -0,0 +1,23 @@ +import unittest +from op_test_util import OpTestMeta +import numpy as np + + +def stable_softmax(x): + """Compute the softmax of vector x in a numerically stable way.""" + shiftx = x - np.max(x) + exps = np.exp(shiftx) + return exps / np.sum(exps) + + +class TestSoftmaxOp(unittest.TestCase): + __metaclass__ = OpTestMeta + + def setUp(self): + self.type = "softmax" + self.X = np.random.random((32, 100)).astype("float32") + self.Y = np.apply_along_axis(stable_softmax, 1, self.X) + + +if __name__ == '__main__': + unittest.main() -- GitLab