diff --git a/paddle/fluid/operators/sequence_enumerate_op.cc b/paddle/fluid/operators/sequence_enumerate_op.cc index 307a4ece923cda510da5592753149703691d3715..ae0241bd77a7de1e28d87e8c020a088b8d23ea93 100644 --- a/paddle/fluid/operators/sequence_enumerate_op.cc +++ b/paddle/fluid/operators/sequence_enumerate_op.cc @@ -50,24 +50,23 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker { "(2-D LoDTensor with the 2nd dimension equal to 1) " "Input LoDTensor of SequenceEnumerate operator."); AddOutput("Out", - "(2-D LoDTensor with the 2nd dimension equal to 1) " + "(2-D LoDTensor with the 2nd dimension equal to win_size) " "Output LoDTensor of SequenceEnumerate operator."); AddAttr("win_size", "(int) The enumerate sequence window size.") .AddCustomChecker([](const int& win_size) { PADDLE_ENFORCE(win_size >= 2, - "The window size should be greater than 2."); + "The window size should be not less than 2."); }); AddAttr("pad_value", "(int) The enumerate sequence padding value.") .SetDefault(0); AddComment(R"DOC( Sequence Enumerate Operator. -Sequence enumerate operator generate a new LoDTensor -with the same 1st dimension length as the original LoDTensor, -and with the 2nd dimension equal to the input window length, -the new sub-sequence on 2nd dimension is enumerated one by one on the original sequence. -The values of the last insufficient part areall filled with the input pad_value. - +Generate a new sequence for the input index sequence, which enumerates all the +sub-sequences with length win_size of the input. +The enumerated sequence has the same 1st dimension with variable input, and +the 2nd dimension is win_size, padded by pad_value if necessary in generation. + Examples: Case 1: Input: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 14ebe22b620487f2eed3761a4aa2327618473246..ffb9858108a6843a35a886e402a8d23fe77a1238 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -5522,13 +5522,12 @@ def flatten(x, axis=1, name=None): return out -def sequence_enumerate(input, win_size, pad_value, name=None): +def sequence_enumerate(input, win_size, pad_value=0, name=None): """ - Generate a new LoDTensor - with the same 1st dimension length as the original LoDTensor, - and with the 2nd dimension equal to the input window length, - the new sub-sequence on 2nd dimension is enumerated one by one on the original sequence. - The values of the last insufficient part areall filled with the input pad_value. + Generate a new sequence for the input index sequence, which enumerates all the + sub-sequences with length win_size of the input. + The enumerated sequence has the same 1st dimension with variable input, and + the 2nd dimension is win_size, padded by pad_value if necessary in generation. Examples: Case 1: @@ -5545,9 +5544,9 @@ def sequence_enumerate(input, win_size, pad_value, name=None): Out.dims = [5, 2] Args: - input (Variable): The input variable which is a LoDTensor - win_size (int): The enumerate sequence window size. - pad_value (int): The enumerate sequence padding value. + input (Variable): The input variable which is a index sequence. + win_size (int): The window size for enumerating all sub-sequences. + pad_value (int): The padding value, default 0. Returns: Variable: The enumerate sequence variable which is a LoDTensor. diff --git a/python/paddle/fluid/tests/unittests/test_sequence_enumerate_op.py b/python/paddle/fluid/tests/unittests/test_sequence_enumerate_op.py index f2e5844c7f7ba18e12e5ff95a9a6114f1c5673c4..9814ec0a15e1803b356f300d378c31e57ba36c09 100644 --- a/python/paddle/fluid/tests/unittests/test_sequence_enumerate_op.py +++ b/python/paddle/fluid/tests/unittests/test_sequence_enumerate_op.py @@ -68,6 +68,17 @@ class TesSequenceEnumerateOpInt64(TestSequenceEnumerateOp): self.out_seq = np.array(out_seq).astype("int64") +class TestSequenceEnumerateOpLargeWinSize(TestSequenceEnumerateOp): + def init_test_case(self): + self.in_seq = np.random.randint(0, 10, (30, 1)).astype("int32") + self.lod = [[9, 4, 11, 6]] + self.win_size = 5 + self.pad_value = 0 + out_seq = sequence_enumerate(self.in_seq, self.lod, self.win_size, + self.pad_value) + self.out_seq = np.array(out_seq).astype("int32") + + class TestSequenceEnumerateOpMaxWinSize(TestSequenceEnumerateOp): def init_test_case(self): self.in_seq = np.random.randint(0, 10, (30, 1)).astype("int32") @@ -79,5 +90,16 @@ class TestSequenceEnumerateOpMaxWinSize(TestSequenceEnumerateOp): self.out_seq = np.array(out_seq).astype("int32") +class TestSequenceEnumerateOpLargePadValue(TestSequenceEnumerateOp): + def init_test_case(self): + self.in_seq = np.random.randint(0, 10, (30, 1)).astype("int32") + self.lod = [[9, 4, 11, 6]] + self.win_size = 5 + self.pad_value = 5 + out_seq = sequence_enumerate(self.in_seq, self.lod, self.win_size, + self.pad_value) + self.out_seq = np.array(out_seq).astype("int32") + + if __name__ == "__main__": unittest.main()