Azure Machine Learning SDK v2 for Python。用于ML工作区、作业、模型、数据集、计算和管道。
下载地址:agentic-awesome-skills/skills/azure-ai-ml-py at main · sickn33/agentic-awesome-skills · GitHub
技能概述
azure-ai-ml-py 技能是管理Azure ML资源的Python客户端库。该SDK提供了现代化的API来管理工作区、作业、模型、数据和计算资源。支持构建端到端的机器学习工作流程,从数据准备到模型训练、注册和部署。
安装
pip install azure-ai-ml
环境变量
AZURE_SUBSCRIPTION_ID= AZURE_RESOURCE_GROUP= AZURE_ML_WORKSPACE_NAME=
认证
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredentialml_client = MLClient(
credential=DefaultAzureCredential(),
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_ML_WORKSPACE_NAME"]
)
从配置文件认证
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential# Uses config.json in current directory or parent
ml_client = MLClient.from_config(
credential=DefaultAzureCredential()
)
工作区管理
创建工作区
from azure.ai.ml.entities import Workspace
ws = Workspace(
name="my-workspace",
location="eastus",
display_name="My Workspace",
description="ML workspace for experiments",
tags={"purpose": "demo"}
)ml_client.workspaces.begin_create(ws).result()
列出工作区
for ws in ml_client.workspaces.list():
print(f"{ws.name}: {ws.location}")
数据资产
注册数据
from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes# Register a file
my_data = Data(
name="my-dataset",
version="1",
path="azureml://datastores/workspaceblobstore/paths/data/train.csv",
type=AssetTypes.URI_FILE,
description="Training data"
)ml_client.data.create_or_update(my_data)
注册文件夹
my_data = Data(
name="my-folder-dataset",
version="1",
path="azureml://datastores/workspaceblobstore/paths/data/",
type=AssetTypes.URI_FOLDER
)ml_client.data.create_or_update(my_data)
模型注册表
注册模型
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypesmodel = Model(
name="my-model",
version="1",
path="./model/",
type=AssetTypes.CUSTOM_MODEL,
description="My trained model"
)ml_client.models.create_or_update(model)
列出模型
for model in ml_client.models.list(name="my-model"):
print(f"{model.name} v{model.version}")
计算资源
创建计算集群
from azure.ai.ml.entities import AmlCompute
cluster = AmlCompute(
name="cpu-cluster",
type="amlcompute",
size="Standard_DS3_v2",
min_instances=0,
max_instances=4,
idle_time_before_scale_down=120
)ml_client.compute.begin_create_or_update(cluster).result()
列出计算资源
for compute in ml_client.compute.list():
print(f"{compute.name}: {compute.type}")
作业
命令作业
from azure.ai.ml import command, Input
job = command(
code="./src",
command="python train.py --data ${{inputs.data}} --lr ${{inputs.learning_rate}}",
inputs={
"data": Input(type="uri_folder", path="azureml:my-dataset:1"),
"learning_rate": 0.01
},
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest",
compute="cpu-cluster",
display_name="training-job"
)returned_job = ml_client.jobs.create_or_update(job)
print(f"Job URL: {returned_job.studio_url}")
监控作业
ml_client.jobs.stream(returned_job.name)
管道
from azure.ai.ml import dsl, Input, Output
from azure.ai.ml.entities import Pipeline
@dsl.pipeline(
compute="cpu-cluster",
description="Training pipeline"
)
def training_pipeline(data_input):
prep_step = prep_component(data=data_input)
train_step = train_component(
data=prep_step.outputs.output_data,
learning_rate=0.01
)
return {"model": train_step.outputs.model}
pipeline = training_pipeline(
data_input=Input(type="uri_folder", path="azureml:my-dataset:1")
)pipeline_job = ml_client.jobs.create_or_update(pipeline)
环境
创建自定义环境
from azure.ai.ml.entities import Environment
env = Environment(
name="my-env",
version="1",
image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04",
conda_file="./environment.yml"
)ml_client.environments.create_or_update(env)
数据存储
列出数据存储
for ds in ml_client.datastores.list():
print(f"{ds.name}: {ds.type}")
获取默认数据存储
default_ds = ml_client.datastores.get_default()
print(f"Default: {default_ds.name}")
MLClient操作
| 属性 | 操作 |
|---|---|
workspaces | create, get, list, delete |
jobs | create_or_update, get, list, stream, cancel |
models | create_or_update, get, list, archive |
data | create_or_update, get, list |
compute | begin_create_or_update, get, list, delete |
environments | create_or_update, get, list |
datastores | create_or_update, get, list, get_default |
components | create_or_update, get, list |
最佳实践
- 使用版本控制- 为数据、模型和环境使用版本控制
- 配置空闲缩减- 设置空闲时间自动缩减以降低计算成本
- 使用环境- 为可重复训练创建可重用环境
- 流式传输作业日志- 监控训练进度
- 注册模型- 成功训练后注册模型
- 使用管道- 为多步骤工作流使用管道
- 标记资源- 为组织和成本跟踪标记资源
触发条件
- 需要在Python应用中管理Azure ML工作区
- 需要提交和管理训练作业
- 需要注册和管理ML模型
- 需要创建和管理计算集群
- 需要构建ML管道