test_seq_conv.py 6.8 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import unittest
import numpy as np
import random
from op_test import OpTest


class TestSeqProject(OpTest):
    def setUp(self):
        self.init_test_case()
        self.op_type = 'sequence_conv'

        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

        # one level, batch size
        x = np.random.uniform(0.1, 1, [self.input_size[0],
                                       self.input_size[1]]).astype('float32')
C
chengduoZH 已提交
23 24 25
        w = np.random.uniform(0.1, 1, [
            self.context_length * self.input_size[1], self.output_represention
        ]).astype('float32')
C
chengduoZH 已提交
26 27 28 29 30 31 32

        begin_pad = np.max([0, -self.context_start])
        end_pad = np.max([0, self.context_start + self.context_length - 1])
        total_pad = begin_pad + end_pad
        padding_data = np.random.uniform(
            0.1, 1, [total_pad, self.input_size[1]]).astype('float32')
        self.pad_data = padding_data
C
chengduoZH 已提交
33 34
        self.inputs = {
            'X': (x, self.lod),
C
chengduoZH 已提交
35
            'Filter': w,
C
chengduoZH 已提交
36
        }
C
chengduoZH 已提交
37 38 39 40 41 42 43 44 45 46
        self.inputs_val = ['X', 'Filter']
        self.inputs_val_no_x = ['Filter']
        self.inputs_val_no_f = ['X']

        if total_pad != 0:
            self.inputs['PaddingData'] = padding_data
            self.inputs_val = ['X', 'PaddingData', 'Filter']
            self.inputs_val_no_x = ['PaddingData', 'Filter']
            self.inputs_val_no_f = ['PaddingData', 'X']

C
chengduoZH 已提交
47
        self.attrs = {
C
chengduoZH 已提交
48 49 50 51
            'contextStart': self.context_start,
            'contextLength': self.context_length,
            'paddingTrainable': self.padding_trainable,
            'contextStride': self.context_stride
C
chengduoZH 已提交
52
        }
C
chengduoZH 已提交
53 54
        out = np.zeros(
            (self.input_size[0], self.output_represention)).astype('float32')
C
chengduoZH 已提交
55 56 57 58 59 60
        self.outputs = {'Out': out}
        self.compute()

    def compute(self):
        x, lod = self.inputs['X']
        filter = self.inputs['Filter']
C
chengduoZH 已提交
61
        pading_data = self.pad_data
C
chengduoZH 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        out = np.zeros((self.input_size[0], self.context_length *
                        self.input_size[1])).astype('float32')
        lod = lod[0]
        begin_pad = np.max([0, -self.context_start])

        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:
                        sub_w = pading_data[j:j + pad_size, :]
                        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:
                        sub_w = pading_data[begin_pad + self.context_start + j -
                                            pad_size:begin_pad +
                                            self.context_start + j, :]
                        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 已提交
100
        np.dot(out, filter, out=self.outputs['Out'])
C
chengduoZH 已提交
101 102 103 104 105 106 107

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        if self.padding_trainable:
            self.check_grad(
C
chengduoZH 已提交
108
                set(self.inputs_val), 'Out', max_relative_error=0.05)
C
chengduoZH 已提交
109 110 111 112 113 114

    def test_check_grad_input(self):
        self.check_grad(
            ['X'],
            'Out',
            max_relative_error=0.05,
C
chengduoZH 已提交
115
            no_grad_set=set(self.inputs_val_no_x))
C
chengduoZH 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129

    def test_check_grad_padding_data(self):
        if self.padding_trainable:
            self.check_grad(
                ['PaddingData'],
                'Out',
                max_relative_error=0.05,
                no_grad_set=set(['X', 'Filter']))

    def test_check_grad_Filter(self):
        self.check_grad(
            ['Filter'],
            'Out',
            max_relative_error=0.05,
C
chengduoZH 已提交
130
            no_grad_set=set(self.inputs_val_no_f))
C
chengduoZH 已提交
131

C
chengduoZH 已提交
132
    def test_check_grad_input_filter(self):
C
chengduoZH 已提交
133 134 135 136 137 138
        if self.padding_trainable:
            self.check_grad(
                ['X', 'Filter'],
                'Out',
                max_relative_error=0.05,
                no_grad_set=set(['PaddingData']))
C
chengduoZH 已提交
139 140 141 142

    def test_check_grad_padding_input(self):
        if self.padding_trainable:
            self.check_grad(
C
chengduoZH 已提交
143
                self.inputs_val_no_f,
C
chengduoZH 已提交
144 145 146 147 148 149 150
                'Out',
                max_relative_error=0.05,
                no_grad_set=set(['Filter']))

    def test_check_grad_padding_filter(self):
        if self.padding_trainable:
            self.check_grad(
C
chengduoZH 已提交
151
                self.inputs_val_no_x,
C
chengduoZH 已提交
152 153 154 155
                'Out',
                max_relative_error=0.05,
                no_grad_set=set(['X']))

C
chengduoZH 已提交
156 157 158 159 160 161 162 163 164
    def init_test_case(self):
        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]]
C
chengduoZH 已提交
165
        self.output_represention = 8  # output feature size
C
chengduoZH 已提交
166 167 168 169 170 171 172 173 174 175 176 177


class TestSeqProjectCase1(TestSeqProject):
    def init_test_case(self):
        self.input_row = 11
        self.context_start = -1
        self.context_length = 3
        self.padding_trainable = True
        self.context_stride = 1

        self.input_size = [self.input_row, 23]
        self.lod = [[0, 4, 5, 8, self.input_row]]
C
chengduoZH 已提交
178
        self.output_represention = 8  # output feature size
C
chengduoZH 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193


class TestSeqProjectCase2(TestSeqProject):
    def init_test_case(self):
        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]]]
C
chengduoZH 已提交
194
        self.output_represention = 8  # output feature size
C
chengduoZH 已提交
195 196 197 198


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