Skip to content

Commit

Permalink
Make default tqdm dict overridable (#749)
Browse files Browse the repository at this point in the history
* overridable tqdm_dict

* Slim down default tqdm_metrics

* gpu fix
  • Loading branch information
kuynzereb committed Feb 5, 2020
1 parent 734b28e commit 5035ce5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
19 changes: 19 additions & 0 deletions pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,25 @@ def on_save_checkpoint(self, checkpoint):
"""

def get_tqdm_dict(self):
r"""
Additional items to be displayed in the progress bar.
Return:
Dictionary with the items to be displayed in the progress bar.
"""
tqdm_dict = {
'loss': '{:.3f}'.format(self.trainer.avg_loss)
}

if self.trainer.truncated_bptt_steps is not None:
tqdm_dict['split_idx'] = self.trainer.split_idx

if self.trainer.logger is not None and self.trainer.logger.version is not None:
tqdm_dict['v_num'] = self.trainer.logger.version

return tqdm_dict


def load_hparams_from_tags_csv(tags_csv):
if not os.path.isfile(tags_csv):
Expand Down
5 changes: 2 additions & 3 deletions pytorch_lightning/trainer/evaluation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def run_evaluation(self, test=False):
desc = 'Testing' if test else 'Validating'
pbar = tqdm(desc=desc, total=max_batches, leave=test, position=position,
disable=not self.show_progress_bar, dynamic_ncols=True,
unit='batch', file=sys.stdout)
file=sys.stdout)
setattr(self, f'{"test" if test else "val"}_progress_bar', pbar)

# run evaluation
Expand All @@ -319,9 +319,8 @@ def run_evaluation(self, test=False):
model.on_post_performance_check()

# add model specific metrics
tqdm_metrics = self.training_tqdm_dict
if not test:
self.main_progress_bar.set_postfix(**tqdm_metrics)
self.main_progress_bar.set_postfix(**self.training_tqdm_dict)

# close progress bar
if test:
Expand Down
22 changes: 4 additions & 18 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,23 +681,9 @@ def training_tqdm_dict(self):
"""Read-only for tqdm metrics.
:return:
"""
tqdm_dict = {
'loss': '{0:.3f}'.format(self.avg_loss),
'batch_idx': '{}'.format(self.batch_idx),
}
ref_model = self.model if not self.data_parallel else self.model.module

if self.truncated_bptt_steps is not None:
tqdm_dict['split_idx'] = self.split_idx

if self.logger is not None and self.logger.version is not None:
tqdm_dict['v_num'] = self.logger.version

tqdm_dict.update(self.tqdm_metrics)

if self.on_gpu:
tqdm_dict['gpu'] = '{}'.format(torch.cuda.current_device())

return tqdm_dict
return dict(**ref_model.get_tqdm_dict(), **self.tqdm_metrics)

@property
def tng_tqdm_dic(self):
Expand Down Expand Up @@ -855,7 +841,7 @@ def run_pretrain_routine(self, model):
pbar = tqdm(desc='Validation sanity check',
total=self.num_sanity_val_steps * len(self.get_val_dataloaders()),
leave=False, position=2 * self.process_position,
disable=not self.show_progress_bar, dynamic_ncols=True, unit='batch')
disable=not self.show_progress_bar, dynamic_ncols=True)
self.main_progress_bar = pbar
# dummy validation progress bar
self.val_progress_bar = tqdm(disable=True)
Expand All @@ -873,7 +859,7 @@ def run_pretrain_routine(self, model):

# init progress bar
pbar = tqdm(leave=True, position=2 * self.process_position,
disable=not self.show_progress_bar, dynamic_ncols=True, unit='batch',
disable=not self.show_progress_bar, dynamic_ncols=True,
file=sys.stdout)
self.main_progress_bar = pbar

Expand Down

0 comments on commit 5035ce5

Please sign in to comment.