sequence_slice

paddle.static.nn. sequence_slice ( input, offset, length, name=None ) [source]

Sequence Slice Layer

The layer crops a subsequence from given sequence with given start offset and subsequence length.

It only supports sequence data (Tensor with lod_level equal to 1).

  - Case:

Given the input Tensor **input**:

    input.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]],
    input.lod = [[3, 2]],
    input.dims = (5, 2),

with offset.data = [[0], [1]] and length.data = [[2], [1]],

the output Tensor will be

    out.data = [[a1, a2], [b1, b2], [e1, e2]],
    out.lod = [[2, 1]],
    out.dims = (3, 2).

Note

The first dimension size of input, offset and length should be equal. The offset should start from 0.

Parameters
  • input (Tensor) – Tensor, The input Tensor which consists of the complete sequences.The data type can be float32, float64, int32 or int64

  • offset (Tensor) – Tensor, The offset to slice each sequence. The data type is int32 or int64.

  • length (Tensor) – Tensor, The length of each subsequence. The data type is int32 or 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

The output subsequences.

Return type

Tensor

Examples

>>> import paddle
>>> paddle.enable_static()

>>> import numpy as np
>>> seqs = paddle.static.data(name='x', shape=[10, 5],
...                  dtype='float32', lod_level=1)
>>> offset = paddle.assign(np.array([[0, 1]]).astype("int32"))
>>> length = paddle.assign(np.array([[2, 1]]).astype("int32"))
... subseqs = paddle.static.nn.sequence_slice(input=seqs, offset=offset,
...                                       length=length)