17
17
import paddle
18
18
import paddle .fluid as fluid
19
19
from functools import partial
20
+ import numpy as np
20
21
21
22
CLASS_DIM = 2
22
23
EMB_DIM = 128
23
24
HID_DIM = 512
24
25
STACKED_NUM = 3
26
+ BATCH_SIZE = 128
25
27
26
28
27
29
def stacked_lstm_net (data , input_dim , class_dim , emb_dim , hid_dim , stacked_num ):
@@ -50,7 +52,7 @@ def stacked_lstm_net(data, input_dim, class_dim, emb_dim, hid_dim, stacked_num):
50
52
return prediction
51
53
52
54
53
- def inference_network (word_dict ):
55
+ def inference_program (word_dict ):
54
56
data = fluid .layers .data (
55
57
name = "words" , shape = [1 ], dtype = "int64" , lod_level = 1 )
56
58
@@ -60,57 +62,71 @@ def inference_network(word_dict):
60
62
return net
61
63
62
64
63
- def train_network (word_dict ):
64
- prediction = inference_network (word_dict )
65
+ def train_program (word_dict ):
66
+ prediction = inference_program (word_dict )
65
67
label = fluid .layers .data (name = "label" , shape = [1 ], dtype = "int64" )
66
68
cost = fluid .layers .cross_entropy (input = prediction , label = label )
67
69
avg_cost = fluid .layers .mean (cost )
68
70
accuracy = fluid .layers .accuracy (input = prediction , label = label )
69
- return avg_cost , accuracy
71
+ return [ avg_cost , accuracy ]
70
72
71
73
72
- def train (use_cuda , save_path ):
73
- BATCH_SIZE = 128
74
- EPOCH_NUM = 5
74
+ def train (use_cuda , train_program , save_dirname ):
75
+ place = fluid . CUDAPlace ( 0 ) if use_cuda else fluid . CPUPlace ()
76
+ optimizer = fluid . optimizer . Adagrad ( learning_rate = 0.002 )
75
77
76
78
word_dict = paddle .dataset .imdb .word_dict ()
79
+ trainer = fluid .Trainer (
80
+ train_func = partial (train_program , word_dict ),
81
+ place = place ,
82
+ optimizer = optimizer )
77
83
78
- train_data = paddle .batch (
84
+ def event_handler (event ):
85
+ if isinstance (event , fluid .EndEpochEvent ):
86
+ test_reader = paddle .batch (
87
+ paddle .dataset .imdb .test (word_dict ), batch_size = BATCH_SIZE )
88
+ avg_cost , acc = trainer .test (
89
+ reader = test_reader , feed_order = ['words' , 'label' ])
90
+
91
+ print ("avg_cost: %s" % avg_cost )
92
+ print ("acc : %s" % acc )
93
+
94
+ if acc > 0.2 : # Smaller value to increase CI speed
95
+ trainer .save_params (save_dirname )
96
+ trainer .stop ()
97
+
98
+ else :
99
+ print ('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}' .format (
100
+ event .epoch + 1 , avg_cost , acc ))
101
+ if math .isnan (avg_cost ):
102
+ sys .exit ("got NaN loss, training failed." )
103
+ elif isinstance (event , fluid .EndStepEvent ):
104
+ print ("Step {0}, Epoch {1} Metrics {2}" .format (
105
+ event .step , event .epoch , map (np .array , event .metrics )))
106
+ if event .step == 1 : # Run 2 iterations to speed CI
107
+ trainer .save_params (save_dirname )
108
+ trainer .stop ()
109
+
110
+ train_reader = paddle .batch (
79
111
paddle .reader .shuffle (
80
- paddle .dataset .imdb .train (word_dict ), buf_size = 1000 ),
112
+ paddle .dataset .imdb .train (word_dict ), buf_size = 25000 ),
81
113
batch_size = BATCH_SIZE )
82
114
83
- test_data = paddle .batch (
84
- paddle .dataset .imdb .test (word_dict ), batch_size = BATCH_SIZE )
85
-
86
- def event_handler (event ):
87
- if isinstance (event , fluid .EndIteration ):
88
- if (event .batch_id % 10 ) == 0 :
89
- avg_cost , accuracy = trainer .test (reader = test_data )
90
-
91
- print ('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}' .format (
92
- event .batch_id + 1 , avg_cost , accuracy ))
115
+ trainer .train (
116
+ num_epochs = 1 ,
117
+ event_handler = event_handler ,
118
+ reader = train_reader ,
119
+ feed_order = ['words' , 'label' ])
93
120
94
- if accuracy > 0.01 : # Low threshold for speeding up CI
95
- trainer .params .save (save_path )
96
- return
97
121
98
- place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
99
- trainer = fluid .Trainer (
100
- partial (train_network , word_dict ),
101
- optimizer = fluid .optimizer .Adagrad (learning_rate = 0.002 ),
102
- place = place ,
103
- event_handler = event_handler )
104
-
105
- trainer .train (train_data , EPOCH_NUM , event_handler = event_handler )
106
-
107
-
108
- def infer (use_cuda , save_path ):
109
- params = fluid .Params (save_path )
122
+ def infer (use_cuda , inference_program , save_dirname = None ):
110
123
place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
111
124
word_dict = paddle .dataset .imdb .word_dict ()
125
+
112
126
inferencer = fluid .Inferencer (
113
- partial (inference_network , word_dict ), params , place = place )
127
+ infer_func = partial (inference_program , word_dict ),
128
+ param_path = save_dirname ,
129
+ place = place )
114
130
115
131
def create_random_lodtensor (lod , place , low , high ):
116
132
data = np .random .random_integers (low , high ,
@@ -131,8 +147,8 @@ def main(use_cuda):
131
147
if use_cuda and not fluid .core .is_compiled_with_cuda ():
132
148
return
133
149
save_path = "understand_sentiment_stacked_lstm.inference.model"
134
- train (use_cuda , save_path )
135
- infer (use_cuda , save_path )
150
+ train (use_cuda , train_program , save_path )
151
+ infer (use_cuda , inference_program , save_path )
136
152
137
153
138
154
if __name__ == '__main__' :
0 commit comments