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

本小妞迷上赌 提交于 2019-11-29 18:15:42

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

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