27
27
# hidden dim
28
28
hid_dim = 128
29
29
30
- # hidden dim2
31
- hid_dim2 = 96
32
-
33
30
# class num
34
31
class_dim = 2
35
32
@@ -42,36 +39,34 @@ def build_program(is_train):
42
39
filenames = TRAIN_FILES if is_train else TEST_FILES ,
43
40
shapes = [[- 1 , 1 ], [- 1 , 1 ]],
44
41
lod_levels = [1 , 0 ],
45
- dtypes = ['int64' , 'int64' ],
46
- thread_num = 1 )
47
- if is_train :
48
- file_obj = fluid .layers .io .shuffle (file_obj_handle , buffer_size = 1000 )
49
- else :
50
- file_obj = file_obj_handle
51
- file_obj = fluid .layers .io .double_buffer (file_obj )
42
+ dtypes = ['int64' , 'int64' ])
43
+
44
+ file_obj = fluid .layers .io .double_buffer (file_obj_handle )
52
45
53
46
with fluid .unique_name .guard ():
54
47
55
48
data , label = fluid .layers .read_file (file_obj )
56
49
57
50
emb = fluid .layers .embedding (input = data , size = [DICT_DIM , emb_dim ])
58
51
59
- # sequence conv with window size = 3
60
- win_size = 3
61
52
conv_3 = fluid .nets .sequence_conv_pool (
62
53
input = emb ,
63
54
num_filters = hid_dim ,
64
- filter_size = win_size ,
55
+ filter_size = 3 ,
65
56
act = "tanh" ,
66
- pool_type = "max " )
57
+ pool_type = "sqrt " )
67
58
68
- # fc layer after conv
69
- fc_1 = fluid .layers .fc (input = [conv_3 ], size = hid_dim2 )
59
+ conv_4 = fluid .nets .sequence_conv_pool (
60
+ input = emb ,
61
+ num_filters = hid_dim ,
62
+ filter_size = 4 ,
63
+ act = "tanh" ,
64
+ pool_type = "sqrt" )
70
65
71
- # probability of each class
72
- prediction = fluid .layers .fc (input = [fc_1 ],
66
+ prediction = fluid .layers .fc (input = [conv_3 , conv_4 ],
73
67
size = class_dim ,
74
68
act = "softmax" )
69
+
75
70
# cross entropy loss
76
71
cost = fluid .layers .cross_entropy (input = prediction , label = label )
77
72
@@ -117,11 +112,9 @@ def main():
117
112
try :
118
113
batch_id = 0
119
114
while True :
120
- result = map (numpy .array ,
121
- train_exe .run (fetch_list = fetch_var_list
122
- if batch_id % 10 == 0 else []))
123
- if len (result ) != 0 :
124
- print 'Train loss: ' , result
115
+ loss , acc = map (numpy .array ,
116
+ train_exe .run (fetch_list = fetch_var_list ))
117
+ print 'Train epoch' , epoch_id , 'batch' , batch_id , 'loss:' , loss , 'acc:' , acc
125
118
batch_id += 1
126
119
except fluid .core .EOFException :
127
120
print 'End of epoch' , epoch_id
@@ -138,7 +131,7 @@ def main():
138
131
acc .append (acc_np [0 ])
139
132
except :
140
133
test_args ['file' ].reset ()
141
- print 'TEST: ' , numpy .mean (loss ), numpy .mean (acc )
134
+ print 'Test loss: ' , numpy .mean (loss ), 'acc:' , numpy .mean (acc )
142
135
143
136
144
137
if __name__ == '__main__' :
0 commit comments