mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-09 11:32:07 +08:00
Allow metrics logged together with hparams (#1630)
Update tensorboard.py Update CHANGELOG.md Update tensorboard.py Update test_tensorboard.py Update test_tensorboard.py tests pep8
This commit is contained in:
committed by
J. Borovec
parent
9604d7bf89
commit
b83b8005f9
@@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
|
||||
### Added
|
||||
|
||||
### Changed
|
||||
|
||||
- Allow logging of metrics togther with hparams ([#1630](https://github.com/PyTorchLightning/pytorch-lightning/pull/1630))
|
||||
|
||||
### Deprecated
|
||||
|
||||
|
||||
@@ -101,7 +101,8 @@ class TensorBoardLogger(LightningLoggerBase):
|
||||
return self._experiment
|
||||
|
||||
@rank_zero_only
|
||||
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
|
||||
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace],
|
||||
metrics: Optional[Dict[str, Any]] = None) -> None:
|
||||
params = self._convert_params(params)
|
||||
params = self._flatten_dict(params)
|
||||
sanitized_params = self._sanitize_params(params)
|
||||
@@ -114,7 +115,9 @@ class TensorBoardLogger(LightningLoggerBase):
|
||||
)
|
||||
else:
|
||||
from torch.utils.tensorboard.summary import hparams
|
||||
exp, ssi, sei = hparams(sanitized_params, {})
|
||||
if metrics is None:
|
||||
metrics = {}
|
||||
exp, ssi, sei = hparams(sanitized_params, metrics)
|
||||
writer = self.experiment._get_file_writer()
|
||||
writer.add_summary(exp)
|
||||
writer.add_summary(ssi)
|
||||
|
||||
@@ -77,3 +77,19 @@ def test_tensorboard_log_hyperparams(tmpdir):
|
||||
"layer": torch.nn.BatchNorm1d
|
||||
}
|
||||
logger.log_hyperparams(hparams)
|
||||
|
||||
|
||||
def test_tensorboard_log_hparams_and_metrics(tmpdir):
|
||||
logger = TensorBoardLogger(tmpdir)
|
||||
hparams = {
|
||||
"float": 0.3,
|
||||
"int": 1,
|
||||
"string": "abc",
|
||||
"bool": True,
|
||||
"dict": {'a': {'b': 'c'}},
|
||||
"list": [1, 2, 3],
|
||||
"namespace": Namespace(foo=Namespace(bar='buzz')),
|
||||
"layer": torch.nn.BatchNorm1d
|
||||
}
|
||||
metrics = {'abc': torch.tensor([0.54])}
|
||||
logger.log_hyperparams(hparams, metrics)
|
||||
|
||||
Reference in New Issue
Block a user