larroy commented on a change in pull request #15413: [MXNET-978] Higher Order
Gradient Support `reciprocal`, `abs`.
URL: https://github.com/apache/incubator-mxnet/pull/15413#discussion_r300196827
##########
File path: src/operator/tensor/elemwise_unary_op_basic.cc
##########
@@ -717,7 +717,38 @@ Example::
MXNET_OPERATOR_REGISTER_BINARY(_backward_reciprocal)
.set_attr<FCompute>("FCompute<cpu>",
- ElemwiseBinaryOp::Compute<cpu, unary_bwd<mshadow_op::reciprocal_grad> >);
+ ElemwiseBinaryOp::Compute<cpu, unary_bwd<mshadow_op::reciprocal_grad> >)
+.set_attr<nnvm::FGradient>("FGradient",
+ [](const nnvm::NodePtr& n, const std::vector<nnvm::NodeEntry>& ograds) {
+ // ograds[0]: dL/dxgrad
+ // inputs[0]: dL/dy
+ // inputs[1]: x
+ // f(x) = y = 1/x
+ // f'(x) = -1/x^2
+ // f''(x) = 2/x^3 = -2 * (f'(x) * f(x))
+
+ const std::unordered_map<std::string, std::string> args = {{"scalar",
"-2.0"}};
+
+ auto dydx_mul_dldy = nnvm::NodeEntry{n}; // f'(x) * head_grads
+ auto dydx = MakeNode("elemwise_div", n->attrs.name + "_dydx",
+ {dydx_mul_dldy, n->inputs[0]}, nullptr, &n);
+ auto fx = MakeNode("reciprocal", n->attrs.name + "_fx",
Review comment:
Small thing, Could we get fx from the first backward (node->inputs) if we do
ElemwiseGradUseInOut ? I guess we would avoid additional divisions if so.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services