在这个(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/g3doc/guide.md)教程中,提供了一种实现自定义渐变功能的方法。
@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])
我想为矩阵指数实现渐变函数,在这种情况下变量是张量。我尝试了直接的方法,但似乎失败了。
我的问题是: