sequence_scatter

api_attr

declarative programming (static graph)

paddle.fluid.layers.sequence_scatter(input, index, updates, name=None)[source]

Note:

The index and updates parameters of the OP must be LoDTensor.

Plus the updates data to the corresponding input according to the index.

The updated algorithm is as follows: output[instance_index][index [pos]] = input[instance_index][index [pos]] + updates[pos], where instance_idx is the K sample corresponding to pos in batch.

The value of output[i][j] depends on whether j can be found in the i+1th interval of the index. If found, out[i][j] = input[i][j] + update[m] [n], otherwise, out[i][j] = input[i][j].

For example, in the following example, the lod information for index is divided into three sequences. Among them, because the element 0 can be found in the first interval of the index, it is updated with the value of the corresponding position of the updates, out[0][0] = input[0][0]+updates[0][0] . Because element 1 cannot be found in the third interval of index, out[2][1] = input[2][1].

*Case 1:

    Given:
        input.data = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                      [1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                      [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
                      input.dims = [3, 6]

        index.data = [[0], [1], [2], [5], [4], [3], [2], [1], [3], [2], [5], [4]]
        index.lod =  [[0,        3,                       8,                 12]]

        updates.data = [[0.3], [0.3], [0.4], [0.1], [0.2], [0.3], [0.4], [0.0], [0.2], [0.3], [0.1], [0.4]]
        updates.lod =  [[  0,            3,                                 8,                         12]]

    Then:
        out.data = [[1.3, 1.3, 1.4, 1.0, 1.0, 1.0],
                    [1.0, 1.0, 1.4, 1.3, 1.2, 1.1],
                    [1.0, 1.0, 1.3, 1.2, 1.4, 1.1]]
        out.dims = X.dims = [3, 6]
Parameters
  • input (Variable) – A Tensor with shape of \([N, k_1... k_n]\). Supported data types: float32, float64, int32, int64.

  • index (Variable) – A LoDTensor contains index information. Its LoD level must be 1 and its data type can be int32 or int64.

  • updates (Variable) – A LodTensor contains updates information. It has the same LoD level with the index and has the same data type with the input. Supported data types: float32, float64, int32, int64.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name

Returns

A Tensor which has been updated. It has the same shape and data type with input.

Return type

Variable

Examples

import paddle.fluid as fluid

input = fluid.data( name="x", shape=[None, 3, 6], dtype='float32' )
index = fluid.data( name='index', shape=[12, 1],  dtype='int64', lod_level=1)
updates = fluid.data( name='updates', shape=[12, 1], dtype='float32', lod_level=1)
output = fluid.layers.sequence_scatter(input, index, updates)