From 32c15a291917786ba136b76adb289aaa78527252 Mon Sep 17 00:00:00 2001 From: dongzhihong Date: Tue, 25 Jul 2017 15:00:24 +0800 Subject: [PATCH] "random op test" --- paddle/operators/random_op.h | 1 + paddle/pybind/pybind.cc | 1 + .../v2/framework/tests/test_plain_net.py | 12 ++++---- .../v2/framework/tests/test_random_op.py | 28 +++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 python/paddle/v2/framework/tests/test_random_op.py diff --git a/paddle/operators/random_op.h b/paddle/operators/random_op.h index a82b3afec8..bee6cc9cbd 100644 --- a/paddle/operators/random_op.h +++ b/paddle/operators/random_op.h @@ -6,6 +6,7 @@ namespace paddle { namespace operators { + template bool Gaussian(DeviceContext& ctx, framework::Tensor* output, diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index d48a948d21..f6e9013471 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -36,6 +36,7 @@ USE_OP(mul); USE_OP(sigmoid); USE_OP(softmax); USE_OP(rowwise_add); +USE_OP(random_op); template void ExposeOperator(ClassType& m) { diff --git a/python/paddle/v2/framework/tests/test_plain_net.py b/python/paddle/v2/framework/tests/test_plain_net.py index 2b919aca28..53c8dd6c22 100644 --- a/python/paddle/v2/framework/tests/test_plain_net.py +++ b/python/paddle/v2/framework/tests/test_plain_net.py @@ -16,13 +16,13 @@ class TestNet(unittest.TestCase): net.complete_add_op(True) expected = ''' -Op(plain_net), inputs:(@EMPTY@, X, Y, w), outputs:(@TEMP@fc@0, Out, fc.out). - Op(add_two), inputs:(X, Y), outputs:(Out). - Op(plain_net), inputs:(@EMPTY@, X, w), outputs:(@TEMP@fc@0, fc.out). + Op(plain_net), inputs:(@EMPTY@, X, Y, w), outputs:(@TEMP@fc@0, Out, fc.out). + Op(add_two), inputs:(X, Y), outputs:(Out). + Op(plain_net), inputs:(@EMPTY@, X, w), outputs:(@TEMP@fc@0, fc.out). Op(fc), inputs:(X, w, @EMPTY@), outputs:(fc.out, @TEMP@fc@0). - Op(mul), inputs:(X, w), outputs:(@TEMP@fc@0). - Op(sigmoid), inputs:(@TEMP@fc@0), outputs:(fc.out). -''' + Op(mul), inputs:(X, w), outputs:(@TEMP@fc@0). + Op(sigmoid), inputs:(@TEMP@fc@0), outputs:(fc.out). + ''' self.assertEqual(expected, "\n" + str(net)) diff --git a/python/paddle/v2/framework/tests/test_random_op.py b/python/paddle/v2/framework/tests/test_random_op.py new file mode 100644 index 0000000000..eb69f35edf --- /dev/null +++ b/python/paddle/v2/framework/tests/test_random_op.py @@ -0,0 +1,28 @@ +import unittest +import paddle.v2.framework.create_op_creation_methods as creation +import paddle.v2.framework.core as core +from op_test_util import OpTestMeta +import numpy + + +class TestRandomOp(unittest.TestCase): + def test_random(self): + scope = core.Scope(None) + # Out = scope.create_var("Out") + op = creation.op_creations.random( + shape=[1000, 1000], mean=5.0, std=1.0, seed=1701, Out="Out") + for out in op.outputs(): + if scope.get_var(out) is None: + scope.create_var(out).get_tensor() + + tensor = scope.get_var("Y").get_tensor() + op.infer_shape(scope) + self.assertEqual([1000, 1000], tensor.shape()) + ctx = core.DeviceContext.cpu_context() + op.run(scope, ctx) + self.assertAlmostEqual(numpy.std(tensor), 1.0) + self.assertAlmostEqual(numpy.mean(tensor), 5.0) + + +if __name__ == '__main__': + unittest.main() -- GitLab