test_hsigmoid_op.py 1.1 KB
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 13 14
        num_classes = 6
        embded_size = 10
        batch_size = 5
        x = np.random.random((batch_size, embded_size)).astype("float32")
        parameter = np.random.random(
            (batch_size, num_classes - 1, embded_size)).astype("float32")
Y
Yancey1989 已提交
15 16
        label = np.random.randint(0, num_classes, batch_size)
        bias = np.random.random((1, num_classes - 1)).astype("float32")
Y
Yancey1989 已提交
17 18 19 20 21 22 23
        self.inputs = {
            'X': x,
            'Parameters': parameter,
            'Label': label,
            'Bias': bias
        }
        self.attrs = {'num_classes': num_classes}
Y
Yancey1989 已提交
24 25 26
        self.outputs = {
            'Out': np.random.random((batch_size, 1)).astype("float32")
        }
Y
Yancey1989 已提交
27 28 29 30 31

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
Y
Yancey1989 已提交
32 33 34 35
        self.check_grad(
            ['X', 'Parameters', 'Label', 'Bias'],
            'Out',
            no_grad_set=set(['Label']))
Y
Yancey1989 已提交
36 37 38 39


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