2020-08-20 02:03:22 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
"""
|
|
|
|
Weights and Biases
|
|
|
|
------------------
|
2020-02-11 04:55:22 +00:00
|
|
|
"""
|
2020-01-14 03:25:27 +00:00
|
|
|
import os
|
2020-03-04 14:33:39 +00:00
|
|
|
from argparse import Namespace
|
2020-09-19 16:51:43 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Union
|
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
|
2020-05-25 11:31:35 +00:00
|
|
|
wandb = None
|
|
|
|
Run = None
|
2020-01-14 03:25:27 +00:00
|
|
|
|
2020-06-30 22:09:16 +00:00
|
|
|
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment
|
2020-04-24 21:21:00 +00:00
|
|
|
from pytorch_lightning.utilities import rank_zero_only
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WandbLogger(LightningLoggerBase):
|
2020-09-19 16:51:43 +00:00
|
|
|
r"""
|
2020-04-16 16:04:12 +00:00
|
|
|
Log using `Weights and Biases <https://www.wandb.com/>`_. Install it with pip:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
pip install wandb
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
Args:
|
2020-04-16 16:04:12 +00:00
|
|
|
name: Display name for the run.
|
|
|
|
save_dir: Path where data is saved.
|
|
|
|
offline: Run offline (data can be streamed later to wandb servers).
|
|
|
|
id: Sets the version, mainly used to resume a previous run.
|
|
|
|
anonymous: Enables or explicitly disables anonymous logging.
|
|
|
|
version: Sets the version, mainly used to resume a previous run.
|
|
|
|
project: The name of the project to which this run will belong.
|
|
|
|
log_model: Save checkpoints in wandb dir to upload on W&B servers.
|
2020-09-19 16:51:43 +00:00
|
|
|
experiment: WandB experiment object.
|
|
|
|
\**kwargs: Additional arguments like `entity`, `group`, `tags`, etc. used by
|
|
|
|
:func:`wandb.init` can be passed as keyword arguments in this logger.
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
Example:
|
2020-09-25 14:00:02 +00:00
|
|
|
|
|
|
|
from pytorch_lightning.loggers import WandbLogger
|
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
wandb_logger = WandbLogger()
|
|
|
|
trainer = Trainer(logger=wandb_logger)
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
See Also:
|
|
|
|
- `Tutorial <https://app.wandb.ai/cayush/pytorchlightning/reports/
|
|
|
|
Use-Pytorch-Lightning-with-Weights-%26-Biases--Vmlldzo2NjQ1Mw>`__
|
|
|
|
on how to use W&B with Pytorch Lightning.
|
|
|
|
|
2020-01-14 03:25:27 +00:00
|
|
|
"""
|
|
|
|
|
2020-09-19 16:51:43 +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,
|
|
|
|
log_model: bool = False,
|
|
|
|
experiment=None,
|
|
|
|
**kwargs
|
|
|
|
):
|
2020-09-25 14:00:02 +00:00
|
|
|
if wandb is None:
|
2020-05-25 11:31:35 +00:00
|
|
|
raise ImportError('You want to use `wandb` logger which is not installed yet,' # pragma: no-cover
|
|
|
|
' install it with `pip install wandb`.')
|
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._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-04-02 12:55:34 +00:00
|
|
|
self._log_model = log_model
|
2020-09-19 16:51:43 +00:00
|
|
|
self._kwargs = kwargs
|
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-06-30 22:09:16 +00:00
|
|
|
@rank_zero_experiment
|
2020-02-25 19:52:39 +00:00
|
|
|
def experiment(self) -> Run:
|
2020-01-17 11:03:31 +00:00
|
|
|
r"""
|
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
Actual wandb object. To use wandb features in your
|
|
|
|
:class:`~pytorch_lightning.core.lightning.LightningModule` do the following.
|
2020-01-17 11:03:31 +00:00
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
Example::
|
2020-01-17 11:03:31 +00:00
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
self.logger.experiment.some_wandb_function()
|
2020-01-17 11:03:31 +00:00
|
|
|
|
2020-04-16 16:04:12 +00:00
|
|
|
"""
|
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-09-19 16:51:43 +00:00
|
|
|
reinit=True, id=self._id, resume='allow', **self._kwargs)
|
2020-04-02 12:55:34 +00:00
|
|
|
# save checkpoints in wandb dir to upload on W&B servers
|
|
|
|
if self._log_model:
|
2020-07-09 11:15:41 +00:00
|
|
|
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-07-08 05:45:25 +00:00
|
|
|
params = self._flatten_dict(params)
|
2020-04-24 14:29:24 +00:00
|
|
|
self.experiment.config.update(params, allow_val_change=True)
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
@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-06-30 22:09:16 +00:00
|
|
|
assert rank_zero_only.rank == 0, 'experiment tried to log from global_rank != 0'
|
|
|
|
|
2020-06-02 22:46:02 +00:00
|
|
|
self.experiment.log({'global_step': step, **metrics} if step is not None else metrics)
|
2020-01-14 03:25:27 +00:00
|
|
|
|
2020-07-09 11:15:41 +00:00
|
|
|
@property
|
|
|
|
def save_dir(self) -> Optional[str]:
|
|
|
|
return self._save_dir
|
|
|
|
|
2020-01-14 03:25:27 +00:00
|
|
|
@property
|
2020-06-29 01:36:46 +00:00
|
|
|
def name(self) -> Optional[str]:
|
2020-04-03 19:03:00 +00:00
|
|
|
# don't create an experiment if we don't have one
|
2020-07-09 11:15:41 +00:00
|
|
|
return self._experiment.project_name() if self._experiment else self._name
|
2020-01-14 03:25:27 +00:00
|
|
|
|
|
|
|
@property
|
2020-06-29 01:36:46 +00:00
|
|
|
def version(self) -> Optional[str]:
|
2020-04-03 19:03:00 +00:00
|
|
|
# don't create an experiment if we don't have one
|
2020-07-09 11:15:41 +00:00
|
|
|
return self._experiment.id if self._experiment else self._id
|