Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Resolve metrics not being properly resetted on validation epoch end #9717

Merged
merged 5 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `lr_find` to generate same results on multiple calls ([#9704](https://github.com/PyTorchLightning/pytorch-lightning/pull/9704))


- Fixed `reset` metrics on validation epoch end ([#9717](https://github.com/PyTorchLightning/pytorch-lightning/pull/9717))



## [1.4.8] - 2021-09-22

- Fixed error reporting in DDP process reconciliation when processes are launched by an external agent ([#9389](https://github.com/PyTorchLightning/pytorch-lightning/pull/9389))
Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/loops/dataloader/evaluation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def _on_evaluation_end(self, *args: Any, **kwargs: Any) -> None:
self.trainer.call_hook("on_validation_end", *args, **kwargs)

# reset any `torchmetrics.Metric` and the logger connector state
tchaton marked this conversation as resolved.
Show resolved Hide resolved
self.trainer.logger_connector.reset_results(metrics=True)
tchaton marked this conversation as resolved.
Show resolved Hide resolved
self.trainer.logger_connector.reset_results()

def _on_evaluation_epoch_start(self, *args: Any, **kwargs: Any) -> None:
"""Runs ``on_epoch_start`` and ``on_{validation/test}_epoch_start`` hooks."""
Expand Down
66 changes: 66 additions & 0 deletions tests/trainer/logging_/test_eval_loop_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,69 @@ def validation_step(self, batch, batch_idx):
)

trainer.fit(model)


@pytest.mark.parametrize("val_check_interval", [0.5, 1.0])
def test_multiple_dataloader_reset(val_check_interval, tmpdir):
class TestModel(BoringModel):
def training_step(self, batch, batch_idx):
out = super().training_step(batch, batch_idx)
value = 1 + batch_idx
if self.current_epoch != 0:
value *= 10
self.log("batch_idx", value, on_step=True, on_epoch=True, prog_bar=True)
return out

def training_epoch_end(self, outputs):
if val_check_interval == 1.0:
metrics = self.trainer.progress_bar_metrics
if self.current_epoch == 0:
assert metrics["batch_idx_epoch"] == (15 / 5.0)
else:
assert metrics["batch_idx_epoch"] == (150 / 5.0)
tchaton marked this conversation as resolved.
Show resolved Hide resolved

def validation_step(self, batch, batch_idx, dataloader_idx):
value = (1 + batch_idx) * (2 if dataloader_idx == 1 else 1)
tchaton marked this conversation as resolved.
Show resolved Hide resolved
if self.current_epoch != 0:
value *= 10
self.log("val_loss", value, on_step=False, on_epoch=True, prog_bar=True, logger=True)
return value

def validation_epoch_end(self, outputs):
if self.current_epoch == 0:
assert sum(outputs[0]) / 5 == 3
assert sum(outputs[1]) / 5 == 6
else:
assert sum(outputs[0]) / 5 == 30
assert sum(outputs[1]) / 5 == 60

tot_loss = torch.tensor(0.0)
for loss in outputs:
tot_loss += sum(loss) / len(loss)
tot_loss = tot_loss / len(outputs)
tchaton marked this conversation as resolved.
Show resolved Hide resolved
if self.current_epoch == 0:
assert tot_loss == (3 + 6) / 2
else:
assert tot_loss == (30 + 60) / 2
self.log("tot_val_loss", tot_loss, prog_bar=True, logger=True)
tchaton marked this conversation as resolved.
Show resolved Hide resolved
assert self.trainer._results["validation_step.val_loss.0"].cumulated_batch_size == 5
assert self.trainer._results["validation_step.val_loss.1"].cumulated_batch_size == 5

def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=0.1)

tchaton marked this conversation as resolved.
Show resolved Hide resolved
def val_dataloader(self):
return [super().val_dataloader(), super().val_dataloader()]

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
limit_train_batches=5,
limit_val_batches=5,
num_sanity_val_steps=0,
val_check_interval=val_check_interval,
max_epochs=3,
log_every_n_steps=1,
weights_summary=None,
)
trainer.fit(model)