indices = [[1, 1]] # A list of coordinates to update.
values = [1.0] # A list of values corresponding to the respective
# coordinate in indices.
shape = [3, 3] # The shape of the corresponding dense tensor, same as `c`.
delta = tf.SparseTensor(indices, values, shape)
2 回答
UPDATE: TensorFlow 1.0包含一个tf.scatter_nd()运算符,可用于在不创建
tf.SparseTensor
的情况下创建delta
.对于现有的操作,这实际上是非常棘手的!也许有人可以建议一个更好的方法来结束以下,但这是一种方法来做到这一点 .
假设你有一个
tf.constant()
张量:...并且您想在位置[1,1]添加
1.0
. 您可以这样做的一种方法是定义一个tf.SparseTensor,delta
,表示更改:然后你可以使用tf.sparse_tensor_to_dense() op从
delta
创建一个密集张量并将其添加到c
:tf.scatter_update(ref, indices, updates)
或tf.scatter_add(ref, indices, updates)
怎么样?见this .