sequence_expand

paddle.static.nn. 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.

Note

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

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 Tensor:
    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 Tensor:
    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 Tensor out:
    out.lod =  [[2,        2,        2,        2]]    #lod based on offset
    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 Tensor:
    y.lod = [[2, 0, 3]]

ref_level: -1

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

  • y (Tensor) – The input variable which is a Tensor, 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

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

Examples

import paddle
from paddle import fluid
paddle.enable_static()
import numpy as np

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

exe = paddle.static.Executor(fluid.CPUPlace())
place = paddle.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')

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/static/nn/sequence_lod.py:docstring of paddle.static.nn.sequence_lod.sequence_expand, line 104)

Explicit markup ends without a blank line; unexpected unindent.

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)

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/static/nn/sequence_lod.py:docstring of paddle.static.nn.sequence_lod.sequence_expand, line 115)

Definition list ends without a blank line; unexpected unindent.

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]