UPDATE:TensorFlow 1.0 包括tf.scatter_nd()运算符,可用于创建delta
下面没有创建tf.SparseTensor
.
对于现有的操作来说,这实际上是非常棘手的!也许有人可以建议一种更好的方法来结束以下内容,但这是一种方法。
假设你有一个tf.constant()
tensor:
c = tf.constant([[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]])
...并且您想添加1.0
在位置 [1, 1]。实现此目的的一种方法是定义一个tf.SparseTensor, delta
,代表变化:
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)
然后您可以使用tf.sparse_tensor_to_dense()op 从中生成稠密张量delta
并将其添加到c
:
result = c + tf.sparse_tensor_to_dense(delta)
sess = tf.Session()
sess.run(result)
# ==> array([[ 0., 0., 0.],
# [ 0., 1., 0.],
# [ 0., 0., 0.]], dtype=float32)