1
1
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # # Licensed under the Apache License, Version 2.0 (the "License");
4
3
# you may not use this file except in compliance with the License.
5
4
# You may obtain a copy of the License at
6
5
#
16
15
import paddle .v2 .fluid as fluid
17
16
import unittest
18
17
import os
18
+ import numpy as np
19
19
import math
20
20
import sys
21
21
22
22
23
- def main (use_cuda , is_sparse , parallel ):
24
- if use_cuda and not fluid .core .is_compiled_with_cuda ():
23
+ def create_random_lodtensor (lod , place , low , high ):
24
+ data = np .random .random_integers (low , high , [lod [- 1 ], 1 ]).astype ("int64" )
25
+ res = fluid .LoDTensor ()
26
+ res .set (data , place )
27
+ res .set_lod ([lod ])
28
+ return res
29
+
30
+
31
+ def infer (use_cuda , save_dirname = None ):
32
+ if save_dirname is None :
25
33
return
26
34
35
+ place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
36
+ exe = fluid .Executor (place )
37
+
38
+ # Use fluid.io.load_inference_model to obtain the inference program desc,
39
+ # the feed_target_names (the names of variables that will be feeded
40
+ # data using feed operators), and the fetch_targets (variables that
41
+ # we want to obtain data from using fetch operators).
42
+ [inference_program , feed_target_names ,
43
+ fetch_targets ] = fluid .io .load_inference_model (save_dirname , exe )
44
+
45
+ word_dict = paddle .dataset .imikolov .build_dict ()
46
+ dict_size = len (word_dict ) - 1
47
+
48
+ # Setup input, by creating 4 words, and setting up lod required for
49
+ # lookup_table_op
50
+ lod = [0 , 1 ]
51
+ first_word = create_random_lodtensor (lod , place , low = 0 , high = dict_size )
52
+ second_word = create_random_lodtensor (lod , place , low = 0 , high = dict_size )
53
+ third_word = create_random_lodtensor (lod , place , low = 0 , high = dict_size )
54
+ fourth_word = create_random_lodtensor (lod , place , low = 0 , high = dict_size )
55
+
56
+ assert feed_target_names [0 ] == 'firstw'
57
+ assert feed_target_names [1 ] == 'secondw'
58
+ assert feed_target_names [2 ] == 'thirdw'
59
+ assert feed_target_names [3 ] == 'forthw'
60
+
61
+ # Construct feed as a dictionary of {feed_target_name: feed_target_data}
62
+ # and results will contain a list of data corresponding to fetch_targets.
63
+ results = exe .run (inference_program ,
64
+ feed = {
65
+ feed_target_names [0 ]: first_word ,
66
+ feed_target_names [1 ]: second_word ,
67
+ feed_target_names [2 ]: third_word ,
68
+ feed_target_names [3 ]: fourth_word
69
+ },
70
+ fetch_list = fetch_targets ,
71
+ return_numpy = False )
72
+ print (results [0 ].lod ())
73
+ np_data = np .array (results [0 ])
74
+ print ("Inference Shape: " , np_data .shape )
75
+ print ("Inference results: " , np_data )
76
+
77
+
78
+ def train (use_cuda , is_sparse , parallel , save_dirname ):
27
79
PASS_NUM = 100
28
80
EMBED_SIZE = 32
29
81
HIDDEN_SIZE = 256
@@ -67,7 +119,7 @@ def __network__(words):
67
119
act = 'softmax' )
68
120
cost = fluid .layers .cross_entropy (input = predict_word , label = words [4 ])
69
121
avg_cost = fluid .layers .mean (x = cost )
70
- return avg_cost
122
+ return avg_cost , predict_word
71
123
72
124
word_dict = paddle .dataset .imikolov .build_dict ()
73
125
dict_size = len (word_dict )
@@ -79,13 +131,13 @@ def __network__(words):
79
131
next_word = fluid .layers .data (name = 'nextw' , shape = [1 ], dtype = 'int64' )
80
132
81
133
if not parallel :
82
- avg_cost = __network__ (
134
+ avg_cost , predict_word = __network__ (
83
135
[first_word , second_word , third_word , forth_word , next_word ])
84
136
else :
85
137
places = fluid .layers .get_places ()
86
138
pd = fluid .layers .ParallelDo (places )
87
139
with pd .do ():
88
- avg_cost = __network__ (
140
+ avg_cost , predict_word = __network__ (
89
141
map (pd .read_input , [
90
142
first_word , second_word , third_word , forth_word , next_word
91
143
]))
@@ -113,13 +165,25 @@ def __network__(words):
113
165
feed = feeder .feed (data ),
114
166
fetch_list = [avg_cost ])
115
167
if avg_cost_np [0 ] < 5.0 :
168
+ if save_dirname is not None :
169
+ fluid .io .save_inference_model (save_dirname , [
170
+ 'firstw' , 'secondw' , 'thirdw' , 'forthw'
171
+ ], [predict_word ], exe )
116
172
return
117
173
if math .isnan (float (avg_cost_np [0 ])):
118
174
sys .exit ("got NaN loss, training failed." )
119
175
120
176
raise AssertionError ("Cost is too large {0:2.2}" .format (avg_cost_np [0 ]))
121
177
122
178
179
+ def main (use_cuda , is_sparse , parallel ):
180
+ if use_cuda and not fluid .core .is_compiled_with_cuda ():
181
+ return
182
+ save_dirname = "word2vec.inference.model"
183
+ train (use_cuda , is_sparse , parallel , save_dirname )
184
+ infer (use_cuda , save_dirname )
185
+
186
+
123
187
FULL_TEST = os .getenv ('FULL_TEST' ,
124
188
'0' ).lower () in ['true' , '1' , 't' , 'y' , 'yes' , 'on' ]
125
189
SKIP_REASON = "Only run minimum number of tests in CI server, to make CI faster"
@@ -142,7 +206,8 @@ def __impl__(*args, **kwargs):
142
206
with fluid .program_guard (prog , startup_prog ):
143
207
main (use_cuda = use_cuda , is_sparse = is_sparse , parallel = parallel )
144
208
145
- if use_cuda and is_sparse and parallel :
209
+ # run only 2 cases: use_cuda is either True or False
210
+ if is_sparse == False and parallel == False :
146
211
fn = __impl__
147
212
else :
148
213
# skip the other test when on CI server
0 commit comments