@@ -67,33 +67,22 @@ class for each layer creation function in paddle.trainer_config_helpers.layers.
67
67
"""
68
68
69
69
import collections
70
+ import inspect
70
71
71
72
import paddle .trainer_config_helpers as conf_helps
72
73
from paddle .trainer_config_helpers .config_parser_utils import \
73
74
parse_network_config as __parse__
74
75
75
76
from paddle .trainer_config_helpers .default_decorators import wrap_name_default
76
77
from paddle .trainer_config_helpers .default_decorators import wrap_act_default
77
- from paddle .trainer_config_helpers .default_decorators import wrap_bias_attr_default
78
+ from paddle .trainer_config_helpers .default_decorators import \
79
+ wrap_bias_attr_default
78
80
from paddle .trainer_config_helpers .layers import layer_support
79
81
80
82
import data_type
81
83
import activation
82
- import attr
83
-
84
- __all__ = [
85
- 'parse_network' , 'data' , 'fc' , 'conv_shift' , 'img_conv' , 'img_pool' , 'spp' ,
86
- 'maxout' , 'img_cmrnorm' , 'batch_norm' , 'sum_to_one_norm' , 'recurrent' ,
87
- 'lstmemory' , 'grumemory' , 'pool' , 'last_seq' , 'first_seq' , 'concat' ,
88
- 'seq_concat' , 'block_expand' , 'expand' , 'repeat' , 'seq_reshape' , 'addto' ,
89
- 'linear_comb' , 'interpolation' , 'bilinear_interp' , 'power' , 'scaling' ,
90
- 'slope_intercept' , 'tensor' , 'cos_sim' , 'trans' , 'max_id' , 'sampling_id' ,
91
- 'pad' , 'classification_cost' , 'cross_entropy_cost' ,
92
- 'cross_entropy_with_selfnorm_cost' , 'regression_cost' ,
93
- 'multi_binary_label_cross_entropy_cost' , 'rank_cost' , 'lambda_cost' ,
94
- 'sum_cost' , 'huber_cost' , 'crf' , 'crf_decoding' , 'ctc' , 'warp_ctc' , 'nce' ,
95
- 'hsigmoid' , 'eos'
96
- ]
84
+
85
+ __all__ = ['parse_network' , 'data' ]
97
86
98
87
__projection_names__ = filter (lambda x : x .endswith ('_projection' ),
99
88
dir (conf_helps ))
@@ -288,83 +277,51 @@ def mixed(size=0,
288
277
AggregateLevel = conf_helps .layers .AggregateLevel
289
278
ExpandLevel = conf_helps .layers .ExpandLevel
290
279
291
- layer_list = [
292
- # [V2LayerImpl, V1_method_name, parent_names]
293
- # fully connected layers
294
- ['fc' , 'fc_layer' , ['input' ]],
295
- # conv layers
296
- ['conv_shift' , 'conv_shift_layer' , ['a' , 'b' ]],
297
- ['img_conv' , 'img_conv_layer' , ['input' ]],
298
- # image pooling layers
299
- ['img_pool' , 'img_pool_layer' , ['input' ]],
300
- ['spp' , 'spp_layer' , ['input' ]],
301
- ['maxout' , 'maxout_layer' , ['input' ]],
302
- # norm layers
303
- ['img_cmrnorm' , 'img_cmrnorm_layer' , ['input' ]],
304
- ['batch_norm' , 'batch_norm_layer' , ['input' ]],
305
- ['sum_to_one_norm' , 'sum_to_one_norm_layer' , ['input' ]],
306
- # recurrent layers
307
- ['recurrent' , 'recurrent_layer' , ['input' ]],
308
- ['lstmemory' , 'lstmemory' , ['input' ]],
309
- ['grumemory' , 'grumemory' , ['input' ]],
310
- # aggregate layers
311
- ['pool' , 'pooling_layer' , ['input' ]],
312
- ['last_seq' , 'last_seq' , ['input' ]],
313
- ['first_seq' , 'first_seq' , ['input' ]],
314
- ['concat' , 'concat_layer' , ['input' ]],
315
- ['seq_concat' , 'seq_concat_layer' , ['a' , 'b' ]],
316
- # reshaping layers
317
- ['block_expand' , 'block_expand_layer' , ['input' ]],
318
- ['expand' , 'expand_layer' , ['input' , 'expand_as' ]],
319
- ['repeat' , 'repeat_layer' , ['input' ]],
320
- ['rotate' , 'rotate_layer' , ['input' ]],
321
- ['seq_reshape' , 'seq_reshape_layer' , ['input' ]],
322
- # math layers
323
- ['addto' , 'addto_layer' , ['input' ]],
324
- ['linear_comb' , 'linear_comb_layer' , ['weights' , 'vectors' ]],
325
- ['interpolation' , 'interpolation_layer' , ['input' , 'weight' ]],
326
- ['bilinear_interp' , 'bilinear_interp_layer' , ['input' ]],
327
- ['power' , 'power_layer' , ['input' , 'weight' ]],
328
- ['scaling' , 'scaling_layer' , ['input' , 'weight' ]],
329
- ['slope_intercept' , 'slope_intercept_layer' , ['input' ]],
330
- ['tensor' , 'tensor_layer' , ['a' , 'b' ]],
331
- ['cos_sim' , 'cos_sim' , ['a' , 'b' ]],
332
- ['trans' , 'trans_layer' , ['input' ]],
333
- # sampling layers
334
- ['max_id' , 'maxid_layer' , ['input' ]],
335
- ['sampling_id' , 'sampling_id_layer' , ['input' ]],
336
- # slicing and joining layers
337
- ['pad' , 'pad_layer' , ['input' ]],
338
- # cost layers
339
- [
340
- 'classification_cost' , 'classification_cost' ,
341
- ['input' , 'label' , 'weight' ]
342
- ],
343
- ['regression_cost' , 'regression_cost' , ['input' , 'label' , 'weight' ]],
344
- ['cross_entropy_cost' , 'cross_entropy' , ['input' , 'label' ]],
345
- [
346
- 'cross_entropy_with_selfnorm_cost' , 'cross_entropy_with_selfnorm' ,
347
- ['input' , 'label' ]
348
- ],
349
- [
350
- 'multi_binary_label_cross_entropy_cost' ,
351
- 'multi_binary_label_cross_entropy' , ['input' , 'label' ]
352
- ],
353
- ['rank_cost' , 'rank_cost' , ['left' , 'right' , 'label' , 'weight' ]],
354
- ['lambda_cost' , 'lambda_cost' , ['input' , 'score' ]],
355
- ['sum_cost' , 'sum_cost' , ['input' ]],
356
- ['huber_cost' , 'huber_cost' , ['input' , 'label' ]],
357
- ['crf' , 'crf_layer' , ['input' , 'label' ]],
358
- ['crf_decoding' , 'crf_decoding_layer' , ['input' ]],
359
- ['ctc' , 'ctc_layer' , ['input' , 'label' ]],
360
- ['warp_ctc' , 'warp_ctc_layer' , ['input' , 'label' ]],
361
- ['nce' , 'nce_layer' , ['input' , 'label' ]],
362
- ['hsigmoid' , 'hsigmoid' , ['input' , 'label' ]],
363
- # check layers
364
- ['eos' , 'eos_layer' , ['input' ]]
365
- ]
366
- for l in layer_list :
367
- globals ()[l [0 ]] = __convert_to_v2__ (l [1 ], l [2 ])
280
+
281
+ def __layer_name_mapping__ (inname ):
282
+ if inname in ['data_layer' , 'memory' , 'mixed_layer' ]:
283
+ # Do Not handle these layers
284
+ return
285
+ elif inname == 'maxid_layer' :
286
+ return 'max_id'
287
+ elif inname .endswith ('memory' ) or inname .endswith (
288
+ '_seq' ) or inname .endswith ('_sim' ) or inname == 'hsigmoid' :
289
+ return inname
290
+ elif inname in [
291
+ 'cross_entropy' , 'multi_binary_label_cross_entropy' ,
292
+ 'cross_entropy_with_selfnorm'
293
+ ]:
294
+ return inname + "_cost"
295
+ elif inname .endswith ('_cost' ):
296
+ return inname
297
+ elif inname .endswith ("_layer" ):
298
+ return inname [:- len ("_layer" )]
299
+
300
+
301
+ def __layer_name_mapping_parent_names__ (inname ):
302
+ all_args = getattr (conf_helps , inname ).argspec .args
303
+ return filter (
304
+ lambda x : x in ['input1' , 'input2' ,'label' , 'input' , 'a' , 'b' , 'expand_as' ,
305
+ 'weights' , 'vectors' , 'weight' , 'score' , 'left' , 'right' ],
306
+ all_args )
307
+
308
+
309
+ def __convert_layer__ (_new_name_ , _old_name_ , _parent_names_ ):
310
+ global __all__
311
+ __all__ .append (_new_name_ )
312
+ globals ()[new_name ] = __convert_to_v2__ (_old_name_ , _parent_names_ )
313
+
314
+
315
+ for each_layer_name in dir (conf_helps ):
316
+ new_name = __layer_name_mapping__ (each_layer_name )
317
+ if new_name is not None :
318
+ parent_names = __layer_name_mapping_parent_names__ (each_layer_name )
319
+ assert len (parent_names ) != 0 , each_layer_name
320
+ __convert_layer__ (new_name , each_layer_name , parent_names )
321
+
322
+ del parent_names
323
+ del new_name
324
+ del each_layer_name
368
325
369
326
# convert projection
370
327
for prj in __projection_names__ :
0 commit comments