sequence_first_step

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

Warning

API “paddle.static.nn.sequence_lod.sequence_first_step” is deprecated since 3.0.0, and will be removed in future versions. Reason: This API will be deprecated in the future, because it’s just for old statics mode.

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

>>> 
>>> # set env var before import paddle to disable pir mode, following example code use os module.
>>> import os
>>> os.environ['FLAGS_enable_pir_api'] = '0'
>>> 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)