larroy commented on a change in pull request #15253: [MXNET-978] Add higher order gradient support `tan`, `tanh` URL: https://github.com/apache/incubator-mxnet/pull/15253#discussion_r302708484
########## File path: src/operator/tensor/elemwise_unary_op_trig.cc ########## @@ -139,7 +139,31 @@ The storage type of ``tan`` output depends upon the input storage type: )code" ADD_FILELINE) .set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseOut{ "_backward_tan" }); -MXNET_OPERATOR_REGISTER_BINARY_WITH_SPARSE_CPU_DR(_backward_tan, unary_bwd<mshadow_op::tan_grad>); +MXNET_OPERATOR_REGISTER_BINARY_WITH_SPARSE_CPU_DR(_backward_tan, unary_bwd<mshadow_op::tan_grad>) +.set_attr<nnvm::FGradient>("FGradient", + [](const nnvm::NodePtr& n, const std::vector<nnvm::NodeEntry>& ograds) { + // NodeEntry{n} : y_grad * f'(x) + // n->inputs[0] : y_grad + // n->inputs[1] : f(x) = tan(x) + // ograds[0] : head_grads + // f'(x) = sec^2(x) + // f''(x) = 2 * f'(x) * f(x) + 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 grad_grad_mid = MakeNode("elemwise_mul", n->attrs.name + "_grad_mul", Review comment: Thanks. About the outputs, I think we should write some documentation explaining what we are doing as I find it non trivial. Can you help me understand the y_grad_grad (first output)? If you want, we can move the conversation to the dev list or slack, as the PR LGTM.  ---------------------------------------------------------------- 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