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_r300196947
##########
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",
+ {n->inputs[1]}, nullptr, &n);
+
+ auto d2ydx2_mid = MakeNode("elemwise_mul", n->attrs.name + "_d2ydx2_mid",
+ {dydx_mul_dldy, nnvm::NodeEntry{fx}}, nullptr,
&n);
+
+ auto d2ydx2 = MakeNode("_mul_scalar", n->attrs.name + "_d2ydx2",
+ {nnvm::NodeEntry{d2ydx2_mid}}, &args, &n);
+
+ std::vector<nnvm::NodeEntry> ret;
+
+ ret.emplace_back(MakeNode("elemwise_mul", n->attrs.name +
"_backward_grad_grad",
Review comment:
Maybe a comment would help here, this one is the output corresponding to
dL/dy from the first backward right?
I'm still unclear since the previous PRs on what
dL/dxgrad * dy/dx represents. To me is not obvious after spending more
than half an hour thinking.
https://github.com/apache/incubator-mxnet/pull/15120
----------------------------------------------------------------
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