tf.scatter_update(ref, indices, updates)
或tf.scatter_add(ref, indices, updates)
如何?
ref[indices[...], :] = updates
ref[indices[...], :] += updates
看到这个 。
0
tf.scatter_update(ref, indices, updates)
或tf.scatter_add(ref, indices, updates)
如何?
ref[indices[...], :] = updates
ref[indices[...], :] += updates
看到这个 。
0
更新: TensorFlow 1.0包含一个tf.scatter_nd()
运算符,可用于在下面创建delta
而不创建tf.SparseTensor
。
对于现有的操作,这实际上是令人惊讶的棘手!也许有人可以提出一种更好的方法来总结以下内容,但这是实现此目的的一种方法。
假设您有一个tf.constant()
张量:
c = tf.constant([[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]])
...并且您想在位置[1,1]处添加1.0
。你可以做到这一点的一种方法是定义一个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)
0
我问这个问题感到很尴尬,但是如何在张量中调整单个值?假设您只想在张量中的一个值上加上“ 1”?
通过建立索引无法正常工作:
一种方法是建立形状相同的0的张量。然后在所需位置调整1。然后将两个张量加在一起。同样,这会遇到与以前相同的问题。
我已经多次阅读API文档,似乎无法弄清楚该如何做。提前致谢!