2020-02-11 04:55:22 +00:00
|
|
|
r"""
|
|
|
|
|
|
|
|
.. _wandb:
|
|
|
|
|
|
|
|
WandbLogger
|
|
|
|
-------------
|
|
|
|
"""
|
2020-01-14 03:25:27 +00:00
|
|
|
import os
|
2020-03-04 14:33:39 +00:00
|
|
|
from argparse import Namespace
|
|
|
|
from typing import Optional, List, Dict, Union, Any
|
2020-01-14 03:25:27 +00:00
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
|
2020-01-14 03:25:27 +00:00
|
|
|
try:
|
|
|
|
import wandb
|
2020-02-25 19:52:39 +00:00
|
|
|
from wandb.wandb_run import Run
|
2020-03-19 13:14:29 +00:00
|
|
|
except ImportError: # pragma: no-cover
|
|
|
|
raise ImportError('You want to use `wandb` logger which is not installed yet,' # pragma: no-cover
|
2020-03-03 01:49:14 +00:00
|
|
|
' install it with `pip install wandb`.')
|
2020-01-14 03:25:27 +00:00
|
|
|
|
2020-03-19 13:14:29 +00:00
|
|
|
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_only
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WandbLogger(LightningLoggerBase):
|
|
|
|
"""
|
2020-02-11 04:55:22 +00:00
|
|
|
Logger for `W&B <https://www.wandb.com/>`_.
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): display name for the run.
|
|
|
|
save_dir (str): path where data is saved.
|
|
|
|
offline (bool): run offline (data can be streamed later to wandb servers).
|
|
|
|
id or version (str): sets the version, mainly used to resume a previous run.
|
|
|
|
anonymous (bool): enables or explicitly disables anonymous logging.
|
|
|
|
project (str): the name of the project to which this run will belong.
|
|
|
|
tags (list of str): tags associated with this run.
|
2020-04-02 12:55:34 +00:00
|
|
|
log_model (bool): save checkpoints in wandb dir to upload on W&B servers.
|
2020-01-17 11:03:31 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
--------
|
|
|
|
.. code-block:: python
|
|
|
|
|
2020-02-01 20:47:58 +00:00
|
|
|
from pytorch_lightning.loggers import WandbLogger
|
2020-01-17 11:03:31 +00:00
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
|
|
|
|
wandb_logger = WandbLogger()
|
|
|
|
trainer = Trainer(logger=wandb_logger)
|
2020-01-14 03:25:27 +00:00
|
|
|
"""
|
|
|
|
|
2020-04-15 00:32:33 +00:00
|
|
|
def __init__(self,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
save_dir: Optional[str] = None,
|
|
|
|
offline: bool = False,
|
|
|
|
id: Optional[str] = None,
|
|
|
|
anonymous: bool = False,
|
|
|
|
version: Optional[str] = None,
|
|
|
|
project: Optional[str] = None,
|
|
|
|
tags: Optional[List[str]] = None,
|
|
|
|
log_model: bool = False,
|
|
|
|
experiment=None,
|
|
|
|
entity=None):
|
2020-01-14 03:25:27 +00:00
|
|
|
super().__init__()
|
|
|
|
self._name = name
|
|
|
|
self._save_dir = save_dir
|
2020-03-03 01:49:14 +00:00
|
|
|
self._anonymous = 'allow' if anonymous else None
|
2020-01-14 03:25:27 +00:00
|
|
|
self._id = version or id
|
|
|
|
self._tags = tags
|
|
|
|
self._project = project
|
2020-01-21 16:20:45 +00:00
|
|
|
self._experiment = experiment
|
2020-01-14 03:25:27 +00:00
|
|
|
self._offline = offline
|
2020-02-06 19:39:57 +00:00
|
|
|
self._entity = entity
|
2020-04-02 12:55:34 +00:00
|
|
|
self._log_model = log_model
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
2020-04-03 19:03:00 +00:00
|
|
|
# args needed to reload correct experiment
|
|
|
|
state['_id'] = self._experiment.id if self._experiment is not None else None
|
|
|
|
|
2020-01-14 03:25:27 +00:00
|
|
|
# cannot be pickled
|
|
|
|
state['_experiment'] = None
|
|
|
|
return state
|
|
|
|
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
def experiment(self) -> Run:
|
2020-01-17 11:03:31 +00:00
|
|
|
r"""
|
|
|
|
|
|
|
|
Actual wandb object. To use wandb features do the following.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
self.logger.experiment.some_wandb_function()
|
|
|
|
|
|
|
|
"""
|
2020-01-14 03:25:27 +00:00
|
|
|
if self._experiment is None:
|
|
|
|
if self._offline:
|
2020-03-03 01:49:14 +00:00
|
|
|
os.environ['WANDB_MODE'] = 'dryrun'
|
2020-01-14 03:25:27 +00:00
|
|
|
self._experiment = wandb.init(
|
|
|
|
name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous,
|
2020-04-03 19:03:00 +00:00
|
|
|
reinit=True, id=self._id, resume='allow', tags=self._tags, entity=self._entity)
|
2020-04-02 12:55:34 +00:00
|
|
|
# save checkpoints in wandb dir to upload on W&B servers
|
|
|
|
if self._log_model:
|
|
|
|
self.save_dir = self._experiment.dir
|
2020-01-14 03:25:27 +00:00
|
|
|
return self._experiment
|
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
def watch(self, model: nn.Module, log: str = 'gradients', log_freq: int = 100):
|
2020-04-03 19:02:38 +00:00
|
|
|
self.experiment.watch(model, log=log, log_freq=log_freq)
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
|
|
|
|
params = self._convert_params(params)
|
2020-01-14 03:25:27 +00:00
|
|
|
self.experiment.config.update(params)
|
|
|
|
|
|
|
|
@rank_zero_only
|
2020-03-04 14:33:39 +00:00
|
|
|
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None:
|
2020-03-03 01:49:14 +00:00
|
|
|
if step is not None:
|
|
|
|
metrics['global_step'] = step
|
2020-01-21 16:20:45 +00:00
|
|
|
self.experiment.log(metrics)
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
def name(self) -> str:
|
2020-04-03 19:03:00 +00:00
|
|
|
# don't create an experiment if we don't have one
|
|
|
|
name = self._experiment.project_name() if self._experiment else None
|
|
|
|
return name
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
def version(self) -> str:
|
2020-04-03 19:03:00 +00:00
|
|
|
# don't create an experiment if we don't have one
|
|
|
|
return self._experiment.id if self._experiment else None
|