Skip to content

export Backward to python #3174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions paddle/operators/add_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion paddle/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions paddle/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */
#include <fstream>
#include <vector>

#include "paddle/framework/backward.h"
#include "paddle/framework/net.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
Expand Down Expand Up @@ -43,6 +44,10 @@ template <typename ClassType>
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<std::string> {
return op.outputs_;
Expand Down Expand Up @@ -154,6 +159,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<std::string>& no_grad_vars) {
return pd::Backward(forwardOp, no_grad_vars);
});

ExposeOperator(operator_base);

py::class_<pd::NetOp, std::shared_ptr<pd::NetOp>> net(m, "Net");
Expand Down
15 changes: 14 additions & 1 deletion python/paddle/v2/framework/tests/test_add_two_op.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -13,5 +17,14 @@ def setUp(self):
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()