lod_reset

paddle.fluid.layers.lod_reset(x, y=None, target_lod=None)[source]

Set LoD of x to a new one specified by y or target_lod. When y provided, y.lod would be considered as target LoD first, otherwise y.data would be considered as target LoD. If y is not provided, target LoD should be specified by target_lod. If target LoD is specified by y.data or target_lod, only one level LoD is supported.

* Example 1:

    Given a 1-level LoDTensor x:
        x.lod =  [[ 2,           3,                   1 ]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    target_lod: [4, 2]

    then we get a 1-level LoDTensor:
        out.lod =  [[4,                          2]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]

* Example 2:

    Given a 1-level LoDTensor x:
        x.lod =  [[2,            3,                   1]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    y is a Tensor:
        y.data = [[2, 4]]
        y.dims = [1, 3]

    target_lod:
        This parameter does not work when y is not none.

    then we get a 1-level LoDTensor:
        out.lod =  [[2,            4]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]

* Example 3:

    Given a 1-level LoDTensor x:
        x.lod =  [[2,            3,                   1]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    y is a 2-level LoDTensor:
        y.lod =  [[2, 2], [2, 2, 1, 1]]
        y.data = [[1.1], [2.1], [3.1], [4.1], [5.1], [6.1]]
        y.dims = [6, 1]

    target_lod:
        This parameter does not work when y is not none.

    then we get a 2-level LoDTensor:
        out.lod =  [[2, 2], [2, 2, 1, 1]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]
Parameters
  • x (Variable) – Input variable which could be a Tensor or LoDTensor.

  • y (Variable|optional) – If provided, output’s LoD would be derived from y.

  • target_lod (list|tuple|optional) – One level LoD which should be considered as target LoD when y not provided.

Returns

Output variable with LoD specified by this layer.

Return type

Variable

Raises

ValueError – If y and target_lod are both None.

Examples

import paddle.fluid as fluid
import numpy

# Graph Organizing
x = fluid.data(name='x', shape=[6])
y = fluid.data(name='y', shape=[6], lod_level=1)
output = fluid.layers.lod_reset(x=x, y=y)

# Create an executor using CPU as an example
place = fluid.CPUPlace()
exe = fluid.Executor(place)

# Execute
x_tensor = fluid.core.LoDTensor()
x_tensor.set(numpy.ones([6]).astype(numpy.float32), place)
y_ndarray = numpy.ones([6]).astype(numpy.float32)
y_lod = [[2, 2], [2, 2, 1, 1]]
y_tensor = fluid.create_lod_tensor(y_ndarray, y_lod, place)

res, = exe.run(fluid.default_main_program(),
               feed={'x':x_tensor, 'y':y_tensor},
               fetch_list=[output],
               return_numpy=False)
print(res)
# Output Value:
# lod: [[0, 2, 4], [0, 2, 4, 5, 6]]
# dim: 6
# layout: NCHW
# dtype: float
# data: [1 1 1 1 1 1]