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

Add step parameter to TensorBoardLogger.log_hyperparams #20176

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions src/lightning/fabric/loggers/tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,19 @@ def log_metrics(self, metrics: Mapping[str, float], step: Optional[int] = None)
@override
@rank_zero_only
def log_hyperparams(
self, params: Union[Dict[str, Any], Namespace], metrics: Optional[Dict[str, Any]] = None
self,
params: Union[Dict[str, Any], Namespace],
metrics: Optional[Dict[str, Any]] = None,
step: Optional[int] = None,
) -> None:
"""Record hyperparameters. TensorBoard logs with and without saved hyperparameters are incompatible, the
hyperparameters are then not displayed in the TensorBoard. Please delete or move the previously saved logs to
display the new ones with hyperparameters.

Args:
params: a dictionary-like container with the hyperparameters
params: A dictionary-like container with the hyperparameters
metrics: Dictionary with metric names as keys and measured quantities as values
step: Optional global step number for the logged metrics

"""
params = _convert_params(params)
Expand All @@ -243,7 +247,7 @@ def log_hyperparams(
metrics = {"hp_metric": metrics}

if metrics:
self.log_metrics(metrics, 0)
self.log_metrics(metrics, step)

if _TENSORBOARD_AVAILABLE:
from torch.utils.tensorboard.summary import hparams
Expand All @@ -252,9 +256,9 @@ def log_hyperparams(

exp, ssi, sei = hparams(params, metrics)
writer = self.experiment._get_file_writer()
writer.add_summary(exp)
writer.add_summary(ssi)
writer.add_summary(sei)
writer.add_summary(exp, step)
writer.add_summary(ssi, step)
writer.add_summary(sei, step)

@override
@rank_zero_only
Expand Down
10 changes: 7 additions & 3 deletions src/lightning/pytorch/loggers/tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ def save_dir(self) -> str:
@override
@rank_zero_only
def log_hyperparams(
self, params: Union[Dict[str, Any], Namespace], metrics: Optional[Dict[str, Any]] = None
self,
params: Union[Dict[str, Any], Namespace],
metrics: Optional[Dict[str, Any]] = None,
step: Optional[int] = None,
) -> None:
"""Record hyperparameters. TensorBoard logs with and without saved hyperparameters are incompatible, the
hyperparameters are then not displayed in the TensorBoard. Please delete or move the previously saved logs to
display the new ones with hyperparameters.

Args:
params: a dictionary-like container with the hyperparameters
params: A dictionary-like container with the hyperparameters
metrics: Dictionary with metric names as keys and measured quantities as values
step: Optional global step number for the logged metrics

"""
if _OMEGACONF_AVAILABLE:
Expand All @@ -175,7 +179,7 @@ def log_hyperparams(
else:
self.hparams.update(params)

return super().log_hyperparams(params=params, metrics=metrics)
return super().log_hyperparams(params=params, metrics=metrics, step=step)

@override
@rank_zero_only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def test_step(self, batch, batch_idx):
"valid_loss_1",
}
assert mock_log_metrics.mock_calls == [
call({"hp_metric": -1}, 0),
call({"hp_metric": -1}, None),
call(metrics={"train_loss": ANY, "epoch": 0}, step=0),
call(metrics={"valid_loss_0_step": ANY, "valid_loss_2": ANY}, step=0),
call(metrics={"valid_loss_0_step": ANY, "valid_loss_2": ANY}, step=1),
Expand Down
Loading