title: How to implement custom gradient of a tensor using eager execution in TensorFlow
body: <p>In this(<a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/g3doc/guide.md" rel="nofollow noreferrer">https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/g3doc/guide.md</a>) tutorial, a method of implementing custom gradient function is provided.</p>
  
  <pre><code>@tfe.custom_gradient
  def log1pexp(x):
    e = tf.exp(x)
    def grad(dy):
      return dy * (1 - 1 / (1 + e))
    return tf.log(1 + e), grad
  grad_log1pexp = tfe.gradients_function(log1pexp)
  
  # Works as before at x = 0.
  assert 0.5 == float(grad_log1pexp(0.)[0])
  
  # But now works at x = 100 as well.
  assert 1.0 == float(grad_log1pexp(100.)[0])
  </code></pre>
  
  <p>I would like to implement a gradient function for matrix exponential, in which case the variable is a tensor. I tried the straight forward approach but it seems failed.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>What's the meaning of dy here?</li>
  <li>How should these code be adopted to tensors?</li>
  </ol>