Skip to content

Commit b8371fa

Browse files
Fixes #2972 #2946 (#2986)
* add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add val step arg to metrics * add step metrics * add step metrics
1 parent f952dec commit b8371fa

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

pytorch_lightning/trainer/evaluation_loop.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def add_progress_bar_metrics(self, *args):
217217
"""Warning: this is just empty shell for code implemented in other class."""
218218

219219
@abstractmethod
220-
def log_metrics(self, *args):
220+
def log_metrics(self, *args, **kwargs):
221221
"""Warning: this is just empty shell for code implemented in other class."""
222222

223223
@abstractmethod
@@ -379,7 +379,7 @@ def _evaluate(
379379

380380
dl_outputs.append(output)
381381

382-
self.__eval_add_step_metrics(output)
382+
self.__eval_add_step_metrics(output, batch_idx)
383383

384384
# track debug metrics
385385
self.dev_debugger.track_eval_loss_history(test_mode, batch_idx, dataloader_idx, output)
@@ -505,14 +505,19 @@ def __gather_epoch_end_eval_results(self, outputs):
505505
eval_results = eval_results[0]
506506
return eval_results
507507

508-
def __eval_add_step_metrics(self, output):
508+
def __eval_add_step_metrics(self, output, batch_idx):
509509
# track step level metrics
510510
if isinstance(output, EvalResult) and not self.running_sanity_check:
511511
step_log_metrics = output.batch_log_metrics
512512
step_pbar_metrics = output.batch_pbar_metrics
513513

514514
if len(step_log_metrics) > 0:
515-
self.log_metrics(step_log_metrics, {})
515+
# make the metrics appear as a different line in the same graph
516+
metrics_by_epoch = {}
517+
for k, v in step_log_metrics.items():
518+
metrics_by_epoch[f'{k}/epoch_{self.current_epoch}'] = v
519+
520+
self.log_metrics(metrics_by_epoch, {}, step=batch_idx)
516521

517522
if len(step_pbar_metrics) > 0:
518523
self.add_progress_bar_metrics(step_pbar_metrics)

pytorch_lightning/trainer/logging.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ def log_metrics(self, metrics, grad_norm_dic, step=None):
6464

6565
if "step" in scalar_metrics and step is None:
6666
step = scalar_metrics.pop("step")
67-
else:
67+
68+
elif step is None:
6869
# added metrics by Lightning for convenience
6970
scalar_metrics['epoch'] = self.current_epoch
7071
step = step if step is not None else self.global_step
72+
7173
# log actual metrics
7274
if self.is_global_zero and self.logger is not None:
7375
self.logger.agg_and_log_metrics(scalar_metrics, step=step)

tests/trainer/test_validation_steps_result_return.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,15 @@ def test_val_step_only_step_metrics(tmpdir):
214214

215215
# make sure we logged the correct epoch metrics
216216
total_empty_epoch_metrics = 0
217+
epoch = 0
217218
for metric in trainer.dev_debugger.logged_metrics:
219+
if 'epoch' in metric:
220+
epoch += 1
218221
if len(metric) > 2:
219222
assert 'no_val_no_pbar' not in metric
220223
assert 'val_step_pbar_acc' not in metric
221-
assert metric['val_step_log_acc']
222-
assert metric['val_step_log_pbar_acc']
224+
assert metric[f'val_step_log_acc/epoch_{epoch}']
225+
assert metric[f'val_step_log_pbar_acc/epoch_{epoch}']
223226
else:
224227
total_empty_epoch_metrics += 1
225228

@@ -228,6 +231,8 @@ def test_val_step_only_step_metrics(tmpdir):
228231
# make sure we logged the correct epoch pbar metrics
229232
total_empty_epoch_metrics = 0
230233
for metric in trainer.dev_debugger.pbar_added_metrics:
234+
if 'epoch' in metric:
235+
epoch += 1
231236
if len(metric) > 2:
232237
assert 'no_val_no_pbar' not in metric
233238
assert 'val_step_log_acc' not in metric
@@ -288,11 +293,12 @@ def test_val_step_epoch_step_metrics(tmpdir):
288293
for metric_idx in range(0, len(trainer.dev_debugger.logged_metrics), batches + 1):
289294
batch_metrics = trainer.dev_debugger.logged_metrics[metric_idx: metric_idx + batches]
290295
epoch_metric = trainer.dev_debugger.logged_metrics[metric_idx + batches]
296+
epoch = epoch_metric['epoch']
291297

292298
# make sure the metric was split
293299
for batch_metric in batch_metrics:
294-
assert 'step_val_step_log_acc' in batch_metric
295-
assert 'step_val_step_log_pbar_acc' in batch_metric
300+
assert f'step_val_step_log_acc/epoch_{epoch}' in batch_metric
301+
assert f'step_val_step_log_pbar_acc/epoch_{epoch}' in batch_metric
296302

297303
# make sure the epoch split was correct
298304
assert 'epoch_val_step_log_acc' in epoch_metric
@@ -421,11 +427,11 @@ def test_val_step_full_loop_result_dp(tmpdir):
421427
assert 'train_step_metric' in seen_keys
422428
assert 'train_step_end_metric' in seen_keys
423429
assert 'epoch_train_epoch_end_metric' in seen_keys
424-
assert 'step_validation_step_metric' in seen_keys
430+
assert 'step_validation_step_metric/epoch_0' in seen_keys
425431
assert 'epoch_validation_step_metric' in seen_keys
426432
assert 'validation_step_end_metric' in seen_keys
427433
assert 'validation_epoch_end_metric' in seen_keys
428-
assert 'step_test_step_metric' in seen_keys
434+
assert 'step_test_step_metric/epoch_2' in seen_keys
429435
assert 'epoch_test_step_metric' in seen_keys
430436
assert 'test_step_end_metric' in seen_keys
431437
assert 'test_epoch_end_metric' in seen_keys

0 commit comments

Comments
 (0)