Skip to content

Commit f95e05a

Browse files
committed
Refine the inference unittests.
1 parent 899ba0d commit f95e05a

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

paddle/fluid/inference/tests/book/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ inference_test(label_semantic_roles)
3030
inference_test(recognize_digits ARGS mlp)
3131
inference_test(recommender_system)
3232
#inference_test(rnn_encoder_decoder)
33-
inference_test(understand_sentiment)
33+
inference_test(understand_sentiment ARGS conv lstm)
3434
inference_test(word2vec)

paddle/fluid/inference/tests/book/test_inference_label_semantic_roles.cc

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,42 @@ TEST(inference, label_semantic_roles) {
3232
paddle::framework::LoDTensor word, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1,
3333
ctx_p2, mark;
3434
paddle::framework::LoD lod{{0, 4, 10}};
35-
36-
SetupLoDTensor(word, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
37-
SetupLoDTensor(
38-
predicate, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
39-
SetupLoDTensor(ctx_n2, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
40-
SetupLoDTensor(ctx_n1, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
41-
SetupLoDTensor(ctx_0, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
42-
SetupLoDTensor(ctx_p1, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
43-
SetupLoDTensor(ctx_p2, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
44-
SetupLoDTensor(mark, lod, static_cast<int64_t>(0), static_cast<int64_t>(1));
35+
int64_t word_dict_len = 44068;
36+
int64_t predicate_dict_len = 3162;
37+
int64_t mark_dict_len = 2;
38+
39+
SetupLoDTensor(word,
40+
lod,
41+
static_cast<int64_t>(0),
42+
static_cast<int64_t>(word_dict_len - 1));
43+
SetupLoDTensor(predicate,
44+
lod,
45+
static_cast<int64_t>(0),
46+
static_cast<int64_t>(predicate_dict_len - 1));
47+
SetupLoDTensor(ctx_n2,
48+
lod,
49+
static_cast<int64_t>(0),
50+
static_cast<int64_t>(word_dict_len - 1));
51+
SetupLoDTensor(ctx_n1,
52+
lod,
53+
static_cast<int64_t>(0),
54+
static_cast<int64_t>(word_dict_len - 1));
55+
SetupLoDTensor(ctx_0,
56+
lod,
57+
static_cast<int64_t>(0),
58+
static_cast<int64_t>(word_dict_len - 1));
59+
SetupLoDTensor(ctx_p1,
60+
lod,
61+
static_cast<int64_t>(0),
62+
static_cast<int64_t>(word_dict_len - 1));
63+
SetupLoDTensor(ctx_p2,
64+
lod,
65+
static_cast<int64_t>(0),
66+
static_cast<int64_t>(word_dict_len - 1));
67+
SetupLoDTensor(mark,
68+
lod,
69+
static_cast<int64_t>(0),
70+
static_cast<int64_t>(mark_dict_len - 1));
4571

4672
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
4773
cpu_feeds.push_back(&word);

paddle/fluid/inference/tests/book/test_inference_understand_sentiment.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ TEST(inference, understand_sentiment) {
3131

3232
paddle::framework::LoDTensor words;
3333
paddle::framework::LoD lod{{0, 4, 10}};
34-
SetupLoDTensor(words, lod, static_cast<int64_t>(0), static_cast<int64_t>(10));
34+
int64_t word_dict_len = 5147;
35+
36+
SetupLoDTensor(words,
37+
lod,
38+
static_cast<int64_t>(0),
39+
static_cast<int64_t>(word_dict_len - 1));
3540

3641
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
3742
cpu_feeds.push_back(&words);

python/paddle/v2/fluid/tests/book/test_label_semantic_roles.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def infer(use_cuda, save_dirname=None):
296296
print(results[0].lod())
297297
np_data = np.array(results[0])
298298
print("Inference Shape: ", np_data.shape)
299-
print("Inference results: ", np_data)
300299

301300

302301
def main(use_cuda):

python/paddle/v2/fluid/tests/book/test_understand_sentiment.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def create_random_lodtensor(lod, place, low, high):
9393
return res
9494

9595

96-
def train(word_dict, net_method, use_cuda, save_dirname=None):
96+
def train(word_dict, nn_type, use_cuda, save_dirname=None):
9797
BATCH_SIZE = 128
9898
PASS_NUM = 5
9999
dict_dim = len(word_dict)
@@ -102,6 +102,11 @@ def train(word_dict, net_method, use_cuda, save_dirname=None):
102102
data = fluid.layers.data(
103103
name="words", shape=[1], dtype="int64", lod_level=1)
104104
label = fluid.layers.data(name="label", shape=[1], dtype="int64")
105+
106+
if nn_type == "conv":
107+
net_method = convolution_net
108+
else:
109+
net_method = stacked_lstm_net
105110
cost, acc_out, prediction = net_method(
106111
data, label, input_dim=dict_dim, class_dim=class_dim)
107112

@@ -132,7 +137,7 @@ def train(word_dict, net_method, use_cuda, save_dirname=None):
132137
net_method.__name__))
133138

134139

135-
def infer(use_cuda, save_dirname=None):
140+
def infer(word_dict, use_cuda, save_dirname=None):
136141
if save_dirname is None:
137142
return
138143

@@ -146,10 +151,11 @@ def infer(use_cuda, save_dirname=None):
146151
[inference_program, feed_target_names,
147152
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
148153

154+
word_dict_len = len(word_dict)
155+
149156
lod = [0, 4, 10]
150-
word_dict = paddle.dataset.imdb.word_dict()
151157
tensor_words = create_random_lodtensor(
152-
lod, place, low=0, high=len(word_dict) - 1)
158+
lod, place, low=0, high=word_dict_len - 1)
153159

154160
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
155161
# and results will contain a list of data corresponding to fetch_targets.
@@ -164,15 +170,15 @@ def infer(use_cuda, save_dirname=None):
164170
print("Inference results: ", np_data)
165171

166172

167-
def main(word_dict, net_method, use_cuda):
173+
def main(word_dict, nn_type, use_cuda):
168174
if use_cuda and not fluid.core.is_compiled_with_cuda():
169175
return
170176

171177
# Directory for saving the trained model
172-
save_dirname = "understand_sentiment.inference.model"
178+
save_dirname = "understand_sentiment_" + nn_type + ".inference.model"
173179

174-
train(word_dict, net_method, use_cuda, save_dirname)
175-
infer(use_cuda, save_dirname)
180+
train(word_dict, nn_type, use_cuda, save_dirname)
181+
infer(word_dict, use_cuda, save_dirname)
176182

177183

178184
class TestUnderstandSentiment(unittest.TestCase):
@@ -191,19 +197,19 @@ def new_program_scope(self):
191197

192198
def test_conv_cpu(self):
193199
with self.new_program_scope():
194-
main(self.word_dict, net_method=convolution_net, use_cuda=False)
200+
main(self.word_dict, nn_type="conv", use_cuda=False)
195201

196202
def test_stacked_lstm_cpu(self):
197203
with self.new_program_scope():
198-
main(self.word_dict, net_method=stacked_lstm_net, use_cuda=False)
204+
main(self.word_dict, nn_type="lstm", use_cuda=False)
199205

200206
def test_conv_gpu(self):
201207
with self.new_program_scope():
202-
main(self.word_dict, net_method=convolution_net, use_cuda=True)
208+
main(self.word_dict, nn_type="conv", use_cuda=True)
203209

204210
def test_stacked_lstm_gpu(self):
205211
with self.new_program_scope():
206-
main(self.word_dict, net_method=stacked_lstm_net, use_cuda=True)
212+
main(self.word_dict, nn_type="lstm", use_cuda=True)
207213

208214

209215
if __name__ == '__main__':

0 commit comments

Comments
 (0)