【问题标题】:Numpy Equivalent to "tf.tensor_scatter_nd_add" methodNumpy 等效于“tf.tensor_scatter_nd_add”方法
【发布时间】:2021-04-20 09:47:51
【问题描述】:

问题确实在标题中,我正在寻找 scipy/numpy/etc 中的方法。 (不是 TensorFlow),它封装了 tf.tensor_scatter_nd_add 中描述的行为,但在 Numpy 数组而不是张量上。

我遇到了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)

希望有人以前解决过类似的问题并可以在这里提供帮助 - 提前致谢!

【问题讨论】:

标签: python numpy tensorflow scipy


【解决方案1】:

我可以确认以下函数为我捕获了所需的行为:

    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

感谢 Remy 在this stackoverflow thread 上的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2010-11-11
    相关资源
    最近更新 更多