test_hsigmoid_op.py 982 字节
Newer Older
Y
Yancey1989 已提交
1 2 3 4 5 6 7
import unittest
import numpy as np
from op_test import OpTest


class TestHSigmoidOp(OpTest):
    def setUp(self):
Y
Yancey1989 已提交
8
        self.op_type = "hierarchical_sigmoid"
Y
Yancey1989 已提交
9 10 11 12
        num_classes = 6
        embded_size = 10
        batch_size = 5
        x = np.random.random((batch_size, embded_size)).astype("float32")
Y
Yancey1989 已提交
13
        w = np.random.random(
Y
Yancey1989 已提交
14
            (batch_size, num_classes - 1, embded_size)).astype("float32")
Y
Yancey1989 已提交
15
        ids = np.random.randint(0, num_classes, batch_size)
Y
Yancey1989 已提交
16
        bias = np.random.random((1, num_classes - 1)).astype("float32")
Y
Yancey1989 已提交
17
        self.inputs = {'X': x, 'W': w, 'Ids': ids, 'Bias': bias}
Y
Yancey1989 已提交
18
        self.attrs = {'num_classes': num_classes}
Y
Yancey1989 已提交
19 20 21
        self.outputs = {
            'Out': np.random.random((batch_size, 1)).astype("float32")
        }
Y
Yancey1989 已提交
22 23 24 25 26

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
Y
Yancey1989 已提交
27
        self.check_grad(['X', 'W', 'Bias'], 'Out', no_grad_set=set('Ids'))
Y
Yancey1989 已提交
28 29 30 31


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