17
17
import unittest
18
18
import numpy as np
19
19
import math
20
+ # import paddle.fluid as fluid
21
+ # import paddle.fluid.core as core
22
+ # from op_builder import OpBuilder
20
23
from op_test import OpTest
21
24
22
25
np .random .seed (100 )
@@ -51,7 +54,7 @@ def cal_index(self, bit):
51
54
52
55
def get_length (self ):
53
56
length = 0
54
- for ele in self .ptable_ [self .index_ ]:
57
+ for ele in self .ptable_ [self .index_ ]: # find the first -1 to stop trace
55
58
56
59
if ele >= 0 :
57
60
length = length + 1
@@ -71,12 +74,10 @@ def hsigmoid(x, w, label, bias, num_classes):
71
74
pre_sum = np .zeros ((batch_size , 1 ))
72
75
out = np .zeros ((batch_size , 1 )).astype ("float32" )
73
76
for i in range (batch_size ):
74
- #print("\n leaf {leaf}: \n".format(leaf = label[i]))
75
77
code_table = CodeTable (num_classes , label [i ])
76
78
length = code_table .get_length ()
77
79
for j in range (length ):
78
80
idx = code_table .cal_index (j )
79
- #print("index {index} ".format(index = j))
80
81
pre_output [i ][j ] += bias [0 ][idx ]
81
82
for i in range (batch_size ):
82
83
code_table = CodeTable (num_classes , label [i ])
@@ -87,13 +88,12 @@ def hsigmoid(x, w, label, bias, num_classes):
87
88
# clip[-40.0, 40.0]
88
89
pre_output = np .clip (pre_output , - 40.0 , 40.0 )
89
90
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
91
+ pre_output = - 1 * pre_output
90
92
for i in range (batch_size ):
91
- #print("\n leaf {leaf}: \n".format(leaf = label[i]))
92
93
code_table = CodeTable (num_classes , label [i ])
93
94
length = code_table .get_length ()
94
95
sum = 0.0
95
96
for j in range (length ):
96
- #print("bit {bit} ".format(bit = code_table.cal_bit(j)))
97
97
if code_table .cal_bit (j ):
98
98
sum += pre_output [i ][j ]
99
99
out [i ] = - 1.0 * sum
@@ -108,6 +108,7 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
108
108
batch_size = x .shape [0 ]
109
109
code_length = len (ptable [0 ])
110
110
code_table = [0 for _ in range (code_length )]
111
+ # init pre_out with shape [N, code_length]
111
112
pre_output = np .zeros ((batch_size , code_length ))
112
113
pre_sum = np .zeros ((batch_size , 1 ))
113
114
out = np .zeros ((batch_size , 1 )).astype ("float32" )
@@ -125,6 +126,7 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
125
126
pre_output [i ][j ] += np .dot (w [idx ], x [i ])
126
127
# clip[-40.0, 40.0]
127
128
pre_output = np .clip (pre_output , - 40.0 , 40.0 )
129
+ pre_output = - 1 * pre_output
128
130
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
129
131
for i in range (batch_size ):
130
132
code_table = CodeTableWithCustomTree (ptable , pcode , i )
@@ -141,26 +143,27 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
141
143
return pre_output , out
142
144
143
145
144
- # class TestHSigmoidOp(OpTest):
145
- # def setUp(self):
146
- # self.op_type = "hierarchical_sigmoid"
147
- # num_classes = 6
148
- # feature_size = 8
149
- # batch_size = 7
150
- # x = np.random.random((batch_size, feature_size)).astype("float32")
151
- # w = np.random.random((num_classes - 1, feature_size)).astype("float32")
152
- # label = np.random.randint(0, num_classes, (batch_size, 1))
153
- # bias = np.random.random((1, num_classes - 1)).astype("float32")
154
- # self.attrs = {'num_classes': num_classes}
155
- # self.inputs = {'X': x, 'W': w, 'Label': label, 'Bias': bias}
156
- # pre_output, out = hsigmoid(x, w, label, bias, num_classes)
157
- # self.outputs = {'PreOut': pre_output, 'Out': out}
146
+ class TestHSigmoidOp (OpTest ):
147
+ def setUp (self ):
148
+ self .op_type = "hierarchical_sigmoid"
149
+ num_classes = 6
150
+ feature_size = 8
151
+ batch_size = 4
152
+ x = np .random .random ((batch_size , feature_size )).astype ("float32" ) * 2
153
+ w = np .random .random (
154
+ (num_classes - 1 , feature_size )).astype ("float32" ) * 2
155
+ label = np .random .randint (0 , num_classes , (batch_size , 1 ))
156
+ bias = np .random .random ((1 , num_classes - 1 )).astype ("float32" )
157
+ self .attrs = {'num_classes' : num_classes }
158
+ self .inputs = {'X' : x , 'W' : w , 'Label' : label , 'Bias' : bias }
159
+ pre_output , out = hsigmoid (x , w , label , bias , num_classes )
160
+ self .outputs = {'PreOut' : pre_output , 'Out' : out }
158
161
159
- # def test_check_output(self):
160
- # self.check_output()
162
+ def test_check_output (self ):
163
+ self .check_output ()
161
164
162
- # def test_check_grad(self):
163
- # self.check_grad(['Bias', 'X', 'W'], ['Out'], no_grad_set=set('Label'))
165
+ def test_check_grad (self ):
166
+ self .check_grad (['Bias' , 'X' , 'W' ], ['Out' ], no_grad_set = set ('Label' ))
164
167
165
168
166
169
class TestHSigmoidOpWithCostumTree (OpTest ):
@@ -169,9 +172,9 @@ def setUp(self):
169
172
num_classes = 6 #using 1,2,3,4,5,6 to build a huffman tree and select 1,2,5,6 as sample
170
173
feature_size = 8
171
174
batch_size = 4
172
- x = np .random .random ((batch_size , feature_size )).astype ("float32" ) * 10
175
+ x = np .random .random ((batch_size , feature_size )).astype ("float32" ) * 2
173
176
w = np .random .random (
174
- (num_classes - 1 , feature_size )).astype ("float32" ) * 10
177
+ (num_classes - 1 , feature_size )).astype ("float32" ) * 2
175
178
label = np .array ([0 , 1 , 4 , 5 ])
176
179
ptable = np .array (
177
180
[(0 , 2 , - 1 , - 1 , - 1 ), (0 , 1 , 3 , - 1 , - 1 ), (0 , 1 , 4 , - 1 , - 1 ),
0 commit comments