如何使用MLflow加载已记录字典?遇AttributeError问题求助
在Databricks 10.4 ML Runtime中使用MLflow 1.24.0读取记录的字典时出现AttributeError问题
我已通过以下代码使用MLflow将pandas DataFrame以字典格式成功记录:
df = <pd.DataFrame> mlflow.log_dict(df.to_dict(orient='dict'), f"my_logged_df_dict.json")
尝试使用mlflow.artifacts.load_dict()读取该字典时,出现错误:
AttributeError: module 'mlflow' has no attribute 'artifacts'
确认MLflow版本为1.24.0,且该版本代码中确实存在mlflow.artifacts模块及load_dict方法,当前运行环境为预装该版本MLflow的Databricks 10.4 ML Runtime。
解决方案
- 显式导入mlflow.artifacts子模块
Databricks环境中可能需要手动导入子模块才能访问其方法,尝试先导入再调用:
import mlflow.artifacts loaded_dict = mlflow.artifacts.load_dict("my_logged_df_dict.json")
- 备选方案:通过MlflowClient读取JSON文件
如果上述方法无效,可以直接通过MLflow客户端下载并读取artifact文件:
import json from mlflow.tracking import MlflowClient # 替换为你的运行ID run_id = "your_run_id_here" client = MlflowClient() # 下载artifact到本地路径 artifact_local_path = client.download_artifacts(run_id, "my_logged_df_dict.json") # 读取JSON文件为字典 with open(artifact_local_path, "r") as f: loaded_dict = json.load(f)
- 验证并确认MLflow实际版本
有时Databricks Runtime标注的版本可能与实际导入的MLflow版本不一致,运行以下代码确认:
import mlflow print(mlflow.__version__)
如果版本低于1.24.0,可以通过以下命令升级(注意环境兼容性):
%pip install mlflow==1.24.0 --upgrade
内容的提问来源于stack exchange,提问作者lazarea
相关产品推荐
相关产品推荐

