scatter_nd_add

paddle.fluid.layers.scatter_nd_add(ref, index, updates, name=None)[source]

Scatter_nd_add Layer

Output is obtained by applying sparse addition to a single value or slice in a Variable.

ref is a Tensor with rank \(R\) and index is a Tensor with rank \(K\) . Thus, index has shape \([i_0, i_1, ..., i_{K-2}, Q]\) where \(Q \leq R\) . updates is a Tensor with rank \(K - 1 + R - Q\) and its shape is \(index.shape[:-1] + ref.shape[index.shape[-1]:]\) .

According to the \([i_0, i_1, ..., i_{K-2}]\) of index , add the corresponding updates slice to the ref slice which is obtained by the last one dimension of index .

Given:

* Case 1:
    ref = [0, 1, 2, 3, 4, 5]
    index = [[1], [2], [3], [1]]
    updates = [9, 10, 11, 12]

  we get:

    output = [0, 22, 12, 14, 4, 5]

* Case 2:
    ref = [[65, 17], [-14, -25]]
    index = [[], []]
    updates = [[[-1, -2], [1, 2]],
               [[3, 4], [-3, -4]]]
    ref.shape = (2, 2)
    index.shape = (2, 0)
    updates.shape = (2, 2, 2)

  we get:

    output = [[67, 19], [-16, -27]]
Parameters
  • ref (Variable) – The ref input. Its dtype should be int32, int64, float32, float64.

  • index (Variable) – The index input with rank > 1 and index.shape[-1] <= ref.rank. Its dtype should be int32 or int64 as it is used as indexes.

  • updates (Variable) – The updated value of scatter_nd_add op, and it must have the same dtype as ref. It must have the shape index.shape[:-1] + ref.shape[index.shape[-1]:].

  • name (str|None) – The output variable name. If set None, the layer will be named automatically.

Returns

The output is a tensor with the same shape and dtype as ref.

Return type

output (Variable)

Examples

import paddle.fluid as fluid

ref = fluid.data(name='ref', shape=[3, 5, 9, 10], dtype='float32')
index = fluid.data(name='index', shape=[3, 2], dtype='int32')
updates = fluid.data(name='update', shape=[3, 9, 10], dtype='float32')

output = fluid.layers.scatter_nd_add(ref, index, updates)