Skip to content
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

Sigmoid backward implementation. #3181

Merged
merged 5 commits into from
Aug 7, 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
8 changes: 4 additions & 4 deletions paddle/operators/sigmoid_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ class SigmoidOpMaker : public OpProtoAndCheckerMaker {

class SigmoidOpGrad : public OperatorWithKernel {
protected:
void InferShape(const InferShapeContext &ctx) const override {}
std::string DebugString() const override {
LOG(INFO) << "SigmoidGrad";
return "";
void InferShape(const InferShapeContext &ctx) const override {
ctx.Output<Tensor>(0)->Resize(ctx.Input<Tensor>(0)->dims());
}
};

Expand All @@ -51,3 +49,5 @@ REGISTER_OP(sigmoid, ops::SigmoidOp, ops::SigmoidOpMaker);
REGISTER_GRADIENT_OP(sigmoid, sigmoid_grad, ops::SigmoidOpGrad);

REGISTER_OP_CPU_KERNEL(sigmoid, ops::SigmoidKernel<ops::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(sigmoid_grad,
ops::SigmoidGradKernel<ops::CPUPlace, float>);
2 changes: 2 additions & 0 deletions paddle/operators/sigmoid_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
#include "paddle/operators/sigmoid_op.h"

REGISTER_OP_GPU_KERNEL(sigmoid, ops::SigmoidKernel<ops::GPUPlace, float>);
REGISTER_OP_GPU_KERNEL(sigmoid_grad,
ops::SigmoidGradKernel<ops::GPUPlace, float>);
19 changes: 19 additions & 0 deletions paddle/operators/sigmoid_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ class SigmoidKernel : public OpKernel {
auto output = context.Output<Tensor>(0);
output->mutable_data<T>(context.GetPlace());

// The clipping is used in Paddle's raw implenmention
auto X = EigenVector<T>::Flatten(*input);
auto Y = EigenVector<T>::Flatten(*output);
auto place = context.GetEigenDevice<Place>();

Y.device(place) = 1.0 / (1.0 + (-1.0 * X).exp());
}
};

template <typename Place, typename T>
class SigmoidGradKernel : public OpKernel {
public:
void Compute(const ExecutionContext& context) const override {
auto Y_t = context.Input<Tensor>("Y");
auto dY_t = context.Input<Tensor>(framework::GradVarName("Y"));
auto dX_t = context.Output<Tensor>(framework::GradVarName("X"));

dX_t->mutable_data<T>(context.GetPlace());

auto dX = EigenVector<T>::Flatten(*dX_t);
auto Y = EigenVector<T>::Flatten(*Y_t);
auto dY = EigenVector<T>::Flatten(*dY_t);
dX.device(context.GetEigenDevice<Place>()) = dY * Y * (1. - Y);
}
};

} // namespace operators
} // namespace paddle
3 changes: 3 additions & 0 deletions python/paddle/v2/framework/tests/test_sigmoid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ def setUp(self):
self.Y = 1 / (1 + np.exp(-self.X))


#class TestSigmoidGradOp(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add USE_OP(sigmoid_grad) in bybind, and test in python side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dzhwinter Thanks, I'll add unit test after the unit test is ready for the backward op.

#TODO(qingqing) add unit test

if __name__ == '__main__':
unittest.main()