sequence_expand¶
- paddle.static.nn. sequence_expand ( x, y, ref_level=- 1, name=None ) [source]
-
- api_attr
-
Static Graph
Sequence Expand Layer. This layer will expand the input variable
xaccording to specified levelref_levellod ofy. Please note that the lod level ofxis at most 1. If the lod level ofxis 1, than the size of lod ofxmust be equal to the length ofref_levellod ofy. If the lod level ofxis 0, then the first dim ofxshould be equal to the size ofref_levelofy. The rank of x is at least 2. When rank ofxis greater than 2, then it would be viewed as a 2-D tensor.-
Please note that the input
xshould be LodTensor or Tensor, -
and input
ymust 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 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 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, 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
yto 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: The expanded variable which is a LoDTensor, with dims
[N, K]. -
Ndepends on the lod info ofxandy. The data type is same as input.
Return Type: Variable
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') 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]
