1
-
1
+ import os ,sys
2
+ curr_path = os .path .abspath (os .path .dirname (__file__ ))
3
+ sys .path .append (os .path .join (curr_path , "../mxnet/python" ))
2
4
import mxnet as mx
3
5
import numpy as np
4
6
@@ -43,6 +45,9 @@ def fc_module(data, prefix, num_hidden=256):
43
45
return relu_fc1
44
46
45
47
def sym_gen_char (bucket_key ):
48
+ num_layers = 1
49
+ num_class = 2000
50
+ num_hidden = 512
46
51
key = bucket_key .split (',' )
47
52
tc_length = int (key [0 ])
48
53
cc_length = int (key [1 ])
@@ -55,17 +60,55 @@ def sym_gen_char(bucket_key):
55
60
cc_slices = list (mx .symbol .SliceChannel (data = cc_data , axis = 1 , num_outputs = cc_length , squeeze_axis = True , name = 'cc_slice' ))
56
61
tc_concat , _ = tc_cell .unroll (tc_length , inputs = tc_slices , merge_outputs = True , layout = 'TNC' )
57
62
cc_concat , _ = cc_cell .unroll (cc_length , inputs = cc_slices , merge_outputs = True , layout = 'TNC' )
58
- tc_concat = mx .sym .swapaxes (tc_concat , 0 , 1 )
59
- cc_concat = mx .sym .swapaxes (cc_concat , 0 , 1 )
63
+ tc_concat = mx .sym .transpose (tc_concat , (1 , 2 , 0 ))
64
+ cc_concat = mx .sym .transpose (cc_concat , (1 , 2 , 0 ))
65
+ tc_concat = mx .sym .Pooling (tc_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
66
+ cc_concat = mx .sym .Pooling (cc_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
67
+ feature = mx .sym .Concat (* [tc_concat , cc_concat ], name = 'concat' )
68
+ feature = mx .sym .Dropout (feature , p = 0.5 )
69
+ feature = fc_module (feature , 'feature' , num_hidden = 2000 )
70
+ loss = mx .sym .LogisticRegressionOutput (feature , label = label , name = 'regression' )
71
+ return loss
60
72
61
73
62
74
def sym_gen_word (bucket_key ):
75
+ num_layers = 1
76
+ num_class = 2000
77
+ num_hidden = 512
78
+ key = bucket_key .split (',' )
79
+ tw_length = int (key [0 ])
80
+ cw_length = int (key [1 ])
81
+ tw_data = mx .sym .Variable ('tw_array' )
82
+ cw_data = mx .sym .Variable ('cw_array' )
83
+ label = mx .sym .Variable ('label' )
84
+ tw_cell = mx .rnn .FusedRNNCell (num_hidden , num_layers = num_layers , bidirectional = True , mode = 'lstm' , prefix = 'tw_' )
85
+ cw_cell = mx .rnn .FusedRNNCell (num_hidden , num_layers = num_layers , bidirectional = True , mode = 'lstm' , prefix = 'cw_' )
86
+ tw_slices = list (mx .symbol .SliceChannel (data = tw_data , axis = 1 , num_outputs = tw_length , squeeze_axis = True , name = 'tw_slice' ))
87
+ cw_slices = list (mx .symbol .SliceChannel (data = cw_data , axis = 1 , num_outputs = cw_length , squeeze_axis = True , name = 'cw_slice' ))
88
+ tw_concat , _ = tw_cell .unroll (tw_length , inputs = tw_slices , merge_outputs = True , layout = 'TNC' )
89
+ cw_concat , _ = cw_cell .unroll (cw_length , inputs = cw_slices , merge_outputs = True , layout = 'TNC' )
90
+ tw_concat = mx .sym .transpose (tw_concat , (1 , 2 , 0 ))
91
+ cw_concat = mx .sym .transpose (cw_concat , (1 , 2 , 0 ))
92
+ tw_concat = mx .sym .Pooling (tw_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
93
+ cw_concat = mx .sym .Pooling (cw_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
94
+ feature = mx .sym .Concat (* [tw_concat , cw_concat ], name = 'concat' )
95
+ feature = mx .sym .Dropout (feature , p = 0.5 )
96
+ feature = fc_module (feature , 'feature' , num_hidden = 2000 )
97
+ loss = mx .sym .LogisticRegressionOutput (feature , label = label , name = 'regression' )
98
+ data_name = ['tw_array' , 'cw_array' ]
99
+ label_name = ['label' ]
100
+ return loss , data_name , label_name
63
101
64
102
65
103
def sym_gen_both (bucket_key ):
66
-
67
-
104
+ num_layers = 1
105
+ num_class = 2000
106
+ num_hidden = 512
68
107
key = bucket_key .split (',' )
108
+ tc_length = int (key [0 ])
109
+ cc_length = int (key [1 ])
110
+ tw_length = int (key [2 ])
111
+ cw_length = int (key [3 ])
69
112
tc_data = mx .sym .Variable ('tc_array' )
70
113
cc_data = mx .sym .Variable ('cc_array' )
71
114
tw_data = mx .sym .Variable ('tw_array' )
@@ -83,34 +126,29 @@ def sym_gen_both(bucket_key):
83
126
cc_concat , _ = cc_cell .unroll (cc_length , inputs = cc_slices , merge_outputs = True , layout = 'TNC' )
84
127
tw_concat , _ = tw_cell .unroll (tw_length , inputs = tw_slices , merge_outputs = True , layout = 'TNC' )
85
128
cw_concat , _ = cw_cell .unroll (cw_length , inputs = cw_slices , merge_outputs = True , layout = 'TNC' )
86
- tc_concat = mx .sym .swapaxes (tc_concat , 0 , 1 )
87
- cc_concat = mx .sym .swapaxes (cc_concat , 0 , 1 )
88
- tw_concat = mx .sym .swapaxes (tw_concat , 0 , 1 )
89
- cw_concat = mx .sym .swapaxes (cw_concat , 0 , 1 )
90
- #ch_outputs = mx.sym.Concat(*[tc_concat, cc_concat])
91
- #wd_outputs = mx.sym.Concat(*[tw_concat, cw_concat])
92
- #title_outputs= mx.sym.Concat(*[tc_concat, tw_concat])
93
- #content_outputs= mx.sym.Concat(*[cc_concat, cw_concat])
94
- #ch_outputs = fc_module(ch_outputs, 'ch_', num_hidden = 2000)
95
- #wd_outputs = fc_module(wd_outputs, 'wd_', num_hidden = 2000)
96
- #title_outputs = fc_module(title_outputs, 'title_', num_hidden = 2000)
97
- #content_outputs = fc_module(content_outputs, 'content_', num_hidden = 2000)
98
- #feature = mx.sym.Concat(*[ch_outputs, wd_outputs, title_outputs, content_outputs])
99
- feature = mx .sym .Concat (* [tc_concat , cc_concat , tw_concat , cw_concat ])
100
- feature = fc_module (feature , 'feature' , num_hidden = 4000 )
101
- feature = mx .sym .FullyConnected (data = feature , num_hidden = num_class , name = 'fc1' )
102
- loss = mx .sym .LogisticRegressionOutput (feature , label , name = 'regression' )
129
+ tc_concat = mx .sym .transpose (tc_concat , (1 , 2 , 0 ))
130
+ cc_concat = mx .sym .transpose (cc_concat , (1 , 2 , 0 ))
131
+ tw_concat = mx .sym .transpose (tw_concat , (1 , 2 , 0 ))
132
+ cw_concat = mx .sym .transpose (cw_concat , (1 , 2 , 0 ))
133
+ tc_concat = mx .sym .Pooling (tc_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
134
+ cc_concat = mx .sym .Pooling (cc_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
135
+ tw_concat = mx .sym .Pooling (tw_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
136
+ cw_concat = mx .sym .Pooling (cw_concat , kernel = (1 ,), global_pool = True , pool_type = 'max' )
137
+ feature = mx .sym .Concat (* [tc_concat , cc_concat , tw_concat , cw_concat ], name = 'concat' )
138
+ feature = mx .sym .Dropout (feature , p = 0.5 )
139
+ feature = fc_module (feature , 'feature' , num_hidden = 2000 )
140
+ loss = mx .sym .LogisticRegressionOutput (feature , label = label , name = 'regression' )
103
141
return loss
104
142
105
143
if __name__ == '__main__' :
106
- sym = sym_gen ( 100 ,100 , 100 , 100 )
144
+ sym = sym_gen_both ( ' 100,33,11,21' )
107
145
batch_size = 32
108
146
dim = 256
109
147
length = 100
110
- shapes = sym .infer_shape_partial (tc_array = (batch_size ,length ,dim ),
111
- cc_array = (batch_size ,length ,dim ),
112
- tw_array = (batch_size ,length ,dim ),
113
- cw_array = (batch_size ,length ,dim ),
148
+ shapes = sym .infer_shape_partial (tc_array = (batch_size ,100 ,dim ),
149
+ cc_array = (batch_size ,33 ,dim ),
150
+ tw_array = (batch_size ,11 ,dim ),
151
+ cw_array = (batch_size ,21 ,dim ),
114
152
label = (batch_size ,2000 ))
115
153
names = sym .list_arguments ()
116
154
for name , shape in zip (names , shapes [0 ]):
0 commit comments