@@ -173,63 +173,33 @@ def train(use_cuda, save_dirname, is_local=True):
173
173
test_reader = paddle .batch (
174
174
paddle .dataset .movielens .test (), batch_size = BATCH_SIZE )
175
175
176
- feeding = {
177
- 'user_id' : 0 ,
178
- 'gender_id' : 1 ,
179
- 'age_id' : 2 ,
180
- 'job_id' : 3 ,
181
- 'movie_id' : 4 ,
182
- 'category_id' : 5 ,
183
- 'movie_title' : 6 ,
184
- 'score' : 7
185
- }
186
-
187
- def func_feed (feeding , data ):
188
- feed_tensors = {}
189
- for (key , idx ) in feeding .iteritems ():
190
- tensor = fluid .LoDTensor ()
191
- if key != "category_id" and key != "movie_title" :
192
- if key == "score" :
193
- numpy_data = np .array (map (lambda x : x [idx ], data )).astype (
194
- "float32" )
195
- else :
196
- numpy_data = np .array (map (lambda x : x [idx ], data )).astype (
197
- "int64" )
198
- else :
199
- numpy_data = map (lambda x : np .array (x [idx ]).astype ("int64" ),
200
- data )
201
- lod_info = [len (item ) for item in numpy_data ]
202
- offset = 0
203
- lod = [offset ]
204
- for item in lod_info :
205
- offset += item
206
- lod .append (offset )
207
- numpy_data = np .concatenate (numpy_data , axis = 0 )
208
- tensor .set_lod ([lod ])
209
-
210
- numpy_data = numpy_data .reshape ([numpy_data .shape [0 ], 1 ])
211
- tensor .set (numpy_data , place )
212
- feed_tensors [key ] = tensor
213
- return feed_tensors
176
+ feed_order = [
177
+ 'user_id' , 'gender_id' , 'age_id' , 'job_id' , 'movie_id' , 'category_id' ,
178
+ 'movie_title' , 'score'
179
+ ]
214
180
215
181
def train_loop (main_program ):
216
182
exe .run (framework .default_startup_program ())
217
183
184
+ feed_list = [
185
+ main_program .global_block ().var (var_name ) for var_name in feed_order
186
+ ]
187
+ feeder = fluid .DataFeeder (feed_list , place )
188
+
218
189
PASS_NUM = 100
219
190
for pass_id in range (PASS_NUM ):
220
191
for batch_id , data in enumerate (train_reader ()):
221
192
# train a mini-batch
222
193
outs = exe .run (program = main_program ,
223
- feed = func_feed ( feeding , data ),
194
+ feed = feeder . feed ( data ),
224
195
fetch_list = [avg_cost ])
225
196
out = np .array (outs [0 ])
226
197
if (batch_id + 1 ) % 10 == 0 :
227
198
avg_cost_set = []
228
199
for test_data in test_reader ():
229
- avg_cost_np = exe .run (
230
- program = test_program ,
231
- feed = func_feed (feeding , test_data ),
232
- fetch_list = [avg_cost ])
200
+ avg_cost_np = exe .run (program = test_program ,
201
+ feed = feeder .feed (test_data ),
202
+ fetch_list = [avg_cost ])
233
203
avg_cost_set .append (avg_cost_np [0 ])
234
204
break # test only 1 segment for speeding up CI
235
205
@@ -279,23 +249,6 @@ def infer(use_cuda, save_dirname=None):
279
249
place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
280
250
exe = fluid .Executor (place )
281
251
282
- def create_lod_tensor (data , lod = None ):
283
- tensor = fluid .LoDTensor ()
284
- if lod is None :
285
- # Tensor, the shape is [batch_size, 1]
286
- index = 0
287
- lod_0 = [index ]
288
- for l in range (len (data )):
289
- index += 1
290
- lod_0 .append (index )
291
- lod = [lod_0 ]
292
- tensor .set_lod (lod )
293
-
294
- flattened_data = np .concatenate (data , axis = 0 ).astype ("int64" )
295
- flattened_data = flattened_data .reshape ([len (flattened_data ), 1 ])
296
- tensor .set (flattened_data , place )
297
- return tensor
298
-
299
252
inference_scope = fluid .core .Scope ()
300
253
with fluid .scope_guard (inference_scope ):
301
254
# Use fluid.io.load_inference_model to obtain the inference program desc,
@@ -307,26 +260,33 @@ def create_lod_tensor(data, lod=None):
307
260
308
261
# Use the first data from paddle.dataset.movielens.test() as input
309
262
assert feed_target_names [0 ] == "user_id"
310
- user_id = create_lod_tensor ([[1 ]])
263
+ # Use create_lod_tensor(data, lod, place) API to generate LoD Tensor
264
+ # where `data` is a list of sequences of index numbers, `lod` is
265
+ # the level of detail (lod) info associated with `data`.
266
+ # For example, data = [[10, 2, 3], [2, 3]] means that it contains
267
+ # two sequences of indexes, of length 3 and 2, respectively.
268
+ # Correspondingly, lod = [[3, 2]] contains one level of detail info,
269
+ # indicating that `data` consists of two sequences of length 3 and 2.
270
+ user_id = fluid .create_lod_tensor ([[1 ]], [[1 ]], place )
311
271
312
272
assert feed_target_names [1 ] == "gender_id"
313
- gender_id = create_lod_tensor ([[1 ]])
273
+ gender_id = fluid . create_lod_tensor ([[1 ]], [[ 1 ]], place )
314
274
315
275
assert feed_target_names [2 ] == "age_id"
316
- age_id = create_lod_tensor ([[0 ]])
276
+ age_id = fluid . create_lod_tensor ([[0 ]], [[ 1 ]], place )
317
277
318
278
assert feed_target_names [3 ] == "job_id"
319
- job_id = create_lod_tensor ([[10 ]])
279
+ job_id = fluid . create_lod_tensor ([[10 ]], [[ 1 ]], place )
320
280
321
281
assert feed_target_names [4 ] == "movie_id"
322
- movie_id = create_lod_tensor ([[783 ]])
282
+ movie_id = fluid . create_lod_tensor ([[783 ]], [[ 1 ]], place )
323
283
324
284
assert feed_target_names [5 ] == "category_id"
325
- category_id = create_lod_tensor ([[10 ], [ 8 ], [ 9 ]], [[0 , 3 ]])
285
+ category_id = fluid . create_lod_tensor ([[10 , 8 , 9 ]], [[3 ]], place )
326
286
327
287
assert feed_target_names [6 ] == "movie_title"
328
- movie_title = create_lod_tensor ([[1069 ], [ 4140 ], [ 2923 ], [ 710 ], [ 988 ]],
329
- [[ 0 , 5 ]])
288
+ movie_title = fluid . create_lod_tensor ([[1069 , 4140 , 2923 , 710 , 988 ]],
289
+ [[ 5 ]], place )
330
290
331
291
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
332
292
# and results will contain a list of data corresponding to fetch_targets.
0 commit comments