【问题标题】:How can I tell if a tf op has a gradient or not?如何判断 tf op 是否具有渐变?
【发布时间】:2018-01-24 08:35:58
【问题描述】:

我有兴趣在 tensorflow 中使用 SparseTensor,但是,我经常得到 ​​p>

LookupError: 没有为操作定义梯度...

显然,对于稀疏张量的许多操作没有定义梯度计算。在实际编写和运行我的代码之前,是否有任何简单的方法可以检查操作是否具有渐变?

【问题讨论】:

    标签: python tensorflow autodiff


    【解决方案1】:

    tensorflow.python.framework.ops 中有一个get_gradient_function 函数。它接受一个操作并返回一个相应的梯度操作。示例:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多