Skip to content

Commit

Permalink
fix paddle/understand_sentiment_lstm.py (the size of last batch is sa…
Browse files Browse the repository at this point in the history
…mller than batch_size)
  • Loading branch information
chengduoZH committed Dec 12, 2017
1 parent 9c2b3c5 commit d3b8e07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion paddle/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ def run_benchmark(model, args):
iter = 0
im_num = 0
for pass_id in range(args.pass_num):
accuracy.reset(exe)
if iter == args.iterations:
break
accuracy.reset(exe)

for batch_id, data in enumerate(train_reader()):
if iter == args.skip_batch_num:
start_time = time.time()
Expand Down
14 changes: 9 additions & 5 deletions paddle/understand_sentiment_dynamic_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ def run_benchmark(model, args):
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

for it, pass_id in enumerate(xrange(args.pass_num)):
iterator = 0
for pass_id in xrange(args.pass_num):
accuracy.reset(exe)
if iter == args.iterations:
break
for data in train_reader():
tensor_words = to_lodtensor(map(lambda x: x[0], data), place)

Expand All @@ -140,8 +139,13 @@ def run_benchmark(model, args):
"label": tensor_label},
fetch_list=[avg_cost] + accuracy.metrics)
pass_acc = accuracy.eval(exe)
print("Iter: %d, loss: %s, acc: %s, pass_acc: %s" %
(it, str(loss), str(acc), str(pass_acc)))

iterator += 1
print("pass_id:%d, Iter: %d, loss: %s, acc: %s, pass_acc: %s" %
(pass_id, iterator, str(loss), str(acc), str(pass_acc)))

if iterator == args.iterations:
return


if __name__ == '__main__':
Expand Down
19 changes: 13 additions & 6 deletions paddle/understand_sentiment_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def run_benchmark(model, args):
word_dict = paddle.dataset.imdb.word_dict()

print("load word dict successfully")

dict_dim = len(word_dict)
data = fluid.layers.data(
name="words",
Expand Down Expand Up @@ -147,13 +147,15 @@ def run_benchmark(model, args):
place = fluid.CPUPlace() if args.device == 'CPU' else fluid.GPUPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
for it, pass_id in enumerate(xrange(args.pass_num)):

iterator = 0
for pass_id in xrange(args.pass_num):
accuracy.reset(exe)
if iter == args.iterations:
break
for data in train_reader():
chopped_data = chop_data(
data, chop_len=args.seq_len, batch_size=args.batch_size)
if len(data) < args.batch_size : continue

tensor_words, tensor_label = prepare_feed_data(chopped_data, place)

loss, acc = exe.run(
Expand All @@ -162,8 +164,13 @@ def run_benchmark(model, args):
"label": tensor_label},
fetch_list=[avg_cost] + accuracy.metrics)
pass_acc = accuracy.eval(exe)
print("Iter: %d, loss: %s, acc: %s, pass_acc: %s" %
(it, str(loss), str(acc), str(pass_acc)))

iterator += 1
print("pass_id:%d, Iter: %d, loss: %s, acc: %s, pass_acc: %s" %
(pass_id, iterator, str(loss), str(acc), str(pass_acc)))

if iterator == args.iterations:
return


if __name__ == '__main__':
Expand Down

0 comments on commit d3b8e07

Please sign in to comment.