test_sequence_expand.py 4.1 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.

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


W
wanghaoshuang 已提交
20
class TestSequenceExpand(OpTest):
W
wanghaoshuang 已提交
21
    def set_data(self):
D
dzhwinter 已提交
22 23 24 25 26 27 28 29
        x = [i / 10.0 for i in range(3)]
        y = [i / 10.0 for i in range(8)]
        x_data = np.array(x).reshape(3, 1).astype('float32')
        y_data = np.array(y).reshape(8, 1).astype('float32')
        print(x_data)
        print(y_data)
        # x_data = np.random.uniform(0.1, 1, [3, 1]).astype('float32')
        # y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float32')
W
wanghaoshuang 已提交
30 31
        y_lod = [[0, 1, 4, 8]]
        self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
W
wanghaoshuang 已提交
32 33

    def compute(self):
W
wanghaoshuang 已提交
34 35
        x = self.inputs['X']
        x_data, x_lod = x if type(x) == tuple else (x, None)
W
wanghaoshuang 已提交
36
        y_data, y_lod = self.inputs['Y']
Y
yangyaming 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

        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:
            x_idx = [i for i in xrange(x_data.shape[0] + 1)]
        else:
            x_idx = x_lod[0]
            out_lod = [[0]]

        for i in xrange(1, len(y_lod[ref_level])):
            repeat_num = y_lod[ref_level][i] - y_lod[ref_level][i - 1]
            x_len = x_idx[i] - x_idx[i - 1]
            if repeat_num > 0:
                x_sub = x_data[x_idx[i - 1]:x_idx[i], :]
                x_sub = np.repeat(x_sub, repeat_num, axis=0)
                out = np.vstack((out, x_sub))
                if x_lod is not None:
                    for j in xrange(repeat_num):
                        out_lod[0].append(out_lod[0][-1] + x_len)

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

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

    def test_check_output(self):
        self.check_output()

W
wanghaoshuang 已提交
75 76
    def test_check_grad(self):
        self.check_grad(["X"], "Out")
W
wanghaoshuang 已提交
77 78


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


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


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


108 109
class TestSequenceExpandCase4(TestSequenceExpand):
    def set_data(self):
Y
yangyaming 已提交
110 111 112
        data = [0.1, 0.3, 0.2, 0.15, 0.25, 0.2, 0.15, 0.25, 0.1, 0.3]
        x_data = np.array(data).reshape([5, 2]).astype('float32')
        x_lod = [[0, 2, 5]]
113 114 115 116 117
        y_data = np.random.uniform(0.1, 1, [2, 1]).astype('float32')
        y_lod = [[0, 1, 2], [0, 1, 2]]
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


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