将本地运行升级到 SDK v2

V1 和 V2 中的本地运行类似。 在上述任一版本中设置计算目标时,请使用“local”字符串。

本文比较 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='local')
    
    # 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(DefaultAzureCredential())
    
    # set up pytorch environment
    env = Environment(
        image='mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04',
        conda_file='pytorch-env.yml',
        name='pytorch-env'
    )
    
    # define the command
    command_job = command(
        code='./src',
        command='train.py',
        environment=env,
        compute='local',
    )
    
    returned_job = ml_client.jobs.create_or_update(command_job)
    returned_job
    

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

SDK v1 中的功能 SDK v2 中的粗略映射
experiment.submit MLCLient.jobs.create_or_update

后续步骤