---
title: mlflow
description: Log using MLflow.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.loggers.mlflow
---
# mlflow {#mlflow}

Classes

- [`MLFlowLogger`](#lightning.pytorch.loggers.mlflow.MLFlowLogger) — Log using MLflow.

## MLflow Logger {#mlflow-logger}

### `class lightning.pytorch.loggers.mlflow.MLFlowLogger(experiment_name='lightning_logs', run_name=None, tracking_uri=None, tags=None, save_dir='./mlruns', log_model=False, prefix='', artifact_location=None, run_id=None, synchronous=None)` {#lightning.pytorch.loggers.mlflow.MLFlowLogger}

Bases: [`Logger`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.loggers.logger.md#lightning.pytorch.loggers.logger.Logger "lightning.pytorch.loggers.logger.Logger")

Log using [MLflow](https://mlflow.org).

Install it with pip:

```bash
pip install mlflow  # or mlflow-skinny
```

```python
from lightning.pytorch import Trainer
from lightning.pytorch.loggers import MLFlowLogger

mlf_logger = MLFlowLogger(experiment_name="lightning_logs", tracking_uri="file:./ml-runs")
trainer = Trainer(logger=mlf_logger)
```

Use the logger anywhere in your [`LightningModule`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.LightningModule.md#lightning.pytorch.core.LightningModule "lightning.pytorch.core.LightningModule") as follows:

```python
from lightning.pytorch import LightningModule


class LitModel(LightningModule):
    def training_step(self, batch, batch_idx):
        # example
        self.logger.experiment.whatever_ml_flow_supports(...)

    def any_lightning_module_function_or_hook(self):
        self.logger.experiment.whatever_ml_flow_supports(...)
```

Parameters:

*   **experiment\_name** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – The name of the experiment.
    
*   **run\_name** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – Name of the new run. The run\_name is internally stored as a `mlflow.runName` tag. If the `mlflow.runName` tag has already been set in tags, the value is overridden by the run\_name.
    
*   **tracking\_uri** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – Address of local or remote tracking server. If not provided, defaults to MLFLOW\_TRACKING\_URI environment variable if set, otherwise it falls back to file:<save\_dir>.
    
*   **tags** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]\]) – A dictionary tags for the experiment.
    
*   **save\_dir** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – A path to a local directory where the MLflow runs get saved. Defaults to ./mlruns if tracking\_uri is not provided. Has no effect if tracking\_uri is provided.
    
*   **log\_model** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`True`, `False`, `'all'`\]) –
    
    Log checkpoints created by [`ModelCheckpoint`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.callbacks.ModelCheckpoint.md#lightning.pytorch.callbacks.ModelCheckpoint "lightning.pytorch.callbacks.model_checkpoint.ModelCheckpoint") as MLFlow artifacts.
    
    *   if `log_model == 'all'`, checkpoints are logged during training.
        
    *   if `log_model == True`, checkpoints are logged at the end of training, except when `save_top_k` `== -1` which also logs every checkpoint during training.
        
    *   if `log_model == False` (default), no checkpoint is logged.
        
    
*   **prefix** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – A string to put at the beginning of metric keys.
    
*   **artifact\_location** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – The location to store run artifacts. If not provided, the server picks an appropriate default.
    
*   **run\_id** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – The run identifier of the experiment. If not provided, a new run is started.
    
*   **synchronous** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")\]) – Hints mlflow whether to block the execution for every logging call until complete where applicable. Requires mlflow >= 2.8.0
    

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If required MLFlow package is not installed on the device.

#### `after_save_checkpoint(checkpoint_callback)` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.after_save_checkpoint}

Called after model checkpoint callback saves a new checkpoint.

Parameters:

**checkpoint\_callback** ([`ModelCheckpoint`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.callbacks.ModelCheckpoint.md#lightning.pytorch.callbacks.ModelCheckpoint "lightning.pytorch.callbacks.model_checkpoint.ModelCheckpoint")) – the model checkpoint callback instance

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `finalize(status='success')` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.finalize}

Do any processing that is necessary to finalize an experiment.

Parameters:

**status** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – Status that the experiment finished with (e.g. success, failed, aborted)

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `log_hyperparams(params)` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.log_hyperparams}

Record hyperparameters.

Parameters:

*   **params** (`Union`\[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\], [`Namespace`](https://docs.python.org/3/library/argparse.html#argparse.Namespace "(in Python v3.15)")\]) – [`Namespace`](https://docs.python.org/3/library/argparse.html#argparse.Namespace "(in Python v3.15)") or Dict containing the hyperparameters
    
*   **args** – Optional positional arguments, depends on the specific logger being used
    
*   **kwargs** – Optional keyword arguments, depends on the specific logger being used
    

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `log_metrics(metrics, step=None)` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.log_metrics}

Records metrics. This method logs metrics as soon as it received them.

Parameters:

*   **metrics** ([`Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – Dictionary with metric names as keys and measured quantities as values
    
*   **step** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\]) – Step number at which the metrics should be recorded
    

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `property experiment: mlflow.tracking.MlflowClient` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.experiment}

Actual MLflow object. To use MLflow features in your [`LightningModule`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.LightningModule.md#lightning.pytorch.core.LightningModule "lightning.pytorch.core.LightningModule") do the following.

Example:

```python
self.logger.experiment.some_mlflow_function()
```

#### `property experiment_id: Optional[str]` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.experiment_id}

Create the experiment if it does not exist to get the experiment id.

Returns:

The experiment id.

#### `property name: Optional[str]` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.name}

Get the experiment id.

Returns:

The experiment id.

#### `property run_id: Optional[str]` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.run_id}

Create the experiment if it does not exist to get the run id.

Returns:

The run id.

#### `property save_dir: Optional[str]` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.save_dir}

The root file directory in which MLflow experiments are saved.

Returns:

Local path to the root experiment directory if the tracking uri is local. Otherwise returns None.

#### `property version: Optional[str]` {#lightning.pytorch.loggers.mlflow.MLFlowLogger.version}

Get the run id.

Returns:

The run id.

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
