merge_selected_rows

paddle.fluid.layers.merge_selected_rows(x, name=None)[source]

MergeSelectedRows Operator.

MergeSelectedRows is used to merge the duplicated rows of the input. The output’s row has no duplicated, and it’s order is incremental.

Example: Input: X.rows is [0, 5, 5, 4, 19] X.height is 20 X.value is: [[1, 1] [2, 2] [3, 3] [4, 4] [6, 6]]

Output: Out.row is [0, 4, 5, 19] Out.height is 20 Out.value is: [[1, 1] [4, 4] [5, 5] [6, 6]]

Parameters
  • x (Variable) – The input type is SelectedRows, and the selected rows may be duplicated

  • name (basestring|None) – Name of the output.

Returns

The output type is SelectedRows, and the selected rows are not duplicated

Return type

out(Variable)

Examples

import paddle.fluid as fluid
import numpy

place = fluid.CPUPlace()
block = fluid.default_main_program().global_block()

var = block.create_var(name="X2",
                       dtype="float32",
                       persistable=True,
                       type=fluid.core.VarDesc.VarType.SELECTED_ROWS)
y = fluid.layers.merge_selected_rows(var)
z = fluid.layers.get_tensor_from_selected_rows(y)

x_rows = [0, 2, 2, 4, 19]
row_numel = 2
np_array = numpy.ones((len(x_rows), row_numel)).astype("float32")

x = fluid.global_scope().var("X2").get_selected_rows()
x.set_rows(x_rows)
x.set_height(20)
x_tensor = x.get_tensor()
x_tensor.set(np_array, place)

exe = fluid.Executor(place=place)
result = exe.run(fluid.default_main_program(), fetch_list=[z])

print("x_rows: ", x_rows)
print("np_array: ", np_array)
print("result: ", result)
'''
Output Values:
('x_rows: ', [0, 2, 2, 4, 19])
('np_array: ', array([[1., 1.],
       [1., 1.],
       [1., 1.],
       [1., 1.],
       [1., 1.]], dtype=float32))
('result: ', [array([[1., 1.],
       [2., 2.],
       [1., 1.],
       [1., 1.]], dtype=float32)])
'''