sequence_expand

paddle.fluid.layers.sequence_expand(x, y, ref_level=-1, name=None)[source]

Sequence Expand Layer. This layer will expand the input variable x according to specified level ref_level lod of y. Please note that the lod level of x is at most 1. If the lod level of x is 1, than the size of lod of x must be equal to the length of ref_level lod of y. If the lod level of x is 0, then the first dim of x should be equal to the size of ref_level of y. The rank of x is at least 2. When rank of x is greater than 2, then it would be viewed as a 2-D tensor.

Please note that the input x should be LodTensor or Tensor, and input y must be LodTensor.

Following examples will explain how sequence_expand works:

Case 1

Consider 2 sequences [a][b] and [c][d], now we want to expand them to [a][b], [a][b], [c][d] and [c][d].
Sequence [a][b] expand twice and [c][d] expands twice, so the lod which according to is [2, 2].

Input x is a 1-level LoDTensor:
    x.lod  = [[2,        2]]    #lod based on length may be easier to understand
    x.data = [[a], [b], [c], [d]]
    x.dims = [4, 1]

input y is a LoDTensor:
    y.lod = [[2,    2],    #the 0th level lod, according to this level
             [3, 3, 1, 1]] #the 1st level lod, it has nothing to do with this level

ref_level: 0

then output is a 1-level LoDTensor out:
    out.lod =  [[2,        2,        2,        2]]    #lod based on offfset
    out.data = [[a], [b], [a], [b], [c], [d], [c], [d]]
    out.dims = [8, 1]


Case 2

Consider 3 sequences [a], [b], [c], now we want to expand them to [a][a], [c][c][c].
It's obvious that the lod info of expanded sequences is [2, 0, 3].

x is a Tensor:
    x.data = [[a], [b], [c]]
    x.dims = [3, 1]

y is a LoDTensor:
    y.lod = [[2, 0, 3]]

ref_level: -1

then output is a 1-level LodTensor:
    out.data = [[a], [a], [c], [c], [c]]
    out.dims = [5, 1]
Parameters
  • x (Variable) – The input variable which is a Tensor or LoDTensor, with the dims [M, K]. The lod level is at most 1. The data type should be float32, float64, int8, int32 or int64.

  • y (Variable) – The input variable which is a LoDTensor, the lod level is at least 1.

  • ref_level (int) – Lod level of y to be referred by x. If set to -1, refer the last level of lod.

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Returns: The expanded variable which is a LoDTensor, with dims [N, K]. N depends on the lod info of x and y. The data type is same as input.

Return Type: Variable

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers
import numpy as np

x = fluid.data(name='x', shape=[4, 1], dtype='float32')
y = fluid.data(name='y', shape=[8, 1],
            dtype='float32', lod_level=1)
out = layers.sequence_expand(x=x, y=y, ref_level=0)

exe = fluid.Executor(fluid.CPUPlace())
place = fluid.CPUPlace()

np_data = np.array([[1], [2], [3], [4]]).astype('float32')
x_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2]], place)
print(x_lod_tensor)
#lod: [[0, 2, 4]]
#    dim: 4, 1
#    layout: NCHW
#    dtype: float
#    data: [1 2 3 4]

np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32')
y_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2], [3,3,1,1]], place)
print(y_lod_tensor)
#lod: [[0, 2, 4][0, 3, 6, 7, 8]]
#    dim: 8, 1
#    layout: NCHW
#    dtype: int64_t
#    data: [0 0 1 1 1 1 1 0]

out_main = exe.run(fluid.default_main_program(),
                feed={'x': x_lod_tensor, 'y': y_lod_tensor},
                fetch_list=[out], return_numpy=False)
print(out_main[0])
#lod: [[0, 2, 4, 6, 8]]
#    dim: 8, 1
#    layout: NCHW
#    dtype: float
#    data: [1 2 1 2 3 4 3 4]