问题确实在标题中,我正在寻找一个在scipy/ Numpy /etc中(不是TensorFlow)的方法,它封装了tf.tensor_scatter_nd_add中描述的行为,但在块数组上而不是张量上。
我遇到过scipy.ndimage.sum方法,但无法让它重现我在下面给出的示例。
您认为合适的方法必须能够重现TF文档中提供的rank-3示例:
indices = tf.constant([[0], [2]])
updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]],
[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]]])
tensor = tf.ones([4, 4, 4],dtype=tf.int32)
updated = tf.tensor_scatter_nd_add(tensor, indices, updates)
print(updated)希望之前有人已经解决了类似的问题,可以在这里提供帮助-提前感谢!
发布于 2021-01-15 19:51:30
我可以确认以下函数为我捕获了所需的行为:
def scatter_nd_add_numpy(target, indices, updates):
indices = tuple(indices.reshape(-1, indices.shape[-1]).T)
np.add.at(target, indices, updates)
return target感谢雷米在this stackoverflow thread上的回答。
https://stackoverflow.com/questions/65734836
复制相似问题