将脚本运行升级到 SDK v2

在 SDK v2 中,“试验”和“运行”整合为作业。

作业有不同类型。 大多数作业都是运行 command 的命令作业,例如 python main.py。 作业中运行的内容与任何编程语言无关,因此你可以运行 bash 脚本、调用 python 解释器、运行一组 curl 命令或其他任何内容。

若要升级,请将提交作业的代码更改为 SDK v2。 无需将作业 运行的内容升级到 SDK v2。 但是,请从模型训练脚本中删除特定于Azure Machine Learning的任何代码。 这种分离便于更轻松地在本地和云之间进行转换,并且被认为是成熟 MLOps 的最佳做法。 实际上,这意味着删除 azureml.* 代码行。 将模型日志记录和跟踪代码替换为 MLflow。 有关详细信息,请参阅如何在 v2 中使用 MLflow

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

提交脚本运行

  • SDK v1

    from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig
    
    # connect to the workspace
    ws = Workspace.from_config()
    
    # define and configure the experiment
    experiment = Experiment(workspace=ws, name='day1-experiment-train')
    config = ScriptRunConfig(source_directory='./src',
                                script='train.py',
                                compute_target='cpu-cluster')
    
    # set up pytorch environment
    env = Environment.from_conda_specification(
        name='pytorch-env',
        file_path='pytorch-env.yml')
    config.run_config.environment = env
    
    run = experiment.submit(config)
    
    aml_url = run.get_portal_url()
    print(aml_url)
    
  • SDK v2

    #import required libraries
    from azure.ai.ml import MLClient, command
    from azure.ai.ml.entities import Environment
    from azure.identity import DefaultAzureCredential
    
    #connect to the workspace
    ml_client = MLClient.from_config(credential=DefaultAzureCredential())
    
    # set up pytorch environment
    env = Environment(
        image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu22.04",
        conda_file="pytorch-env.yml",
        name="pytorch-env"
    )
    
    # define the command
    command_job = command(
        code="./src",
        command="python train.py",
        environment=env,
        compute="cpu-cluster",
    )
    
    returned_job = ml_client.jobs.create_or_update(command_job)
    print(f"Job submitted. View in studio: {returned_job.studio_url}")
    

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

SDK v1 中的功能 SDK v2 中的粗略映射
experiment.submit MLClient.jobs.create_or_update
ScriptRunConfig() command()

后续步骤

有关详细信息,请参见:

  • V1 - 试验
  • 使用 Azure Machine Learning Python SDK v2