@@ -152,29 +152,6 @@ def seq_to_seq_net():
152
152
return avg_cost , prediction
153
153
154
154
155
- def to_lodtensor (data , place ):
156
- seq_lens = [len (seq ) for seq in data ]
157
- cur_len = 0
158
- lod = [cur_len ]
159
- for l in seq_lens :
160
- cur_len += l
161
- lod .append (cur_len )
162
- flattened_data = np .concatenate (data , axis = 0 ).astype ("int64" )
163
- flattened_data = flattened_data .reshape ([len (flattened_data ), 1 ])
164
- res = core .LoDTensor ()
165
- res .set (flattened_data , place )
166
- res .set_lod ([lod ])
167
- return res
168
-
169
-
170
- def create_random_lodtensor (lod , place , low , high ):
171
- data = np .random .random_integers (low , high , [lod [- 1 ], 1 ]).astype ("int64" )
172
- res = fluid .LoDTensor ()
173
- res .set (data , place )
174
- res .set_lod ([lod ])
175
- return res
176
-
177
-
178
155
def train (use_cuda , save_dirname = None ):
179
156
[avg_cost , prediction ] = seq_to_seq_net ()
180
157
@@ -188,22 +165,20 @@ def train(use_cuda, save_dirname=None):
188
165
189
166
place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
190
167
exe = Executor (place )
191
-
192
168
exe .run (framework .default_startup_program ())
193
169
170
+ feed_order = ['source_sequence' , 'target_sequence' , 'label_sequence' ]
171
+ feed_list = [
172
+ framework .default_main_program ().global_block ().var (var_name )
173
+ for var_name in feed_order
174
+ ]
175
+ feeder = fluid .DataFeeder (feed_list , place )
176
+
194
177
batch_id = 0
195
178
for pass_id in xrange (2 ):
196
179
for data in train_data ():
197
- word_data = to_lodtensor (map (lambda x : x [0 ], data ), place )
198
- trg_word = to_lodtensor (map (lambda x : x [1 ], data ), place )
199
- trg_word_next = to_lodtensor (map (lambda x : x [2 ], data ), place )
200
-
201
180
outs = exe .run (framework .default_main_program (),
202
- feed = {
203
- 'source_sequence' : word_data ,
204
- 'target_sequence' : trg_word ,
205
- 'label_sequence' : trg_word_next
206
- },
181
+ feed = feeder .feed (data ),
207
182
fetch_list = [avg_cost ])
208
183
209
184
avg_cost_val = np .array (outs [0 ])
@@ -237,9 +212,23 @@ def infer(use_cuda, save_dirname=None):
237
212
[inference_program , feed_target_names ,
238
213
fetch_targets ] = fluid .io .load_inference_model (save_dirname , exe )
239
214
240
- lod = [0 , 4 , 10 ]
241
- word_data = create_random_lodtensor (lod , place , low = 0 , high = 1 )
242
- trg_word = create_random_lodtensor (lod , place , low = 0 , high = 1 )
215
+ # Setup input by creating LoDTensor to represent sequence of words.
216
+ # Here each word is the basic element of the LoDTensor and the shape of
217
+ # each word (base_shape) should be [1] since it is simply an index to
218
+ # look up for the corresponding word vector.
219
+ # Suppose the length_based level of detail (lod) info is set to [[4, 6]],
220
+ # which has only one lod level. Then the created LoDTensor will have only
221
+ # one higher level structure (sequence of words, or sentence) than the basic
222
+ # element (word). Hence the LoDTensor will hold data for two sentences of
223
+ # length 4 and 6, respectively.
224
+ # Note that lod info should be a list of lists.
225
+ lod = [[4 , 6 ]]
226
+ base_shape = [1 ]
227
+ # The range of random integers is [low, high]
228
+ word_data = fluid .create_random_int_lodtensor (
229
+ lod , base_shape , place , low = 0 , high = 1 )
230
+ trg_word = fluid .create_random_int_lodtensor (
231
+ lod , base_shape , place , low = 0 , high = 1 )
243
232
244
233
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
245
234
# and results will contain a list of data corresponding to fetch_targets.
0 commit comments