2019-11-28 17:48:55 +00:00
|
|
|
"""
|
|
|
|
Log using `mlflow <https://mlflow.org>'_
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pytorch_lightning.logging import MLFlowLogger
|
|
|
|
mlf_logger = MLFlowLogger(
|
|
|
|
experiment_name="default",
|
|
|
|
tracking_uri="file:/."
|
|
|
|
)
|
|
|
|
trainer = Trainer(logger=mlf_logger)
|
|
|
|
|
|
|
|
|
|
|
|
Use the logger anywhere in you LightningModule as follows:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def train_step(...):
|
|
|
|
# example
|
|
|
|
self.logger.experiment.whatever_ml_flow_supports(...)
|
|
|
|
|
|
|
|
def any_lightning_module_function_or_hook(...):
|
|
|
|
self.logger.experiment.whatever_ml_flow_supports(...)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
from logging import getLogger
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
try:
|
|
|
|
import mlflow
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError('Missing mlflow package.')
|
|
|
|
|
|
|
|
from .base import LightningLoggerBase, rank_zero_only
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class MLFlowLogger(LightningLoggerBase):
|
|
|
|
def __init__(self, experiment_name, tracking_uri=None, tags=None):
|
2020-01-17 11:03:31 +00:00
|
|
|
r"""
|
|
|
|
|
|
|
|
Logs using MLFlow
|
|
|
|
|
|
|
|
Args:
|
|
|
|
experiment_name (str): The name of the experiment
|
|
|
|
tracking_uri (str): where this should track
|
|
|
|
tags (dict): todo this param
|
|
|
|
"""
|
2019-11-27 03:39:18 +00:00
|
|
|
super().__init__()
|
2019-12-08 00:25:12 +00:00
|
|
|
self._mlflow_client = mlflow.tracking.MlflowClient(tracking_uri)
|
2019-11-27 03:39:18 +00:00
|
|
|
self.experiment_name = experiment_name
|
|
|
|
self._run_id = None
|
|
|
|
self.tags = tags
|
|
|
|
|
2019-12-08 00:25:12 +00:00
|
|
|
@property
|
|
|
|
def experiment(self):
|
2020-01-17 11:03:31 +00:00
|
|
|
r"""
|
|
|
|
|
|
|
|
Actual mlflow object. To use mlflow features do the following.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
self.logger.experiment.some_mlflow_function()
|
|
|
|
|
|
|
|
"""
|
2019-12-08 00:25:12 +00:00
|
|
|
return self._mlflow_client
|
|
|
|
|
2019-11-27 03:39:18 +00:00
|
|
|
@property
|
|
|
|
def run_id(self):
|
|
|
|
if self._run_id is not None:
|
|
|
|
return self._run_id
|
|
|
|
|
2019-12-08 00:25:12 +00:00
|
|
|
expt = self._mlflow_client.get_experiment_by_name(self.experiment_name)
|
|
|
|
|
|
|
|
if expt:
|
|
|
|
self._expt_id = expt.experiment_id
|
|
|
|
else:
|
2020-01-14 03:11:04 +00:00
|
|
|
logger.warning(f"Experiment with name {self.experiment_name} not found. Creating it.")
|
2019-12-08 00:25:12 +00:00
|
|
|
self._expt_id = self._mlflow_client.create_experiment(name=self.experiment_name)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
2019-12-08 00:25:12 +00:00
|
|
|
run = self._mlflow_client.create_run(experiment_id=self._expt_id, tags=self.tags)
|
2019-11-27 03:39:18 +00:00
|
|
|
self._run_id = run.info.run_id
|
|
|
|
return self._run_id
|
|
|
|
|
|
|
|
@rank_zero_only
|
|
|
|
def log_hyperparams(self, params):
|
|
|
|
for k, v in vars(params).items():
|
|
|
|
self.experiment.log_param(self.run_id, k, v)
|
|
|
|
|
|
|
|
@rank_zero_only
|
2019-12-07 13:50:21 +00:00
|
|
|
def log_metrics(self, metrics, step=None):
|
2019-11-27 03:39:18 +00:00
|
|
|
timestamp_ms = int(time() * 1000)
|
|
|
|
for k, v in metrics.items():
|
|
|
|
if isinstance(v, str):
|
|
|
|
logger.warning(
|
|
|
|
f"Discarding metric with string value {k}={v}"
|
|
|
|
)
|
|
|
|
continue
|
2019-12-07 13:50:21 +00:00
|
|
|
self.experiment.log_metric(self.run_id, k, v, timestamp_ms, step)
|
2019-11-27 03:39:18 +00:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@rank_zero_only
|
|
|
|
def finalize(self, status="FINISHED"):
|
|
|
|
if status == 'success':
|
|
|
|
status = 'FINISHED'
|
|
|
|
self.experiment.set_terminated(self.run_id, status)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.experiment_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
return self._run_id
|