sequence_expand_as¶
- paddle.static.nn. 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 ofy
. Current implementation requires the level number ofy
’s lod must be 1, and the first dimension ofx
should be equal to the size ofy
’s zeroth level lod, thus the expanded Tensor has the same lod info asy
. The expanded result has nothing to do withx
’s lod, so the lod of Input(X) is not considered.Note
Please note that the input
x
should be Tensor or Tensor, and inputy
must be Tensor.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 Tensor ``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 Tensor 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 Tensor: 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 (Tensor) – The input variable which is a Tensor or Tensor, with the dims
[M, K]
. The data type should be float32, float64, int32 or int64.y (Tensor) – The input variable which is a Tensor 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
-
Tensor, The expanded variable which is a Tensor with the dims
[N, K]
.N
depends on the lod ofy
, and the lod level must be 1. The data type is same as input.
Examples
>>> import paddle >>> import paddle.base as 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_as(x=x, y=y) >>> exe = base.Executor(base.CPUPlace()) >>> place = base.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, [[3,3,1,1]], place) >>> print(y_lod_tensor) - lod: {{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, 3, 6, 7, 8}} - place: Place(cpu) - shape: [8, 1] - layout: NCHW - dtype: float32 - data: [1 1 1 2 2 2 3 4]