From cc6c33b8b221894a5b0b8b3533803c085bb70610 Mon Sep 17 00:00:00 2001 From: Qiao Longfei Date: Thu, 3 Aug 2017 06:36:44 +0800 Subject: [PATCH] export Backward to python (#3174) * export Backward to python --- paddle/operators/add_op.cc | 4 ---- paddle/pybind/CMakeLists.txt | 2 +- paddle/pybind/pybind.cc | 12 ++++++++++++ .../paddle/v2/framework/tests/test_add_two_op.py | 15 ++++++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/paddle/operators/add_op.cc b/paddle/operators/add_op.cc index 3a43dbfba..85269a5f7 100644 --- a/paddle/operators/add_op.cc +++ b/paddle/operators/add_op.cc @@ -50,10 +50,6 @@ The equation is: Out = X + Y class AddOpGrad : public OperatorWithKernel { protected: void InferShape(const InferShapeContext &ctx) const override {} - std::string DebugString() const override { - LOG(INFO) << "AddOpGrad"; - return ""; - } }; } // namespace operators diff --git a/paddle/pybind/CMakeLists.txt b/paddle/pybind/CMakeLists.txt index 845589dcb..ac12b504b 100644 --- a/paddle/pybind/CMakeLists.txt +++ b/paddle/pybind/CMakeLists.txt @@ -1,6 +1,6 @@ cc_library(paddle_pybind SHARED SRCS pybind.cc - DEPS pybind python + DEPS pybind python backward fc_op sgd_op add_op diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index d3cde07bd..40ff16449 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -16,6 +16,7 @@ limitations under the License. */ #include #include +#include "paddle/framework/backward.h" #include "paddle/framework/net.h" #include "paddle/framework/op_registry.h" #include "paddle/framework/operator.h" @@ -45,6 +46,10 @@ template void ExposeOperator(ClassType& m) { m.def("infer_shape", &ClassType::type::InferShape) .def("run", &ClassType::type::Run) + .def("type", + [](const typename ClassType::type& op) -> std::string { + return op.type_; + }) .def("outputs", [](const typename ClassType::type& op) -> std::vector { return op.outputs_; @@ -192,6 +197,13 @@ All parameter, weight, gradient are variables in Paddle. desc.InitializationErrorString()); return pd::OpRegistry::CreateOp(desc); }); + + operator_base.def("backward", + [](const pd::OperatorBase& forwardOp, + const std::unordered_set& no_grad_vars) { + return pd::Backward(forwardOp, no_grad_vars); + }); + ExposeOperator(operator_base); py::class_> net(m, "Net"); diff --git a/python/paddle/v2/framework/tests/test_add_two_op.py b/python/paddle/v2/framework/tests/test_add_two_op.py index 73b373490..6e6643201 100644 --- a/python/paddle/v2/framework/tests/test_add_two_op.py +++ b/python/paddle/v2/framework/tests/test_add_two_op.py @@ -1,6 +1,10 @@ import unittest -from op_test_util import OpTestMeta + import numpy +import paddle.v2.framework.core as core +import paddle.v2.framework.create_op_creation_methods as creation + +from op_test_util import OpTestMeta class TestAddOp(unittest.TestCase): @@ -13,5 +17,14 @@ class TestAddOp(unittest.TestCase): self.Out = self.X + self.Y +class TestAddGradOp(unittest.TestCase): + def test_add_grad(self): + op = creation.op_creations.add_two(X="X", Y="Y", Out="Out") + backward_op = core.Operator.backward(op, set()) + self.assertEqual(backward_op.type(), "add_two_grad") + expected = '''Op(add_two_grad), inputs:(X, Y, Out, Out@GRAD), outputs:(X@GRAD, Y@GRAD).''' + self.assertEqual(expected, str(backward_op)) + + if __name__ == '__main__': unittest.main() -- GitLab