sequence_expand_as

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

Sequence Expand As Layer. This OP will expand the input variable x according to the zeroth level lod of y. Current implementation requires the level number of y’s lod must be 1, and the first dimension of x should be equal to the size of y’s zeroth level lod, thus the expanded LodTensor has the same lod info as y. The expanded result has nothing to do with x’s lod, so the lod of Input(X) is not considered.

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

Following examples will explain how sequence_expand_as works:

Case 1:

Consider 4 sequences [a], [b], [c], [d], now we want to expand them to [a][a][a], [b][b][b], [c] and [d].
It's obvious that the lod info of expanded sequences is [0, 3, 6, 7, 8].
Given a 1-level LodTensor ``x``:
    x.data = [[a], [b], [c], [d]]
    x.dims = [4, 1]
and input ``y``
    y.lod = [[3, 3, 1, 1]] #lod based on length may be easier to understand

then we get 1-level LoDTensor out:
    Out.lod =  [[0,            3,              6,  7,  8]] #based on offset
    Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
    Out.dims = [8, 1]


Case 2:

Given a common Tensor ``x``:
    x.data = [[a, b], [c, d], [e, f]]
    x.dims = [3, 2]
and input ``y``:
    y.lod = [[0, 2, 3, 6]]

then we get a 1-level LoDTensor:
    out.lod =  [[0,             2,     3,                    6]]
    out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
    out.dims = [6, 2]
Parameters
  • x (Variable) – The input variable which is a Tensor or LoDTensor, with the dims [M, K]. The data type should be float32, float64, int8, int32 or int64.

  • y (Variable) – The input variable which is a LoDTensor with 1-level 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 the dims [N, K]. N depends on the lod of y, and the lod level must be 1. 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_as(x=x, y=y)

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, [[3,3,1,1]], place)
print(y_lod_tensor)
#lod: [[0, 3, 6, 7, 8]]
#    dim: 8, 1
#    layout: NCHW
#    dtype: int64_t
#    data: [0 0 1 0 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, 3, 6, 7, 8]]
#    dim: 8, 1
#    layout: NCHW
#    dtype: float
#    data: [1 1 1 2 2 2 3 4]