test_sequence_expand.py 4.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

W
wanghaoshuang 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
W
wanghaoshuang 已提交
20 21


W
wanghaoshuang 已提交
22
class TestSequenceExpand(OpTest):
W
wanghaoshuang 已提交
23
    def set_data(self):
24 25
        x_data = np.random.uniform(0.1, 1, [3, 40]).astype('float64')
        y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float64')
26
        y_lod = [[1, 3, 4]]
W
wanghaoshuang 已提交
27
        self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
W
wanghaoshuang 已提交
28 29

    def compute(self):
W
wanghaoshuang 已提交
30 31
        x = self.inputs['X']
        x_data, x_lod = x if type(x) == tuple else (x, None)
W
wanghaoshuang 已提交
32
        y_data, y_lod = self.inputs['Y']
Y
yangyaming 已提交
33 34 35 36 37 38 39 40 41

        if hasattr(self, 'attrs'):
            ref_level = self.attrs['ref_level']
        else:
            ref_level = len(y_lod) - 1

        out = np.zeros(shape=((0, ) + x_data.shape[1:]), dtype=x_data.dtype)

        if x_lod is None:
42 43
            # x_idx = [i for i in xrange(x_data.shape[0] + 1)]
            x_idx = [1] * x_data.shape[0]
Y
yangyaming 已提交
44 45
        else:
            x_idx = x_lod[0]
46 47 48
            out_lod = [[]]

        offset = 0
49
        for i in range(len(y_lod[ref_level])):
50 51
            repeat_num = y_lod[ref_level][i]
            x_len = x_idx[i]
Y
yangyaming 已提交
52 53

            if repeat_num > 0:
54
                x_sub = x_data[offset:(offset + x_len), :]
D
dzhwinter 已提交
55 56 57 58
                stacked_x_sub = x_sub
                for r in range(repeat_num - 1):
                    stacked_x_sub = np.vstack((stacked_x_sub, x_sub))
                out = np.vstack((out, stacked_x_sub))
Y
yangyaming 已提交
59
                if x_lod is not None:
60
                    for j in range(repeat_num):
61 62
                        out_lod[0].append(x_len)
            offset += x_len
Y
yangyaming 已提交
63 64 65 66 67

        if x_lod is None:
            self.outputs = {'Out': out}
        else:
            self.outputs = {'Out': (out, out_lod)}
W
wanghaoshuang 已提交
68 69

    def setUp(self):
W
wanghaoshuang 已提交
70
        self.op_type = 'sequence_expand'
W
wanghaoshuang 已提交
71 72 73 74
        self.set_data()
        self.compute()

    def test_check_output(self):
H
hong 已提交
75
        self.check_output(check_dygraph=False)
W
wanghaoshuang 已提交
76

W
wanghaoshuang 已提交
77
    def test_check_grad(self):
H
hong 已提交
78
        self.check_grad(["X"], "Out", check_dygraph=False)
W
wanghaoshuang 已提交
79 80


W
wanghaoshuang 已提交
81
class TestSequenceExpandCase1(TestSequenceExpand):
W
wanghaoshuang 已提交
82
    def set_data(self):
83 84
        x_data = np.random.uniform(0.1, 1, [5, 1]).astype('float64')
        y_data = np.random.uniform(0.1, 1, [13, 1]).astype('float64')
85
        y_lod = [[2, 3], [2, 2, 3, 3, 3]]
Y
yangyaming 已提交
86
        self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
87
        self.attrs = {'ref_level': 1}
W
wanghaoshuang 已提交
88 89


W
wanghaoshuang 已提交
90
class TestSequenceExpandCase2(TestSequenceExpand):
W
wanghaoshuang 已提交
91
    def set_data(self):
92
        x_data = np.random.uniform(0.1, 1, [1, 2, 2]).astype('float64')
93
        x_lod = [[1]]
94
        y_data = np.random.uniform(0.1, 1, [2, 2, 2]).astype('float64')
95
        y_lod = [[2], [1, 1]]
W
wanghaoshuang 已提交
96
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
Y
yangyaming 已提交
97
        self.attrs = {'ref_level': 0}
W
wanghaoshuang 已提交
98 99


W
wanghaoshuang 已提交
100
class TestSequenceExpandCase3(TestSequenceExpand):
W
wanghaoshuang 已提交
101
    def set_data(self):
102
        x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float64')
103
        x_lod = [[1, 1, 1, 1]]
104
        y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float64')
105
        y_lod = [[2, 2, 2, 2]]
W
wanghaoshuang 已提交
106 107 108
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


109 110
class TestSequenceExpandCase4(TestSequenceExpand):
    def set_data(self):
D
dzhwinter 已提交
111
        data = np.random.uniform(0.1, 1, [5 * 2, 1])
112
        x_data = np.array(data).reshape([5, 2]).astype('float64')
113
        x_lod = [[2, 3]]
114
        y_data = np.random.uniform(0.1, 1, [5, 1]).astype('float64')
115
        y_lod = [[2], [2, 3]]
116 117 118
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


119 120
class TestSequenceExpandCase5(TestSequenceExpand):
    def set_data(self):
121 122
        x_data = np.random.uniform(0.1, 1, [6, 1]).astype('float64')
        y_data = np.random.uniform(0.1, 1, [13, 1]).astype('float64')
123 124 125 126 127 128 129
        y_lod = [[2, 4], [2, 2, 3, 0, 3, 3]]
        self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
        self.attrs = {'ref_level': 1}


class TestSequenceExpandCase6(TestSequenceExpand):
    def set_data(self):
130
        x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float64')
131
        x_lod = [[1, 1, 0, 1, 1]]
132
        y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float64')
133 134 135 136
        y_lod = [[0, 2, 4, 2, 0]]
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


W
wanghaoshuang 已提交
137 138
if __name__ == '__main__':
    unittest.main()