数据源 mlflow-experiment 提供 Spark DataFrameReader API,用于将 MLflow 试验运行数据加载到 DataFrame 中。 Azure Databricks用户经常使用它来分析训练运行结果、比较各试验的指标,并在试验历史记录的基础上生成仪表板。 有关详细信息,请参阅 使用 MLflow 试验组织训练运行。
Prerequisites
读取 MLflow 试验运行数据需要 Databricks Runtime 6.0 ML 及更高版本。
用法
以下示例演示如何使用 Spark 数据帧 API 加载和筛选 MLflow 试验数据。
从笔记本试验加载数据
若要从当前笔记本的实验中加载数据,请调用 load(),不带任何参数。
Python
df = spark.read.format("mlflow-experiment").load()
display(df)
Scala
val df = spark.read.format("mlflow-experiment").load()
display(df)
使用试验 ID 加载数据
若要从一个或多个工作区试验加载数据,请将试验 ID 作为逗号分隔的字符串传递给 load()。
Python
df = spark.read.format("mlflow-experiment").load("3270527066281272")
display(df)
Scala
val df = spark.read.format("mlflow-experiment").load("3270527066281272,953590262154175")
display(df)
使用试验名称加载数据
若要按试验名称加载数据,请使用 MLflow 客户端将名称解析为 load()ID,然后将 ID 传递给 。
Python
expId = mlflow.get_experiment_by_name("/Shared/diabetes_experiment/").experiment_id
df = spark.read.format("mlflow-experiment").load(expId)
display(df)
Scala
val expId = mlflow.getExperimentByName("/Shared/diabetes_experiment/").get.getExperimentId
val df = spark.read.format("mlflow-experiment").load(expId)
display(df)
基于指标和参数筛选数据
加载试验数据后,使用标准 DataFrame 筛选器表达式跨指标和参数进行查询。
Python
df = spark.read.format("mlflow-experiment").load("3270527066281272")
filtered_df = df.filter("metrics.loss < 0.01 AND params.learning_rate > '0.001'")
display(filtered_df)
Scala
val df = spark.read.format("mlflow-experiment").load("3270527066281272")
val filtered_df = df.filter("metrics.loss < 1.85 AND params.num_epochs > '30'")
display(filtered_df)
输出架构
无论加载了哪个实验,mlflow-experiment 数据源返回的架构都是固定的:
root
|-- run_id: string
|-- experiment_id: string
|-- metrics: map
| |-- key: string
| |-- value: double
|-- params: map
| |-- key: string
| |-- value: string
|-- tags: map
| |-- key: string
| |-- value: string
|-- start_time: timestamp
|-- end_time: timestamp
|-- status: string
|-- artifact_uri: string
其他资源
-
使用 Apache Spark DataFrame 读取 OpenSharing 共享表:如果您的数据是通过 Delta Sharing 共享的,而不是存储在 MLflow 中,请使用
deltasharing格式,通过相同的 DataFrameReader API 读取共享表。