sequence_first_step

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

Only supports Tensor as input. Given the input Tensor, it will select first 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 = [[1.], [2.], [5.]], where 1.=first(1., 3.), 2.=first(2., 4., 6.), 5.=first(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.], [3.], [4.], [0.0], [6.]]
     where 1.=first(1.), 3.=first(3., 2.), 4.=first(4.), 0.0 = pad_value, 6.=first(6., 5., 1.)
Parameters

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

Returns

Tensor consist of the sequence’s first step vector. The data type is float32 or float64.

Return type

Tensor

Examples

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

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