|
| 1 | +""" |
| 2 | +Log using `W&B <https://www.wandb.com>`_ |
| 3 | +
|
| 4 | +.. code-block:: python |
| 5 | +
|
| 6 | + >>> from pytorch_lightning.logging import WandbLogger |
| 7 | + >>> from pytorch_lightning import Trainer |
| 8 | + >>> wandb_logger = WandbLogger() |
| 9 | + >>> trainer = Trainer(logger=wandb_logger) |
| 10 | +
|
| 11 | +
|
| 12 | +Use the logger anywhere in you LightningModule as follows: |
| 13 | +
|
| 14 | +.. code-block:: python |
| 15 | +
|
| 16 | + def train_step(...): |
| 17 | + # example |
| 18 | + self.logger.experiment.whatever_wandb_supports(...) |
| 19 | +
|
| 20 | + def any_lightning_module_function_or_hook(...): |
| 21 | + self.logger.experiment.whatever_wandb_supports(...) |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | + |
| 27 | +try: |
| 28 | + import wandb |
| 29 | +except ImportError: |
| 30 | + raise ImportError('Missing wandb package.') |
| 31 | + |
| 32 | +from .base import LightningLoggerBase, rank_zero_only |
| 33 | + |
| 34 | + |
| 35 | +class WandbLogger(LightningLoggerBase): |
| 36 | + """ |
| 37 | + Logger for W&B. |
| 38 | +
|
| 39 | + Args: |
| 40 | + name (str): display name for the run. |
| 41 | + save_dir (str): path where data is saved. |
| 42 | + offline (bool): run offline (data can be streamed later to wandb servers). |
| 43 | + id or version (str): sets the version, mainly used to resume a previous run. |
| 44 | + anonymous (bool): enables or explicitly disables anonymous logging. |
| 45 | + project (str): the name of the project to which this run will belong. |
| 46 | + tags (list of str): tags associated with this run. |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False, |
| 50 | + version=None, project=None, tags=None): |
| 51 | + super().__init__() |
| 52 | + self._name = name |
| 53 | + self._save_dir = save_dir |
| 54 | + self._anonymous = "allow" if anonymous else None |
| 55 | + self._id = version or id |
| 56 | + self._tags = tags |
| 57 | + self._project = project |
| 58 | + self._experiment = None |
| 59 | + self._offline = offline |
| 60 | + |
| 61 | + def __getstate__(self): |
| 62 | + state = self.__dict__.copy() |
| 63 | + # cannot be pickled |
| 64 | + state['_experiment'] = None |
| 65 | + # args needed to reload correct experiment |
| 66 | + state['_id'] = self.experiment.id |
| 67 | + return state |
| 68 | + |
| 69 | + @property |
| 70 | + def experiment(self): |
| 71 | + if self._experiment is None: |
| 72 | + if self._offline: |
| 73 | + os.environ["WANDB_MODE"] = "dryrun" |
| 74 | + self._experiment = wandb.init( |
| 75 | + name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous, |
| 76 | + id=self._id, resume="allow", tags=self._tags) |
| 77 | + return self._experiment |
| 78 | + |
| 79 | + def watch(self, model, log="gradients", log_freq=100): |
| 80 | + wandb.watch(model, log, log_freq) |
| 81 | + |
| 82 | + @rank_zero_only |
| 83 | + def log_hyperparams(self, params): |
| 84 | + self.experiment.config.update(params) |
| 85 | + |
| 86 | + @rank_zero_only |
| 87 | + def log_metrics(self, metrics, step=None): |
| 88 | + metrics["global_step"] = step |
| 89 | + self.experiment.history.add(metrics) |
| 90 | + |
| 91 | + def save(self): |
| 92 | + pass |
| 93 | + |
| 94 | + @rank_zero_only |
| 95 | + def finalize(self, status='success'): |
| 96 | + try: |
| 97 | + exit_code = 0 if status == 'success' else 1 |
| 98 | + wandb.join(exit_code) |
| 99 | + except TypeError: |
| 100 | + wandb.join() |
| 101 | + |
| 102 | + @property |
| 103 | + def name(self): |
| 104 | + return self.experiment.project_name() |
| 105 | + |
| 106 | + @property |
| 107 | + def version(self): |
| 108 | + return self.experiment.id |
0 commit comments