test_seq_project.py 8.1 KB
Newer Older
C
chengduoZH 已提交
1 2
import unittest
import numpy as np
3
import random
C
chengduoZH 已提交
4 5 6 7 8 9 10
from op_test import OpTest


class TestSeqProject(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = 'sequence_project'
C
chengduoZH 已提交
11 12 13 14
        if self.context_length == 1 and self.context_start == 0 and self.padding_trainable:
            print "If context_start is 0 and context_length is 1, padding_trainable should be false."
            return

C
chengduoZH 已提交
15 16 17 18 19 20 21
        # one level, batch size
        x = np.random.uniform(
            0.1, 1, [self.input_size[0], self.input_size[1]]).astype('float32')

        self.begin_pad = np.max([0, -self.context_start])
        self.end_pad = np.max([0, self.context_start + self.context_length - 1])
        self.total_pad = self.begin_pad + self.end_pad
C
chengduoZH 已提交
22 23 24 25
        if self.total_pad == 0:
            self.total_pad = 1
        # PaddingData mast be not empty. Otherwise(EnforceNotMet: enforce numel() > 0 failed, 0 <= 0)
        padding_data = np.random.uniform(
C
chengduoZH 已提交
26
            0.1, 1, [self.total_pad, self.input_size[1]]).astype('float32')
C
chengduoZH 已提交
27

28 29
        self.inputs = {
            'X': (x, self.lod),
C
chengduoZH 已提交
30
            'PaddingData': (padding_data, [[0, self.total_pad]])
31
        }
C
chengduoZH 已提交
32 33 34
        self.attrs = {
            'context_start': self.context_start,
            'context_length': self.context_length,
35 36
            'padding_trainable': self.padding_trainable,
            'context_stride': self.context_stride
C
chengduoZH 已提交
37 38 39 40 41 42 43 44
        }
        out = np.zeros((self.input_size[0], self.input_size[1] *
                        self.context_length)).astype('float32')
        self.outputs = {'Out': out}
        self.compute()

    def compute(self):
        x, lod = self.inputs['X']
C
chengduoZH 已提交
45
        pading_data, _ = self.inputs['PaddingData']
C
chengduoZH 已提交
46 47
        out = self.outputs['Out']
        lod = lod[0]
48
        begin_pad = np.max([0, -self.context_start])
C
chengduoZH 已提交
49 50 51 52 53 54 55 56 57 58

        for i in range(len(lod) - 1):
            for j in range(self.context_length):
                in_begin = lod[i] + self.context_start + j
                in_end = lod[i + 1] + self.context_start + j
                out_begin = lod[i]
                out_end = lod[i + 1]
                if in_begin < lod[i]:
                    pad_size = np.min([lod[i] - in_begin, lod[i + 1] - lod[i]])
                    if self.padding_trainable:
C
chengduoZH 已提交
59
                        sub_w = pading_data[j:j + pad_size, :]
C
chengduoZH 已提交
60 61 62 63 64 65 66 67 68
                        out[lod[i]:lod[i] + pad_size, j * self.input_size[1]:(
                            j + 1) * self.input_size[1]] = sub_w
                    out_begin = lod[i] + pad_size
                    in_begin = lod[i]

                if in_end > lod[i + 1]:
                    pad_size = np.min(
                        [in_end - lod[i + 1], lod[i + 1] - lod[i]])
                    if self.padding_trainable:
C
chengduoZH 已提交
69 70 71
                        sub_w = pading_data[begin_pad + self.context_start + j -
                                            pad_size:begin_pad +
                                            self.context_start + j, :]
C
chengduoZH 已提交
72 73 74 75 76 77 78 79 80 81 82
                        out[lod[i + 1] - pad_size:lod[i + 1], j * self.
                            input_size[1]:(j + 1) * self.input_size[1]] = sub_w
                    in_end = lod[i + 1]
                    out_end = lod[i + 1] - pad_size
                if in_end <= in_begin:
                    continue

                in_sub = x[in_begin:in_end, :]
                out[out_begin:out_end, j * self.input_size[1]:(j + 1) *
                    self.input_size[1]] += in_sub

C
chengduoZH 已提交
83 84 85 86
    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
C
chengduoZH 已提交
87 88 89
        if self.padding_trainable:
            self.check_grad(
                set(['X', 'PaddingData']), 'Out', max_relative_error=0.05)
C
chengduoZH 已提交
90 91 92 93 94 95 96 97 98

    def test_check_grad_no_filter(self):
        self.check_grad(
            ['X'],
            'Out',
            max_relative_error=0.05,
            no_grad_set=set(['PaddingData']))

    def test_check_grad_no_input(self):
C
chengduoZH 已提交
99 100 101 102 103 104
        if self.padding_trainable:
            self.check_grad(
                ['PaddingData'],
                'Out',
                max_relative_error=0.05,
                no_grad_set=set(['X']))
C
chengduoZH 已提交
105

C
chengduoZH 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
    def init_test_case(self):
        self.op_type = "sequence_project"
        self.input_row = 11
        self.context_start = 0
        self.context_length = 1
        self.padding_trainable = False
        self.context_stride = 1

        self.input_size = [self.input_row, 23]
        self.lod = [[0, 4, 5, 8, self.input_row]]


class TestSeqProjectCase1(TestSeqProject):
C
chengduoZH 已提交
119 120
    def init_test_case(self):
        self.op_type = "sequence_project"
C
chengduoZH 已提交
121
        self.input_row = 11
C
chengduoZH 已提交
122 123
        self.context_start = -1
        self.context_length = 3
124 125
        self.padding_trainable = True
        self.context_stride = 1
C
chengduoZH 已提交
126

C
chengduoZH 已提交
127 128 129 130
        self.input_size = [self.input_row, 23]
        self.lod = [[0, 4, 5, 8, self.input_row]]


C
chengduoZH 已提交
131
class TestSeqProjectCase2(TestSeqProject):
C
chengduoZH 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
    def init_test_case(self):
        self.op_type = "sequence_project"
        self.input_row = 25
        self.context_start = 2
        self.context_length = 3
        self.padding_trainable = True
        self.context_stride = 1

        self.input_size = [self.input_row, 23]
        idx = range(self.input_size[0])
        del idx[0]
        self.lod = [[0] + np.sort(random.sample(idx, 8)).tolist() +
                    [self.input_size[0]]]
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177


'''
class TestSeqProjectCases(TestSeqProject):
    def setUp(self):
        self.init_test_case()
        self.op_type = 'sequence_project'

        num = 0
        for context_start in [-5, -3, -1, 0, 3]:
            for context_length in [1, 2, 5, 7]:
                for batch_size in [1, 2, 5, 7]:
                    for padding_trainable in [False, True]:

                        if context_length == 1 and context_start == 0 and padding_trainable:
                            continue

                        self.context_start = context_start
                        self.context_length = context_length
                        self.padding_trainable = padding_trainable
                        self.input_size = [batch_size, 23]
                        x = np.random.uniform(0.1, 1,
                                              self.input_size).astype('float32')
                        self.lod = [[0, self.input_size[0]]]
                        if self.input_size[0] > 2:
                            idx = range(self.input_size[0])
                            del idx[0]
                            self.lod = [
                                [0] + np.sort(random.sample(idx, 2)).tolist() +
                                [self.input_size[0]]
                            ]

                        self.begin_pad = np.max([0, -self.context_start])
C
chengduoZH 已提交
178
                        self.end_pad = np.max([0, self.context_start + self.context_length - 1])
179
                        self.total_pad = self.begin_pad + self.end_pad
C
chengduoZH 已提交
180
                        if self.total_pad == 0:
181
                            self.total_pad = 1
C
chengduoZH 已提交
182 183 184
                        # PaddingData mast be not empty. Otherwise(EnforceNotMet: enforce numel() > 0 failed, 0 <= 0)
                        padding_data = np.random.uniform(
                            0.1, 1, [self.total_pad, self.input_size[1]]).astype('float32')
185 186 187

                        self.inputs = {
                            'X': (x, self.lod),
C
chengduoZH 已提交
188
                            'PaddingData': (padding_data, [[0, self.total_pad]])
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                        }
                        self.attrs = {
                            'context_start': self.context_start,
                            'context_length': self.context_length,
                            'padding_trainable': self.padding_trainable,
                            'context_stride': self.context_stride
                        }
                        out = np.zeros((self.input_size[0], self.input_size[1] *
                                        self.context_length)).astype('float32')
                        self.outputs = {'Out': out}
                        print num
                        print self.attrs
                        print batch_size
                        print padding_trainable
                        print "$$$$$$$$$$$$$"

                        self.compute()
                        self.test_check_output()
C
chengduoZH 已提交
207

208 209
                        num += 1
'''
C
chengduoZH 已提交
210 211 212

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