test_seq_pool.py 2.3 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 85 86


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