From 553c279b5b76de9d5053db8052cdd2c740c6d1b2 Mon Sep 17 00:00:00 2001 From: Yiqun Liu Date: Wed, 25 Nov 2020 13:12:57 +0800 Subject: [PATCH] Move tensor to numpy from print. (#4950) --- dygraph/ptb_lm/ptb_dy.py | 6 +++--- dygraph/seq2seq/train.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dygraph/ptb_lm/ptb_dy.py b/dygraph/ptb_lm/ptb_dy.py index c4980bcb..c99d4916 100644 --- a/dygraph/ptb_lm/ptb_dy.py +++ b/dygraph/ptb_lm/ptb_dy.py @@ -444,8 +444,9 @@ def train_ptb_lm(): dy_loss.backward() sgd.minimize(dy_loss) - ptb_model.clear_gradients() + + global_lr = sgd._global_learning_rate().numpy() total_loss += out_loss iters += num_steps total_batch_num = total_batch_num + 1 #this is for benchmark @@ -457,8 +458,7 @@ def train_ptb_lm(): ppl = np.exp(total_loss / iters) print( "-- Epoch:[%d]; Batch:[%d]; ppl: %.5f, lr: %.5f, loss: %.5f, batch_cost: %.5f sec, reader_cost: %.5f sec, ips: %.5f words/sec" - % (epoch_id, batch_id, ppl[0], - sgd._global_learning_rate().numpy(), out_loss, + % (epoch_id, batch_id, ppl[0], global_lr, out_loss, batch_cost_avg.get_average(), reader_cost_avg.get_average(), batch_size / batch_cost_avg.get_average())) diff --git a/dygraph/seq2seq/train.py b/dygraph/seq2seq/train.py index 18fdee1d..1457623a 100644 --- a/dygraph/seq2seq/train.py +++ b/dygraph/seq2seq/train.py @@ -170,22 +170,22 @@ def main(): batch, epoch_id=epoch_id) word_count += word_num loss = model(input_data_feed) - # print(loss.numpy()[0]) loss.backward() optimizer.minimize(loss) model.clear_gradients() total_loss += loss * batch_size + total_loss_value = total_loss.numpy() - train_batch_cost = time.time() - batch_start - batch_times.append(train_batch_cost) + batch_times.append(time.time() - batch_start) if batch_id > 0 and batch_id % 100 == 0: print( - "-- Epoch:[%d]; Batch:[%d]; ppl: %.5f, batch_cost: %.5f s, reader_cost: %.5f s, ips: %.5f words/s" - % (epoch_id, batch_id, np.exp(total_loss.numpy() / + "-- Epoch:[%d]; Batch:[%d]; ppl: %.5f, batch_cost: %.5f sec, reader_cost: %.5f sec, ips: %.5f words/sec" + % (epoch_id, batch_id, np.exp(total_loss_value / word_count), - train_batch_cost, total_reader_cost / 100, + (time.time() - interval_time_start) / 100, + total_reader_cost / 100, word_count / (time.time() - interval_time_start))) - ce_ppl.append(np.exp(total_loss.numpy() / word_count)) + ce_ppl.append(np.exp(total_loss_value / word_count)) total_loss = 0.0 word_count = 0.0 total_reader_cost = 0.0 @@ -194,7 +194,7 @@ def main(): train_epoch_cost = time.time() - epoch_start print( - "\nTrain epoch:[%d]; epoch_cost: %.5f s; avg_batch_cost: %.5f s/step\n" + "\nTrain epoch:[%d]; epoch_cost: %.5f sec; avg_batch_cost: %.5f s/step\n" % (epoch_id, train_epoch_cost, sum(batch_times) / len(batch_times))) ce_time.append(train_epoch_cost) -- GitLab