test_seq_pool.py 3.2 KB
Newer Older
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


6 7 8 9 10 11 12 13 14 15 16 17
class SeqPoolType(OpTest):
    AVERAGE = 0
    SUM = 1
    SQRT = 2
    MAX = 3
    LAST = 4
    FIRST = 5


class TestSeqAvgPool(OpTest):
    def set_data(self):
        self.op_type = 'sequence_pool'
18 19 20
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
        lod = [[0, 4, 5, 8, 11]]
21
        self.inputs = {'X': (x, lod)}
22 23

        out = np.zeros((4, 23)).astype('float32')
24 25 26 27 28 29
        self.outputs = {'Out': out}

    def compute(self):
        self.attrs = {'strategy': SeqPoolType.AVERAGE}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
30 31 32 33
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.mean(axis=0)

34 35 36
    def setUp(self):
        self.set_data()
        self.compute()
37 38 39 40 41 42 43 44

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(["X"], "Out")


45 46 47
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
48 49 50
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
51
        self.inputs = {'X': (x, lod)}
52 53

        out = np.zeros((4, 3, 17)).astype('float32')
54 55 56 57 58 59
        self.outputs = {'Out': out}

    def compute(self):
        self.attrs = {'strategy': SeqPoolType.AVERAGE}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
60 61 62 63 64
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.mean(axis=0), (3, 17))


65 66 67 68 69 70 71 72
class TestSeqSumPool(TestSeqAvgPool):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SUM}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.sum(axis=0)
73

74 75 76 77 78 79 80 81 82

class TestSeqSumPool2D(TestSeqAvgPool2D):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SUM}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.sum(axis=0), (3, 17))
83 84


L
Luo Tao 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
class TestSeqSqrtPool(TestSeqAvgPool):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SQRT}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            len = lod[0][i + 1] - lod[0][i]
            out[i] = sub_x.sum(axis=0) / np.sqrt(len)


class TestSeqSqrtPool2D(TestSeqAvgPool2D):
    def compute(self):
        self.attrs = {'strategy': SeqPoolType.SQRT}
        x, lod = self.inputs['X']
        out = self.outputs['Out']
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            len = lod[0][i + 1] - lod[0][i]
            out[i] = np.reshape(sub_x.sum(axis=0) / np.sqrt(len), (3, 17))

    def test_check_grad(self):
        self.check_grad(["X"], "Out", max_relative_error=0.06)


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