test_sequence_enumerate_op.py 4.2 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


22 23 24 25
def sequence_enumerate(input_seq, in_lod, win_size, pad_value):
    lod0 = [0]
    for i in range(0, len(in_lod[0])):
        lod0.append(lod0[i] + in_lod[0][i])
C
chenweihang 已提交
26
    out_seq = []
27 28 29 30 31 32
    for i in range(0, len(lod0) - 1):
        for idx in range(lod0[i], lod0[i + 1]):
            single_seq = []
            for word_idx in range(win_size):
                word_pos = idx + word_idx
                dat = input_seq[word_pos] if word_pos < lod0[i+1] \
C
chenweihang 已提交
33
                    else pad_value
34 35
                single_seq.append(dat)
            out_seq.append(single_seq)
C
chenweihang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    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
55 56
        out_seq = sequence_enumerate(self.in_seq, self.lod, self.win_size,
                                     self.pad_value)
C
chenweihang 已提交
57 58 59 60 61 62 63 64 65
        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
66 67
        out_seq = sequence_enumerate(self.in_seq, self.lod, self.win_size,
                                     self.pad_value)
C
chenweihang 已提交
68 69 70
        self.out_seq = np.array(out_seq).astype("int64")


C
chenweihang 已提交
71 72 73 74 75 76 77 78 79 80 81
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")


C
chenweihang 已提交
82 83 84 85 86 87
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
88 89
        out_seq = sequence_enumerate(self.in_seq, self.lod, self.win_size,
                                     self.pad_value)
C
chenweihang 已提交
90 91 92
        self.out_seq = np.array(out_seq).astype("int32")


C
chenweihang 已提交
93 94 95 96 97 98 99 100 101 102 103
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")


104 105 106 107 108 109 110 111 112 113 114
class TestSequenceEnumerateOpLargePadValueSeqLen0(TestSequenceEnumerateOp):
    def init_test_case(self):
        self.in_seq = np.random.randint(0, 10, (30, 1)).astype("int32")
        self.lod = [[0, 14, 0, 16, 0]]
        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")


C
chenweihang 已提交
115 116
if __name__ == "__main__":
    unittest.main()