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_r302241795
 
 

 ##########
 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:
   Can we clarify / add a comment on why is correct to multiply by y_grad (the 
first head gradient?) again? This would help readers as is not obvious, as well 
as the very non-obvious implicit multiplication by f'(x) it compounds.

----------------------------------------------------------------
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

Reply via email to