How can I tell if a tf op has a gradient or not?

拜拜、爱过 提交于 2019-12-30 04:40:08

问题


I am interested in using a SparseTensor in tensorflow, however, I often get

LookupError: No gradient defined for operation ...

Apparently gradient computation is not defined for many ops for sparse tensors. Are there any easy ways to check if an op has a gradient or not before actually writing and running my code?


回答1:


There is a get_gradient_function function in tensorflow.python.framework.ops. It accepts an op and returns a corresponding gradient op. Example:

import tensorflow as tf
from tensorflow.python.framework.ops import get_gradient_function

a = tf.add(1, 2, name="Add_these_numbers")
b = tf.multiply(a, 3, name='mult')

mult = tf.get_default_graph().get_operation_by_name('mult')
print(get_gradient_function(mult))  # <function _MulGrad at 0x7fa29950dc80>

tf.stop_gradient(a, name='stop')
stop = tf.get_default_graph().get_operation_by_name('stop')
print(get_gradient_function(stop))  # None


来源:https://stackoverflow.com/questions/48418029/how-can-i-tell-if-a-tf-op-has-a-gradient-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!