将模型管理升级到 SDK v2

重要

本文引用Azure Machine Learning SDK v1。 SDK v1 自 2025 年 3 月 31 日起弃用。 对它的支持将于 2026 年 6 月 30 日结束。 使用 SDK v1 的现有工作流在支持终止日期后继续运行,但可能会面临安全风险或中断性变更。 在 2026 年 6 月 30 日之前过渡到 SDK v2。 有关详细信息,请参阅 什么是 Azure Machine Learning CLI 和 Python SDK v2?

本文对 SDK v1 和 SDK v2 中的方案进行了比较。

创建模型

  • SDK v1

    from azureml.core.model import Model
    
    # Register model
    model = Model.register(ws, model_name="local-file-example", model_path="mlflow-model/model.pkl")
    
  • SDK v2

    from azure.ai.ml.entities import Model
    from azure.ai.ml.constants import AssetTypes
    
    file_model = Model(
        path="mlflow-model/model.pkl",
        type=AssetTypes.CUSTOM_MODEL,
        name="local-file-example",
        description="Model created from local file.",
        stage="Development"  # Optional lifecycle stage: Development, Production, or Archived
    )
    ml_client.models.create_or_update(file_model)
    

在试验或作业中使用模型

  • SDK v1

    model = run.register_model(model_name='run-model-example',
                               model_path='outputs/model/')
    print(model.name, model.id, model.version, sep='\t')
    
  • SDK v2

    from azure.ai.ml.entities import Model
    from azure.ai.ml.constants import AssetTypes
    
    run_model = Model(
        path="azureml://jobs/$RUN_ID/outputs/artifacts/paths/model/",
        name="run-model-example",
        description="Model created from run.",
        type=AssetTypes.CUSTOM_MODEL
    )
    
    ml_client.models.create_or_update(run_model)
    

有关模型对详细信息,请参阅在 Azure 机器学习中使用模型

SDK v1 和 SDK v2 中关键功能的映射

SDK v1 中的功能 SDK v2 中的粗略映射
Model.register ml_client.models.create_or_update
run.register_model ml_client.models.create_or_update
Model.deploy ml_client.begin_create_or_update(blue_deployment)

后续步骤

有关详细信息,请参阅以下文档: