Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions PaddleNLP/language_model/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,25 @@

SEED = 123


class TimeCostAverage(object):
def __init__(self):
self.reset()

def reset(self):
self.cnt = 0
self.total_time = 0

def record(self, usetime):
self.cnt += 1
self.total_time += usetime

def get_average(self):
if self.cnt == 0:
return 0
return self.total_time / self.cnt


@contextlib.contextmanager
def profile_context(profile=True, profiler_path='/tmp/paddingrnn.profile'):
if profile:
Expand Down Expand Up @@ -339,7 +344,8 @@ def train_an_epoch(epoch_id, batch_times):
ppl = np.exp(total_loss / iters)
print(
"-- Epoch:[%d]; Batch:[%d]; Time: %.5f s; ppl: %.5f, lr: %.5f"
% (epoch_id, batch_id, batch_cost_avg.get_average(), ppl[0], lr[0]))
% (epoch_id, batch_id, batch_cost_avg.get_average(), ppl[0],
lr[0]))
batch_cost_avg.reset()

# profiler tools for benchmark
Expand Down Expand Up @@ -402,7 +408,8 @@ def train_an_epoch_dataloader(epoch_id, batch_times):
ppl = np.exp(total_loss / iters)
print(
"-- Epoch:[%d]; Batch:[%d]; Time: %.5f s; ppl: %.5f, lr: %.5f"
% (epoch_id, batch_id, batch_cost_avg.get_average(), ppl[0], lr[0]))
% (epoch_id, batch_id, batch_cost_avg.get_average(),
ppl[0], lr[0]))
batch_cost_avg.reset()

batch_id += 1
Expand Down Expand Up @@ -507,4 +514,3 @@ def is_valid_data(data, batch_size, num_steps):

if __name__ == '__main__':
main()