test_sequence_enumerate_op.py 2.6 KB
Newer Older
C
chenweihang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import unittest
import numpy as np
from op_test import OpTest


C
chenweihang 已提交
22
def sequence_enumerate(input_seq, win_size, pad_value):
C
chenweihang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    out_seq = []
    for idx in range(0, len(input_seq)):
        single_seq = []
        for word_idx in range(win_size):
            word_pos = idx + word_idx
            dat = input_seq[word_pos] if word_pos < len(input_seq) \
                    else pad_value
            single_seq.append(dat)
        out_seq.append(single_seq)
    return out_seq


class TestSequenceEnumerateOp(OpTest):
    def setUp(self):
        self.op_type = "sequence_enumerate"
        self.init_test_case()
        self.inputs = {'X': (self.in_seq, self.lod)}
        self.attrs = {'win_size': self.win_size, 'pad_value': self.pad_value}
        self.outputs = {'Out': (self.out_seq, self.lod)}

    def test_check_output(self):
        self.check_output()

    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 = 2
        self.pad_value = 0
C
chenweihang 已提交
51
        out_seq = sequence_enumerate(self.in_seq, self.win_size, self.pad_value)
C
chenweihang 已提交
52 53 54 55 56 57 58 59 60
        self.out_seq = np.array(out_seq).astype("int32")


class TesSequenceEnumerateOpInt64(TestSequenceEnumerateOp):
    def init_test_case(self):
        self.in_seq = np.random.randint(0, 10, (30, 1)).astype("int64")
        self.lod = [[9, 4, 11, 6]]
        self.win_size = 2
        self.pad_value = 0
C
chenweihang 已提交
61
        out_seq = sequence_enumerate(self.in_seq, self.win_size, self.pad_value)
C
chenweihang 已提交
62 63 64 65 66 67 68 69 70
        self.out_seq = np.array(out_seq).astype("int64")


class TestSequenceEnumerateOpMaxWinSize(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 = 30
        self.pad_value = 0
C
chenweihang 已提交
71
        out_seq = sequence_enumerate(self.in_seq, self.win_size, self.pad_value)
C
chenweihang 已提交
72 73 74 75 76
        self.out_seq = np.array(out_seq).astype("int32")


if __name__ == "__main__":
    unittest.main()