MLproject 文件有哪些字段?
查看完整题解
参考回答
直接回答
MLproject 是项目目录中的 YAML 文件。常见字段有 name、环境声明和 entry_points。每个入口下面通常有 parameters 与 command。
讲清原理
参数可以声明类型和默认值,命令按变量名插值。它本身只描述怎样运行,真正的训练逻辑仍在脚本。环境配置要使用当前运行器支持的写法。
例子与易错点
示例创建一份临时项目,用 MLflow SDK 运行自定义入口,让它取得整数默认值,再从运行参数确认该值已登记。
from pathlib import Path
from tempfile import TemporaryDirectory
import mlflow
from mlflow import MlflowClient
fixture = TemporaryDirectory()
root = Path(fixture.name)
mlflow.set_tracking_uri(f"sqlite:///{root / 'tracking.db'}")
client = MlflowClient()
experiment = client.create_experiment("demo", artifact_location=(root / "artifacts").as_uri())
(root / "MLproject").write_text(
"name: local_demo\n"
"entry_points:\n"
" score:\n"
" parameters:\n"
" size: {type: int, default: 3}\n"
" command: 'python score.py {size}'\n"
)
(root / "score.py").write_text(
"from pathlib import Path\n"
"import sys\n"
"Path(__file__).with_name('result.txt').write_text(str(int(sys.argv[1]) * 2))\n"
)
run = mlflow.projects.run(
str(root), entry_point="score", env_manager="local", experiment_id=experiment,
)
assert (root / "result.txt").read_text() == "6"
assert client.get_run(run.run_id).data.params["size"] == "3"
print("MLproject default size 3 produced 6")
这道题,你掌握得怎么样?
掌握程度由你主动标记,阅读和收藏不会自动改变状态。