Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
馃摗 馃搲 Adding Tensorboard Tracker (#416)
Co-authored-by: Charles Tapley Hoyt <cthoyt@gmail.com> Co-authored-by: Max Berrendorf <berrendorf@dbs.ifi.lmu.de> Co-authored-by: PyKEEN_bot <pykeen2019@gmail.com>
- Loading branch information
1 parent
eb9919d
commit 811fa2b
Showing
9 changed files
with
219 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ Tracking Results during Training | |
using_mlflow | ||
using_neptune | ||
using_wandb | ||
using_tensorboard | ||
using_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
Using Tensorboard | ||
================= | ||
`Tensorboard <https://www.tensorflow.org/tensorboard/>`_ is a service for tracking experimental results | ||
during or after training. It is part of the larger Tensorflow project but can be used independently of it. | ||
|
||
Installing Tensorboard | ||
---------------------- | ||
The :mod:`tensorboard` package can either be installed directly with ``pip install tensorboard`` | ||
or with PyKEEN by using the ``tensorboard`` extra in ``pip install pykeen[tensorboard]``. | ||
|
||
.. note:: | ||
|
||
Tensorboard logs can created without actually installing tensorboard itself. | ||
However, if you want to view and interact with the data created via the tracker, it must be installed. | ||
|
||
Starting Tensorboard | ||
-------------------- | ||
The :mod:`tensorboard` web application can be started from the command line with | ||
|
||
.. code-block:: shell | ||
$ tensorboard --logdir=~/.data/pykeen/logs/tensorboard/ | ||
where the value passed to the ``--logdir`` is location of log directory. By default, PyKEEN logs to | ||
``~/.data/pykeen/logs/tensorboard/``, but this is configurable. | ||
The Tensorboard can then be accessed via a browser at: http://localhost:6006/ | ||
|
||
.. note:: | ||
|
||
It is not required for the Tensorboard process to be running while the training is happening. Indeed, | ||
it only needs to be started once you want to interact with and view the logs. It can be stopped at any | ||
time and the logs will persist in the filesystem. | ||
|
||
Minimal Pipeline Example | ||
------------------------ | ||
The tensorboard tracker can be used during training with the :func:`pykeen.pipeline.pipeline` as follows: | ||
|
||
.. code-block:: python | ||
from pykeen.pipeline import pipeline | ||
pipeline_result = pipeline( | ||
model='RotatE', | ||
dataset='Kinships', | ||
result_tracker='tensorboard', | ||
) | ||
It is placed in a subdirectory of :mod:`pystow` default data directory of PyKEEN called ``tensorboard``, | ||
which will likely be at ``~/.data/pykeen/logs/tensorboard`` on your system. The file is named based on the | ||
current time if no alternative is provided. | ||
|
||
Specifying a Log Name | ||
--------------------- | ||
If you want to specify the name of the log file in the default directory, use the ``experiment_name`` keyword | ||
argument like: | ||
|
||
.. code-block:: python | ||
from pykeen.pipeline import pipeline | ||
pipeline_result = pipeline( | ||
model='RotatE', | ||
dataset='Kinships', | ||
result_tracker='tensorboard', | ||
result_tracker_kwargs=dict( | ||
experiment_name='rotate-kinships', | ||
), | ||
) | ||
Specifying a Custom Log Directory | ||
--------------------------------- | ||
If you want to specify a custom directory to store the tensorboard logs, use the ``experiment_path`` keyword | ||
argument like: | ||
|
||
.. code-block:: python | ||
from pykeen.pipeline import pipeline | ||
pipeline_result = pipeline( | ||
model='RotatE', | ||
dataset='Kinships', | ||
result_tracker='tensorboard', | ||
result_tracker_kwargs=dict( | ||
experiment_path='tb-logs/rotate-kinships', | ||
), | ||
) | ||
.. warning:: | ||
|
||
Please be aware that if you re-run an experiment using the same directory, then the logs will be combined. | ||
It is advisable to use a unique sub-directory for each experiment to allow for easy comparison. | ||
|
||
Minimal HPO Pipeline Example | ||
---------------------------- | ||
Tensorboard tracking can also be used in conjunction with a HPO pipeline as follows: | ||
|
||
.. code-block:: python | ||
from pykeen.pipeline import pipeline | ||
hpo_pipeline_result = hpo_pipeline( | ||
n_trials=30, | ||
dataset='Nations', | ||
model='TransE', | ||
result_tracker='tensorboard', | ||
) | ||
This provides a way to compare directly between different trails and parameter configurations. Please not that it | ||
is recommended to leave the experiment name as the default value here to allow for a directory to be created per | ||
trail. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""An adapter for TensorBoard.""" | ||
|
||
import pathlib | ||
import time | ||
from typing import Any, Dict, Mapping, Optional, TYPE_CHECKING, Union | ||
|
||
from .base import ResultTracker | ||
from ..constants import PYKEEN_LOGS | ||
from ..utils import flatten_dictionary | ||
|
||
if TYPE_CHECKING: | ||
import torch.utils.tensorboard | ||
|
||
__all__ = [ | ||
'TensorBoardResultTracker', | ||
] | ||
|
||
|
||
class TensorBoardResultTracker(ResultTracker): | ||
"""A tracker for TensorBoard.""" | ||
|
||
summary_writer: 'torch.utils.tensorboard.SummaryWriter' | ||
path: pathlib.Path | ||
|
||
def __init__( | ||
self, | ||
experiment_path: Union[None, str, pathlib.Path] = None, | ||
experiment_name: Optional[str] = None, | ||
): | ||
""" | ||
Initialize result tracking via Tensorboard. | ||
:param experiment_path: | ||
The experiment path. A custom path at which the tensorboard logs will be saved. | ||
:param experiment_name: | ||
The name of the experiment, will be used as a sub directory name for the logging. If no default is given, | ||
the current time is used. If set, experiment_path is set, this argument has no effect. | ||
""" | ||
import torch.utils.tensorboard | ||
|
||
if isinstance(experiment_path, str): | ||
path = pathlib.Path(experiment_path) | ||
elif isinstance(experiment_path, pathlib.Path): | ||
path = experiment_path | ||
else: | ||
if experiment_name is None: | ||
experiment_name = time.strftime('%Y-%m-%d-%H-%M-%S') | ||
path = PYKEEN_LOGS.joinpath("tensorboard", experiment_name) | ||
|
||
# if we really need access to the path later, we can expose it as a property | ||
# via self.writer.log_dir | ||
self.writer = torch.utils.tensorboard.SummaryWriter(log_dir=path.resolve()) | ||
|
||
def log_metrics( | ||
self, | ||
metrics: Mapping[str, float], | ||
step: Optional[int] = None, | ||
prefix: Optional[str] = None, | ||
) -> None: # noqa: D102 | ||
metrics = flatten_dictionary(dictionary=metrics, prefix=prefix) | ||
for key, value in metrics.items(): | ||
self.writer.add_scalar(tag=key, scalar_value=value, global_step=step) | ||
self.writer.flush() | ||
|
||
def log_params(self, params: Mapping[str, Any], prefix: Optional[str] = None) -> None: # noqa: D102 | ||
params = flatten_dictionary(dictionary=params, prefix=prefix) | ||
for key, value in params.items(): | ||
self.writer.add_text(tag=str(key), text_string=str(value)) | ||
self.writer.flush() | ||
|
||
def end_run(self) -> None: # noqa: D102 | ||
self.writer.flush() | ||
self.writer.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters