@@ -43,9 +43,9 @@ def cal_bit(self, bit):
43
43
44
44
45
45
class CodeTableWithCustomTree (object ):
46
- def __init__ (self , ptable , pcode , index ):
47
- self .ptable_ = ptable
48
- self .pcode_ = pcode
46
+ def __init__ (self , path_table , path_code , index ):
47
+ self .ptable_ = path_table
48
+ self .pcode_ = path_code
49
49
self .index_ = index
50
50
51
51
def cal_index (self , bit ):
@@ -102,23 +102,24 @@ def hsigmoid(x, w, label, bias, num_classes):
102
102
return pre_output , out
103
103
104
104
105
- def hsigmoidWithCustomTree (x , w , ptable , pcode , label , bias , num_classes ):
105
+ def hsigmoidWithCustomTree (x , w , path_table , path_code , label , bias ,
106
+ num_classes ):
106
107
batch_size = x .shape [0 ]
107
- code_length = len (ptable [0 ])
108
+ code_length = len (path_table [0 ])
108
109
code_table = [0 for _ in range (code_length )]
109
110
# init pre_out with shape [N, code_length]
110
111
pre_output = np .zeros ((batch_size , code_length ))
111
112
pre_sum = np .zeros ((batch_size , 1 ))
112
113
out = np .zeros ((batch_size , 1 )).astype ("float32" )
113
114
if isinstance (bias , np .ndarray ):
114
115
for i in range (batch_size ):
115
- code_table = CodeTableWithCustomTree (ptable , pcode , i )
116
+ code_table = CodeTableWithCustomTree (path_table , path_code , i )
116
117
length = code_table .get_length ()
117
118
for j in range (length ):
118
119
idx = code_table .cal_index (j )
119
120
pre_output [i ][j ] += bias [idx ][0 ]
120
121
for i in range (batch_size ):
121
- code_table = CodeTableWithCustomTree (ptable , pcode , i )
122
+ code_table = CodeTableWithCustomTree (path_table , path_code , i )
122
123
length = code_table .get_length ()
123
124
for j in range (length ):
124
125
idx = code_table .cal_index (j )
@@ -127,7 +128,7 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
127
128
pre_output = np .clip (pre_output , - 40.0 , 40.0 )
128
129
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
129
130
for i in range (batch_size ):
130
- code_table = CodeTableWithCustomTree (ptable , pcode , i )
131
+ code_table = CodeTableWithCustomTree (path_table , path_code , i )
131
132
length = code_table .get_length ()
132
133
sum = 0.0
133
134
for j in range (length ):
@@ -173,24 +174,24 @@ def setUp(self):
173
174
x = np .random .random ((batch_size , feature_size )).astype ("float32" )
174
175
w = np .random .random ((num_classes - 1 , feature_size )).astype ("float32" )
175
176
label = np .array ([0 , 1 , 4 , 5 ])
176
- ptable = np .array (
177
+ path_table = np .array (
177
178
[(0 , 2 , - 1 , - 1 , - 1 ), (0 , 1 , 3 , - 1 , - 1 ), (0 , 1 , 4 , - 1 , - 1 ),
178
179
(0 , 2 , - 1 , - 1 ,
179
180
- 1 )]) #np.array to store 1,2,5,6s' non-leaf path(root -> leaf)
180
- pcode = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
181
+ path_code = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
181
182
1 , 0 , 0 , - 1 , - 1 ), (0 , 1 , - 1 , - 1 , - 1 )]) #np.array to store
182
183
bias = np .random .random ((num_classes - 1 , 1 )).astype ("float32" )
183
184
self .attrs = {'num_classes' : num_classes , 'is_sparse' : True }
184
185
self .inputs = {
185
186
'X' : x ,
186
187
'W' : w ,
187
- 'PTable' : ptable ,
188
- 'PathCode' : pcode ,
188
+ 'PTable' : path_table ,
189
+ 'PathCode' : path_code ,
189
190
'Label' : label ,
190
191
'Bias' : bias
191
192
}
192
- pre_output , out = hsigmoidWithCustomTree (x , w , ptable , pcode , label ,
193
- bias , num_classes )
193
+ pre_output , out = hsigmoidWithCustomTree (x , w , path_table , path_code ,
194
+ label , bias , num_classes )
194
195
self .outputs = {'PreOut' : pre_output , 'Out' : out }
195
196
196
197
def test_check_output (self ):
@@ -200,11 +201,13 @@ def test_check_output(self):
200
201
class TestHSigmoidOpWithSparseGrad (unittest .TestCase ):
201
202
def hs_net_conf (self , is_sparse ):
202
203
input_word = fluid .layers .data (name = "x" , shape = [1 ], dtype = 'int64' )
203
- ptable = fluid .layers .data (name = 'ptable' , shape = [3 ], dtype = 'int64' )
204
- pcode = fluid .layers .data (name = 'pcode' , shape = [3 ], dtype = 'int64' )
204
+ path_table = fluid .layers .data (
205
+ name = 'path_table' , shape = [3 ], dtype = 'int64' )
206
+ path_code = fluid .layers .data (
207
+ name = 'path_code' , shape = [3 ], dtype = 'int64' )
205
208
label = fluid .layers .data (name = 'label' , shape = [1 ], dtype = 'int64' )
206
209
207
- data_list = [input_word , ptable , pcode , label ]
210
+ data_list = [input_word , path_table , path_code , label ]
208
211
209
212
emb = fluid .layers .embedding (
210
213
input = input_word ,
@@ -218,9 +221,9 @@ def hs_net_conf(self, is_sparse):
218
221
label = label ,
219
222
bias_attr = True ,
220
223
non_leaf_num = 3 ,
221
- ptable = ptable ,
222
- pcode = pcode ,
223
- is_costum = True ,
224
+ path_table = path_table ,
225
+ path_code = path_code ,
226
+ is_custom = True ,
224
227
is_sparse = is_sparse )
225
228
226
229
avg_cost = fluid .layers .reduce_mean (cost )
@@ -232,8 +235,8 @@ def training_test(self, is_sparse):
232
235
start_up = fluid .default_startup_program ()
233
236
start_up .random_seed = 1 # Fix random seed
234
237
x = np .arange (6 ).reshape (6 )
235
- ptable = np .array ([(1 , 2 , - 1 ), (1 , 2 , - 1 )])
236
- pcode = np .array ([(1 , 0 , - 1 ), (0 , 0 , - 1 )])
238
+ path_table = np .array ([(1 , 2 , - 1 ), (1 , 2 , - 1 )])
239
+ path_code = np .array ([(1 , 0 , - 1 ), (0 , 0 , - 1 )])
237
240
label = np .array ([1 , 4 ])
238
241
239
242
loss , data_list = self .hs_net_conf (is_sparse )
@@ -248,8 +251,8 @@ def training_test(self, is_sparse):
248
251
exe .run (start_up )
249
252
result = list ()
250
253
for i in range (10 ):
251
- data = [([[x [i % 2 ]]], [list (ptable [i % 2 ])],
252
- [list (pcode [i % 2 ])], [label [i % 2 ]])]
254
+ data = [([[x [i % 2 ]]], [list (path_table [i % 2 ])],
255
+ [list (path_code [i % 2 ])], [label [i % 2 ]])]
253
256
254
257
loss_val = exe .run (main_program ,
255
258
feed = feeder .feed (data ),
@@ -273,24 +276,24 @@ def setUp(self):
273
276
w = np .random .random (
274
277
(num_classes - 1 , feature_size )).astype ("float32" ) * 2
275
278
label = np .array ([0 , 1 , 4 , 5 ])
276
- ptable = np .array (
279
+ path_table = np .array (
277
280
[(0 , 2 , - 1 , - 1 , - 1 ), (0 , 1 , 3 , - 1 , - 1 ), (0 , 1 , 4 , - 1 , - 1 ),
278
281
(0 , 2 , - 1 , - 1 ,
279
282
- 1 )]) #np.array to store 1,2,5,6s' non-leaf path(root -> leaf)
280
- pcode = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
283
+ path_code = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
281
284
1 , 0 , 0 , - 1 , - 1 ), (0 , 1 , - 1 , - 1 , - 1 )]) #np.array to store
282
285
bias = np .random .random ((num_classes - 1 , 1 )).astype ("float32" )
283
286
self .attrs = {'num_classes' : num_classes , 'is_sparse' : False }
284
287
self .inputs = {
285
288
'X' : x ,
286
289
'W' : w ,
287
- 'PTable' : ptable ,
288
- 'PathCode' : pcode ,
290
+ 'PTable' : path_table ,
291
+ 'PathCode' : path_code ,
289
292
'Label' : label ,
290
293
'Bias' : bias
291
294
}
292
- pre_output , out = hsigmoidWithCustomTree (x , w , ptable , pcode , label ,
293
- bias , num_classes )
295
+ pre_output , out = hsigmoidWithCustomTree (x , w , path_table , path_code ,
296
+ label , bias , num_classes )
294
297
self .outputs = {'PreOut' : pre_output , 'Out' : out }
295
298
296
299
def test_check_output (self ):
@@ -310,26 +313,26 @@ def setUp(self):
310
313
w = np .random .random (
311
314
(num_classes - 1 , feature_size )).astype ("float32" ) * 2
312
315
label = np .array ([0 , 1 , 4 , 5 ])
313
- ptable = np .array (
316
+ path_table = np .array (
314
317
[(0 , 2 , - 1 , - 1 , - 1 ), (0 , 1 , 3 , - 1 , - 1 ), (0 , 1 , 4 , - 1 , - 1 ),
315
318
(0 , 2 , - 1 , - 1 ,
316
319
- 1 )]) #np.array to store 1,2,5,6s' non-leaf path(root -> leaf)
317
- pcode = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
320
+ path_code = np .array ([(0 , 0 , - 1 , - 1 , - 1 ), (1 , 1 , 1 , - 1 , - 1 ), (
318
321
1 , 0 , 0 , - 1 , - 1 ), (0 , 1 , - 1 , - 1 , - 1 )]) #np.array to store
319
322
# bias = np.random.random((num_classes - 1, 1)).astype("float32")
320
323
self .attrs = {'num_classes' : num_classes , 'is_sparse' : False }
321
324
self .inputs = {
322
325
'X' : x ,
323
326
'W' : w ,
324
- 'PTable' : ptable ,
325
- 'PathCode' : pcode ,
327
+ 'PTable' : path_table ,
328
+ 'PathCode' : path_code ,
326
329
'Label' : label ,
327
330
}
328
331
pre_output , out = hsigmoidWithCustomTree (
329
332
x = x ,
330
333
w = w ,
331
- ptable = ptable ,
332
- pcode = pcode ,
334
+ path_table = path_table ,
335
+ path_code = path_code ,
333
336
label = label ,
334
337
bias = None ,
335
338
num_classes = num_classes )
0 commit comments