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 levelref_level
lod ofy
. Please note that the lod level ofx
is at most 1. If the lod level ofx
is 1, than the size of lod ofx
must be equal to the length ofref_level
lod ofy
. If the lod level ofx
is 0, then the first dim ofx
should be equal to the size ofref_level
ofy
. The rank of x is at least 2. When rank ofx
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 inputy
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 byx
. 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 ofx
andy
. The data type is same as input.
Examples
>>> import paddle >>> from paddle import base >>> 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(base.CPUPlace()) >>> place = paddle.CPUPlace() >>> np_data = np.array([[1], [2], [3], [4]]).astype('float32') >>> x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) >>> print(x_lod_tensor) - lod: {{0, 2, 4}} - place: Place(cpu) - shape: [4, 1] - layout: NCHW - dtype: float32 - data: [1 2 3 4] >>> np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') >>> y_lod_tensor = base.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}} - place: Place(cpu) - shape: [8, 1] - layout: NCHW - dtype: float32 - data: [1 2 3 4 5 6 7 8] >>> out_main = exe.run(base.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}} - place: Place(cpu) - shape: [8, 1] - layout: NCHW - dtype: float32 - data: [1 2 1 2 3 4 3 4]