From 16e18150d85c5315c79859138c293ecd101fbe0c Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Sun, 19 May 2019 02:51:49 +0530 Subject: [PATCH] add gradient on --- .../tensor/elemwise_unary_op_basic.cc | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/operator/tensor/elemwise_unary_op_basic.cc b/src/operator/tensor/elemwise_unary_op_basic.cc index bcd9962834e5..d5e715f80252 100644 --- a/src/operator/tensor/elemwise_unary_op_basic.cc +++ b/src/operator/tensor/elemwise_unary_op_basic.cc @@ -989,16 +989,7 @@ The storage type of ``log`` output is always dense )code" ADD_FILELINE) .set_attr("FCompute", UnaryOp::LogCompute) -.set_attr("FGradient", - [](const nnvm::NodePtr& n, const std::vector& ograds) { - auto x_grad = MakeNode("reciprocal", n->attrs.name + "_mid_x_grad", - {n->inputs[0]}, nullptr, &n); - auto in_grad = MakeNode("elemwise_mul", n->attrs.name + "_backward", - {ograds[0], nnvm::NodeEntry{x_grad, 0, 0}}, nullptr, &n); - std::vector ret; - ret.emplace_back(nnvm::NodeEntry{in_grad, 0, 0}); - return ret; - }); +.set_attr("FGradient", ElemwiseGradUseIn{"_backward_log"}); // log10 MXNET_OPERATOR_REGISTER_UNARY_WITH_SPARSE_DR(log10, cpu, mshadow_op::log10) @@ -1025,7 +1016,28 @@ The storage type of ``log2`` output is always dense .set_attr("FGradient", ElemwiseGradUseIn{"_backward_log2"}); MXNET_OPERATOR_REGISTER_BINARY_WITH_SPARSE_CPU_DR(_backward_log, - unary_bwd); + unary_bwd) +.set_attr("FGradient", + [](const nnvm::NodePtr& n, const std::vector& ograds) { + // For g(x) -> g = log + // g''(x) = -1 * (g'(x) * g'(x)) + auto gx = nnvm::NodeEntry{n, 0, 0}; + auto ggx_mid = MakeNode("elemwise_mul", n->attrs.name + "_backward_mid_grad_grad", + {gx, gx}, nullptr, &n); + auto ggx = MakeNode("negative", n->attrs.name + "_backward_grad_grad", + {nnvm::NodeEntry{ggx_mid, 0, 0}}, nullptr, &n); + auto gxx_chain = MakeNode("elemwise_mul", n->attrs.name + "_backward_grad_grad_inp", + {ograds[0], nnvm::NodeEntry{ggx, 0, 0}}, nullptr, &n); + + // grad + auto ggrad_chain = MakeNode("elemwise_mul", n->attrs.name + "_backward_grad_grad", + {ograds[0], gx}, nullptr, &n); + + std::vector ret; + ret.emplace_back(nnvm::NodeEntry{ggrad_chain, 0, 0}); + ret.emplace_back(nnvm::NodeEntry{gxx_chain, 0, 0}); + return ret; + }); MXNET_OPERATOR_REGISTER_BINARY_WITH_SPARSE_CPU_DR(_backward_log10, unary_bwd);