test_sequence_expand.py 2.6 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#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.
W
wanghaoshuang 已提交
14 15 16 17 18
import unittest
import numpy as np
from op_test import OpTest


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

    def compute(self):
W
wanghaoshuang 已提交
27 28
        x = self.inputs['X']
        x_data, x_lod = x if type(x) == tuple else (x, None)
W
wanghaoshuang 已提交
29
        n = 1 + x_data.shape[0] if not x_lod else len(x_lod[0])
W
wanghaoshuang 已提交
30 31 32 33
        y_data, y_lod = self.inputs['Y']
        repeats = [((y_lod[-1][i + 1] - y_lod[-1][i]))
                   for i in range(len(y_lod[-1]) - 1)]
        out = x_data.repeat(repeats, axis=0)
W
wanghaoshuang 已提交
34
        self.outputs = {'Out': out}
W
wanghaoshuang 已提交
35 36

    def setUp(self):
W
wanghaoshuang 已提交
37
        self.op_type = 'sequence_expand'
W
wanghaoshuang 已提交
38 39 40 41 42 43
        self.set_data()
        self.compute()

    def test_check_output(self):
        self.check_output()

W
wanghaoshuang 已提交
44 45
    def test_check_grad(self):
        self.check_grad(["X"], "Out")
W
wanghaoshuang 已提交
46 47


W
wanghaoshuang 已提交
48
class TestSequenceExpandCase1(TestSequenceExpand):
W
wanghaoshuang 已提交
49 50 51 52
    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 已提交
53
        y_lod = [[0, 2, 5], [0, 2, 4, 7, 10, 13]]
W
wanghaoshuang 已提交
54
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
W
wanghaoshuang 已提交
55 56


W
wanghaoshuang 已提交
57
class TestSequenceExpandCase2(TestSequenceExpand):
W
wanghaoshuang 已提交
58 59 60 61 62 63
    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_lod = [[0, 2]]
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
W
wanghaoshuang 已提交
64 65


W
wanghaoshuang 已提交
66
class TestSequenceExpandCase3(TestSequenceExpand):
W
wanghaoshuang 已提交
67 68 69 70 71 72 73 74
    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)}


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