apeforest commented on a change in pull request #15288: [MXNET-978] Higher order gradient for sigmoid URL: https://github.com/apache/incubator-mxnet/pull/15288#discussion_r299301226
########## File path: src/operator/tensor/elemwise_unary_op_basic.cc ########## @@ -121,7 +121,30 @@ The storage type of ``sigmoid`` output is always dense .set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseOut{"_backward_sigmoid"}); MXNET_OPERATOR_REGISTER_BINARY_WITH_SPARSE_CPU(_backward_sigmoid, - unary_bwd<mshadow_op::sigmoid_grad>); + unary_bwd<mshadow_op::sigmoid_grad>) +.set_attr<nnvm::FGradient>("FGradient", + [](const nnvm::NodePtr& n, const std::vector<nnvm::NodeEntry>& ograds) { + // n->inputs[0] : y_grad + // n->inputs[1] : f(x) = sigmoid(x) + // ograds[0] : head_grads + // f''(x) = f'(x) * (1 - 2*f(x)) + auto ones = MakeNode("ones_like", n->attrs.name + "_grad_ones", {n->inputs[1]}, nullptr, &n); + const std::unordered_map<std::string, std::string> args = {{"scalar", "2.0"}}; + auto two_y = MakeNode("_mul_scalar", n->attrs.name + "_mul_two", {n->inputs[1]}, &args, &n); + auto one_minus_two_y = MakeNode("elemwise_sub", n->attrs.name + "_grad_sub", + {nnvm::NodeEntry{ones}, nnvm::NodeEntry{two_y}}, nullptr, &n); + auto grad_grad_mid = MakeNode("elemwise_mul", n->attrs.name + "_grad_mul", + {n->inputs[0], nnvm::NodeEntry{one_minus_two_y}}, nullptr, &n); + // when building gradient graph, the backward node of n->inputs[1] will be + // added to the graph again, therefore f`(x) will be multiplied Review comment: @kshitij12345 I have fixed the issue. The result can pass your test now. Please review again. Thanks! ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services