test_lookup_table.py 953 字节
Newer Older
1 2 3 4 5 6
import unittest
import numpy as np
from op_test_util import OpTestMeta
from gradient_checker import GradientChecker, create_op


Q
qingqing01 已提交
7
class TestLookupTableOp(unittest.TestCase):
8 9 10 11 12
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = 'lookup_table'
        table = np.random.random((17, 31)).astype('float32')
D
dangqingqing 已提交
13
        ids = np.random.randint(0, 17, 4).astype('int32')
14 15 16 17
        self.inputs = {'W': table, 'Ids': ids}
        self.outputs = {'Out': table[ids]}


Q
qingqing01 已提交
18
class TestLookupTableGradOp(GradientChecker):
19 20 21
    def test_grad(self):
        op = create_op('lookup_table')
        table = np.random.random((17, 31)).astype('float32')
D
dangqingqing 已提交
22
        ids = np.random.randint(0, 17, 4).astype('int32')
23
        inputs = {'W': table, 'Ids': ids}
24 25
        # comapre gradients 
        self.compare_grad(op, inputs, set(['Ids']))
26 27 28 29 30 31
        # check gradients 
        self.check_grad(op, inputs, set('W'), 'Out')


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