test_seq_expand.py 3.2 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


W
wanghaoshuang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
def repeat(list, starts, times, is_first):
    newlist = [list[0]]
    if is_first:
        for i, time in enumerate(times):
            size = list[i + 1] - list[i]
            newlist.append(newlist[-1] + size * time)
    else:
        for i, time in enumerate(times):
            start = list.index(starts[i])
            end = list.index(starts[i + 1]) + 1
            for t in range(time):
                for index in range(start, end - 1):
                    newlist.append(newlist[-1] + list[index + 1] - list[index])
    return newlist


def repeat_array(array, starts, times):
    newlist = []
    for i, time in enumerate(times):
        for t in range(time):
            newlist.extend(array[starts[i]:starts[i + 1]])
    return newlist


W
wanghaoshuang 已提交
30 31
class TestSeqExpand(OpTest):
    def set_data(self):
W
wanghaoshuang 已提交
32 33
        x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float32')
        self.inputs = {'X': x_data}
W
wanghaoshuang 已提交
34
        self.repeat = 2
W
wanghaoshuang 已提交
35 36

    def compute(self):
W
wanghaoshuang 已提交
37 38
        x = self.inputs['X']
        x_data, x_lod = x if type(x) == tuple else (x, None)
W
wanghaoshuang 已提交
39 40 41 42
        if not x_lod:
            x_lod = [[i for i in range(1 + x_data.shape[0])]]
        else:
            x_lod = [x_lod[0]] + x_lod
W
wanghaoshuang 已提交
43
        if self.repeat:
W
wanghaoshuang 已提交
44 45
            self.attrs = {'repeat': self.repeat}
            repeats = (len(x_lod[0]) - 1) * [self.repeat]
W
wanghaoshuang 已提交
46
        else:
W
wanghaoshuang 已提交
47 48 49 50 51 52 53 54
            y_data, y_lod = self.inputs['Y']
            repeats = [((y_lod[0][i + 1] - y_lod[0][i]) /
                        (x_lod[0][i + 1] - x_lod[0][i]))
                       for i in range(len(y_lod[0]) - 1)]
        out_lod = [repeat(x_lod[0], x_lod[0], repeats, True)] + [
            repeat(lod, x_lod[0], repeats, False) for lod in x_lod[1:]
        ]
        out = repeat_array(x_data.tolist(), x_lod[0], repeats)
W
wanghaoshuang 已提交
55
        self.outputs = {'Out': out}
W
wanghaoshuang 已提交
56 57

    def setUp(self):
W
wanghaoshuang 已提交
58
        self.op_type = 'seq_expand'
W
wanghaoshuang 已提交
59 60 61 62 63 64
        self.set_data()
        self.compute()

    def test_check_output(self):
        self.check_output()

W
wanghaoshuang 已提交
65 66
    def test_check_grad(self):
        self.check_grad(["X"], "Out")
W
wanghaoshuang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79


class TestSeqExpandCase1(TestSeqExpand):
    def set_data(self):
        x_data = np.random.uniform(0.1, 1, [7, 1]).astype('float32')
        x_lod = [[0, 5, 7], [0, 2, 5, 7]]
        self.inputs = {'X': (x_data, x_lod)}
        self.repeat = 2


class TestSeqExpandCase2(TestSeqExpand):
    def set_data(self):
        x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float32')
W
wanghaoshuang 已提交
80
        self.inputs = {'X': x_data}
W
wanghaoshuang 已提交
81 82 83 84 85 86 87 88
        self.repeat = 2


class TestSeqExpandCase3(TestSeqExpand):
    def set_data(self):
        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]]
W
wanghaoshuang 已提交
89
        self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
W
wanghaoshuang 已提交
90 91 92 93 94 95 96 97 98 99 100
        self.repeat = None


class TestSeqExpandCase4(TestSeqExpand):
    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')
        y_lod = [[0, 4, 13], [0, 2, 4, 7, 10, 13]]
        self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
        self.repeat = None
W
wanghaoshuang 已提交
101 102 103 104


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