sequence_last_step

paddle.static.nn. sequence_last_step ( input ) [source]

Only supports Tensor as input. Given the input Tensor, it will select last time-step feature of each sequence as output.

Case 1:
input is 1-level Tensor:
    input.lod = [[0, 2, 5, 7]]
    input.data = [[1.], [3.], [2.], [4.], [6.], [5.], [1.]]
    input.shape = [7, 1]

output is a Tensor:
    out.shape = [3, 1]
    out.shape[0] == len(x.lod[-1]) == 3
    out.data = [[3.], [6.], [1.]], where 3.=last(1., 3.), 6.=last(2., 4., 6.), 1.=last(5., 1.)

Case 2:
input is a 2-level Tensor containing 3 sequences with length info [2, 0, 3],
where 0 means empty sequence.
The first sequence contains 2 subsequence with length info [1, 2];
The last sequence contains 3 subsequence with length info [1, 0, 3].
    input.lod = [[0, 2, 2, 5], [0, 1, 3, 4, 4, 7]]
    input.data = [[1.], [3.], [2.], [4.], [6.], [5.], [1.]]
    input.shape = [7, 1]

It will apply pooling on last lod_level [0, 1, 3, 4, 4, 7]. pad_value = 0.0
output is a Tensor:
    out.shape= [5, 1]
    out.lod = [[0, 2, 2, 5]]
    out.shape[0] == len(x.lod[-1]) == 5
    out.data = [[1.], [2.], [4.], [0.0], [1.]]
    where 1.=last(1.), 2.=last(3., 2.), 4.=last(4.), 0.0 = pad_value, 1=last(6., 5., 1.)
Parameters

input (Tensor) – Tensor with lod_level no more than 2. The data type should be float32.

Returns

Tensor consist of the sequence’s last step vector. The data type is float32.

Return type

Tensor

Examples

import paddle
paddle.enable_static()

x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1)
x_last_step = paddle.static.nn.sequence_last_step(input=x)