Skip to content

Commit 46e61d8

Browse files
author
Yibing Liu
committed
Wrapper py api for sequence_unpad
test=develop
1 parent cbe4292 commit 46e61d8

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ paddle.fluid.layers.conv3d_transpose ArgSpec(args=['input', 'num_filters', 'outp
7676
paddle.fluid.layers.sequence_expand ArgSpec(args=['x', 'y', 'ref_level', 'name'], varargs=None, keywords=None, defaults=(-1, None))
7777
paddle.fluid.layers.sequence_expand_as ArgSpec(args=['x', 'y', 'name'], varargs=None, keywords=None, defaults=(None,))
7878
paddle.fluid.layers.sequence_pad ArgSpec(args=['x', 'pad_value', 'maxlen'], varargs=None, keywords=None, defaults=(None,))
79+
paddle.fluid.layers.sequence_unpad ArgSpec(args=['x', 'length'], varargs=None, keywords=None, defaults=None)
7980
paddle.fluid.layers.lstm_unit ArgSpec(args=['x_t', 'hidden_t_prev', 'cell_t_prev', 'forget_bias', 'param_attr', 'bias_attr', 'name'], varargs=None, keywords=None, defaults=(0.0, None, None, None))
8081
paddle.fluid.layers.reduce_sum ArgSpec(args=['input', 'dim', 'keep_dim', 'name'], varargs=None, keywords=None, defaults=(None, False, None))
8182
paddle.fluid.layers.reduce_mean ArgSpec(args=['input', 'dim', 'keep_dim', 'name'], varargs=None, keywords=None, defaults=(None, False, None))

python/paddle/fluid/layers/nn.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
'sequence_expand',
5757
'sequence_expand_as',
5858
'sequence_pad',
59+
'sequence_unpad',
5960
'lstm_unit',
6061
'reduce_sum',
6162
'reduce_mean',
@@ -2843,6 +2844,64 @@ def sequence_pad(x, pad_value, maxlen=None):
28432844
return out, length
28442845

28452846

2847+
def sequence_unpad(x, length):
2848+
"""
2849+
Sequence Unpad Layer
2850+
2851+
This layer removes the padding data in the input sequences and convert
2852+
them into sequences with actual length as output, identitied by lod
2853+
information.
2854+
2855+
.. code-block:: text
2856+
2857+
Example:
2858+
2859+
Given input Variable **x**:
2860+
x.data = [[ 1.0, 2.0, 3.0, 4.0, 5.0],
2861+
[ 6.0, 7.0, 8.0, 9.0, 10.0],
2862+
[11.0, 12.0, 13.0, 14.0, 15.0]],
2863+
2864+
in which there are 3 sequences padded to length 5, and the acutal length
2865+
specified by input Variable *length*:
2866+
2867+
length.data = [[2], [3], [4]],
2868+
2869+
after unpadding, the output Variable will be:
2870+
2871+
out.data = [[1.0, 2.0, 6.0, 7.0, 8.0, 11.0, 12.0, 13.0, 14.0]]
2872+
out.lod = [[0, 2, 5, 9]]
2873+
2874+
Args:
2875+
x(Variable): Input Variable which contains the padded sequences with
2876+
equal length.
2877+
length(Variable): The Variable that specifies the actual ength of
2878+
sequences after unpadding.
2879+
2880+
Returns:
2881+
Variable: The Variable contains the unpadded sequences.
2882+
2883+
Examples:
2884+
.. code-block:: python
2885+
2886+
x = fluid.layers.data(name='x', shape=[10, 5], dtype='float32')
2887+
len = fluid.layers.data(name='length', shape=[1], dtype='int64')
2888+
out = fluid.layers.sequence_unpad(x=x, length=len)
2889+
"""
2890+
2891+
helper = LayerHelper('sequence_unpad', input=x, **locals())
2892+
dtype = helper.input_dtype()
2893+
out = helper.create_tmp_variable(dtype)
2894+
2895+
length.stop_gradient = True
2896+
2897+
helper.append_op(
2898+
type='sequence_unpad',
2899+
inputs={'X': x,
2900+
'Length': length},
2901+
outputs={'Out': out})
2902+
return out
2903+
2904+
28462905
def beam_search(pre_ids,
28472906
pre_scores,
28482907
ids,

python/paddle/fluid/tests/unittests/test_layers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ def test_sequence_expand(self):
194194
self.assertIsNotNone(layers.sequence_expand(x=x, y=y, ref_level=1))
195195
print(str(program))
196196

197+
def test_sequence_unpad(self):
198+
program = Program()
199+
with program_guard(program):
200+
x = layers.data(name='x', shape=[10, 5], dtype='float32')
201+
length = layers.data(name='length', shape=[1], dtype='int64')
202+
self.assertIsNotNone(layers.sequence_unpad(x=x, length=length))
203+
print(str(program))
204+
197205
def test_lstm_unit(self):
198206
program = Program()
199207
with program_guard(program):

0 commit comments

Comments
 (0)