From 65aff91e863e90528eb779817e390746088f6431 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Thu, 2 Mar 2017 12:51:35 +0800 Subject: [PATCH] Stash --- demo/recommendation/api_train_v2.py | 30 ++++++++++++++++++----------- python/paddle/v2/trainer.py | 5 ++--- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/demo/recommendation/api_train_v2.py b/demo/recommendation/api_train_v2.py index 3ff86c2c4..93e2a26f7 100644 --- a/demo/recommendation/api_train_v2.py +++ b/demo/recommendation/api_train_v2.py @@ -70,27 +70,35 @@ def main(): parameters=parameters, update_equation=paddle.optimizer.Adam( learning_rate=1e-4)) + reader_dict = { + 'user_id': 0, + 'gender_id': 1, + 'age_id': 2, + 'job_id': 3, + 'movie_id': 4, + 'category_id': 5, + 'movie_title': 6, + 'score': 7 + } def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d Batch %d Cost %.2f" % ( event.pass_id, event.batch_id, event.cost) + elif isinstance(event, paddle.event.EndPass): + result = trainer.test(reader=paddle.reader.batched( + paddle.dataset.movielens.test(), batch_size=256)) + print result.cost trainer.train( reader=paddle.reader.batched( - paddle.dataset.movielens.train(), batch_size=256), + paddle.reader.shuffle( + paddle.dataset.movielens.train(), buf_size=8192), + batch_size=256), event_handler=event_handler, - reader_dict={ - 'user_id': 0, - 'gender_id': 1, - 'age_id': 2, - 'job_id': 3, - 'movie_id': 4, - 'category_id': 5, - 'movie_title': 6, - 'score': 7 - }) + reader_dict=reader_dict, + num_passes=10) if __name__ == '__main__': diff --git a/python/paddle/v2/trainer.py b/python/paddle/v2/trainer.py index 5003f55f3..f45b3ec97 100644 --- a/python/paddle/v2/trainer.py +++ b/python/paddle/v2/trainer.py @@ -123,9 +123,8 @@ class SGD(ITrainer): for each_param in self.__gradient_machine__.getParameters(): updater.update(each_param) # Get cost. We use numpy to calculate total cost for this batch. - cost_vec = out_args.getSlotValue(0) - cost_vec = cost_vec.copyToNumpyMat() - cost = cost_vec.sum() / len(data_batch) + cost_sum = out_args.sumCosts() + cost = cost_sum / len(data_batch) updater.finishBatch(cost) batch_evaluator.finish() event_handler( -- GitLab