sequence_unpad

paddle.static.nn. sequence_unpad ( x, length, name=None ) [source]

Note

The input of the OP is Tensor and the output is Tensor. For padding operation, See:** sequence_pad

Remove the padding data from the input based on the length information and returns a Tensor.

Case 1:

Given input Tensor **x**:
    x.data = [[ 1.0,  2.0,  3.0,  4.0,  5.0],
              [ 6.0,  7.0,  8.0,  9.0, 10.0],
              [11.0, 12.0, 13.0, 14.0, 15.0]],

in which there are 3 sequences padded to length 5, and the actual length
specified by input Tensor **length**:

    length.data = [2, 3, 4],

after unpadding, the output Tensor will be:

    out.data = [[1.0, 2.0, 6.0, 7.0, 8.0, 11.0, 12.0, 13.0, 14.0]]
    out.lod = [[0, 2, 5, 9]]
Parameters
  • x (Tensor) – A Tensor which contains padding data, and its shape size can not be less than 2. Supported data types: float32, float64, int32, int64.

  • length (Tensor) – A 1D Tensor that stores the actual length of each sample, and the Tensor has the same shape with the 0th dimension of the X . Supported data types: int64.

  • name (str|None) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name

Returns

A Tensor whose recursive sequence length is consistent with the information of the length parameter and it has the same data type with input.

Return type

Tensor

Examples

>>> import paddle
>>> paddle.enable_static()
>>> import paddle.base as base
>>> import numpy

>>> # pad data
>>> x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1)
>>> pad_value = paddle.assign(numpy.array([0.0], dtype=numpy.float32))
>>> pad_data, len = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value)

>>> # unpad data
>>> unpad_data = paddle.static.nn.sequence_unpad(x=pad_data, length=len)