Official ZeroOut gradient example error: AttributeError: 'list' object has no attribute 'eval'

后端 未结 1 861
一整个雨季
一整个雨季 2020-12-22 13:20

I followed the official tutorial of the tensorflow website: https://www.tensorflow.org/extend/adding_an_op There is also described how to call the gradient of the example Ze

相关标签:
1条回答
  • 2020-12-22 13:41

    Answer to old question

    The implementation

    def _zero_out_grad(op, *grads):
        topdiff = grads[0]
        bottom = op.inputs[0]
    
        shape = array_ops.shape(bottom)
        index = array_ops.zeros_like(shape)
        first_grad = array_ops.reshape(topdiff, [-1])[0]
        to_zero_grad = sparse_ops.sparse_to_dense([index], shape, first_grad, 0)
    
        return to_zero_grad
    

    works quite nicely here. Are you sure "@ops.RegisterGradient("ZeroOut")" is executed before the tf.Session()?

    Usually the

    zero_out_module = tf.load_op_library('./libzeroout.so')
    @ops.RegisterGradient("ZeroOut")
    def _zero_out_grad(op, grad):
        # ...
    

    is placed in a different file and just imported. A full working example even with the recent TensorFlow version is here.

    Answer to completely changed question

    Your gradient function returns a list and a Python list has no 'eval()'. Try either:

    grad = tf.gradients(ys=tf.reduce_sum(ret), xs=t_in)[0]
    

    Or follow best practice and use

    grad = tf.gradients(ys=tf.reduce_sum(ret), xs=t_in)
    with tf.Session() as sess:
        sess.run(grad, feed_dict=feed_dict)
    

    Please do not change your entire question

    0 讨论(0)
提交回复
热议问题