Azure 机器学习 - 使用Python SDK训练模型

简介: Azure 机器学习 - 使用Python SDK训练模型

了解如何用 SDK v1 将 Azure 计算资源附加到 Azure 机器学习工作区。 然后,可以将这些资源用作机器学习任务中的训练和推理计算目标


一、环境准备

  • Azure 机器学习工作区。 有关详细信息,请参阅创建工作区资源。
  • 机器学习服务的 Azure CLI 扩展、Azure 机器学习 Python SDK 或 Azure 机器学习 Visual Studio Code 扩展。

二、工作区限制

  • 请勿在工作区中为同一计算创建多个同步附件。 例如,使用两个不同的名称将一个 Azure Kubernetes 服务群集附加到工作区。 每个新附件都会破坏先前存在的附件。
    如果要重新附加计算目标来实现某个目的(例如,更改 TLS 或其他群集配置设置),则必须先删除现有附件。

三、什么是计算目标?

使用 Azure 机器学习,可在不同的资源或环境(统称为计算目标)中训练模型。 计算目标可以是本地计算机,也可以是云资源,例如 Azure 机器学习计算、Azure HDInsight 或远程虚拟机。 还可以使用计算目标进行模型部署,如部署模型的位置和方式中所述。


四、本地计算机

使用本地计算机进行训练时,无需创建计算目标。 只需从本地计算机提交训练运行。

使用本地计算机进行推理时,必须安装 Docker。 若要执行部署,请使用 LocalWebservice.deploy_configuration() 来定义 Web 服务将使用的端口。 然后使用通过 Azure 机器学习部署模型中所述的常规部署流程。


五、远程虚拟机

Azure 机器学习还支持连接 Azure 虚拟机。 VM 必须是 Azure Data Science Virtual Machine (DSVM)。 此 VM 提供精选的工具和框架用于满足整个机器学习开发生命周期的需求。 有关如何将 DSVM 与 Azure 机器学习配合使用的详细信息,请参阅配置开发环境。

  1. 创建:Azure 机器学习无法为你创建远程 VM。 你需要自行创建 VM,然后将其附加到 Azure 机器学习工作区。 要详细了解如何创建 DSVM,请参阅预配适用于 Linux (Ubuntu) 的 Data Science Virtual Machine。
    Azure 机器学习仅支持运行 Ubuntu 的虚拟机。 创建 VM 或选择现有 VM 时,必须选择使用 Ubuntu 的 VM。
    Azure 机器学习还要求虚拟机具有公共 IP 地址。
  2. 附加:若要附加现有虚拟机作为计算目标,必须提供虚拟机的资源 ID、用户名和密码。 可以使用订阅 ID、资源组名称和 VM 名称按以下字符串格式构造 VM 的资源 ID:/subscriptions//resourceGroups//providers/Microsoft.Compute/virtualMachines/
from azureml.core.compute import RemoteCompute, ComputeTarget
# Create the compute config 
compute_target_name = "attach-dsvm"
attach_config = RemoteCompute.attach_configuration(resource_id='<resource_id>',
                                                ssh_port=22,
                                                username='<username>',
                                                password="<password>")
# Attach the compute
compute = ComputeTarget.attach(ws, compute_target_name, attach_config)
compute.wait_for_completion(show_output=True)
  1. 或者,可以使用 Azure 机器学习工作室将 DSVM 附加到工作区。
    请勿在工作区中为同一 DSVM 创建多个同步附件。 每个新附件都会破坏先前存在的附件。
  2. 配置:为 DSVM 计算目标创建运行配置。 Docker 与 conda 用于在 DSVM 上创建和配置训练环境。
from azureml.core import ScriptRunConfig
from azureml.core.environment import Environment
from azureml.core.conda_dependencies import CondaDependencies
# Create environment
myenv = Environment(name="myenv")
# Specify the conda dependencies
myenv.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'])
# If no base image is explicitly specified the default CPU image "azureml.core.runconfig.DEFAULT_CPU_IMAGE" will be used
# To use GPU in DSVM, you should specify the default GPU base Docker image or another GPU-enabled image:
# myenv.docker.enabled = True
# myenv.docker.base_image = azureml.core.runconfig.DEFAULT_GPU_IMAGE
# Configure the run configuration with the Linux DSVM as the compute target and the environment defined above
src = ScriptRunConfig(source_directory=".", script="train.py", compute_target=compute, environment=myenv)

如果要从工作区中删除(拆离)VM,请使用 RemoteCompute.detach() 方法。

Azure 机器学习不会为你删除 VM。 必须使用 Azure 门户、CLI 或适用于 Azure VM 的 SDK 手动删除 VM。


六、Apache Spark 池

通过 Azure Synapse Analytics 与 Azure 机器学习的集成(预览版),你可以附加由 Azure Synapse 提供支持的 Apache Spark 池,以进行交互式数据探索和准备。 借助这种集成,你可使用专用计算大规模地进行数据整理。 有关详细信息,请参阅如何附加由 Azure Synapse Analytics 提供支持的 Apache Spark 池。


七、Azure HDInsight

Azure HDInsight 是用于大数据分析的热门平台。 该平台提供的 Apache Spark 可用于训练模型。

  1. 创建:Azure 机器学习无法为你创建 HDInsight 群集。 你需要自行创建群集,然后将其附加到 Azure 机器学习工作区。 有关详细信息,请参阅在 HDInsight 中创建 Spark 群集。

Azure 机器学习要求 HDInsight 群集具有公共 IP 地址。 创建群集时,必须指定 SSH 用户名和密码。 请记下这些值,因为在将 HDInsight 用作计算目标时需要用到这些值。 创建群集后,使用主机名 clustername-ssh.azurehdinsight.net 连接到该群集,其中,clustername是为该群集提供的名称。

  1. 附加:若要将 HDInsight 群集附加为计算目标,必须提供该 HDInsight 群集的资源 ID、用户名和密码。 可以使用订阅 ID、资源组名称和 HDInsight 群集名称按以下字符串格式构造 HDInsight 群集的资源 ID:/subscriptions//resourceGroups//providers/Microsoft.HDInsight/clusters/
from azureml.core.compute import ComputeTarget, HDInsightCompute
    from azureml.exceptions import ComputeTargetException
    try:
    # if you want to connect using SSH key instead of username/password you can provide parameters private_key_file and private_key_passphrase
    attach_config = HDInsightCompute.attach_configuration(resource_id='<resource_id>',
                                                          ssh_port=22, 
                                                          username='<ssh-username>', 
                                                          password='<ssh-pwd>')
    hdi_compute = ComputeTarget.attach(workspace=ws, 
                                       name='myhdi', 
                                       attach_configuration=attach_config)
    except ComputeTargetException as e:
    print("Caught = {}".format(e.message))
    hdi_compute.wait_for_completion(show_output=True)

或者,可以使用 Azure 机器学习工作室将 HDInsight 群集附加到工作区。

请勿在工作区中为同一 HDInsight 创建多个同步附件。 每个新附件都会破坏先前存在的附件。

  1. 配置:为 HDI 计算目标创建运行配置。
    ``` from azureml.core.runconfig import RunConfiguration from azureml.core.conda_dependencies import CondaDependencies
# use pyspark framework
run_hdi = RunConfiguration(framework="pyspark")
# Set compute target to the HDI cluster
run_hdi.target = hdi_compute.name
# specify CondaDependencies object to ask system installing numpy
cd = CondaDependencies()
cd.add_conda_package('numpy')
run_hdi.environment.python.conda_dependencies = cd
```

如果要从工作区中删除(拆离)HDInsight 群集,请使用 HDInsightCompute.detach() 方法。

Azure 机器学习不会为你删除 HDInsight 群集。 必须使用 Azure 门户、CLI 或适用于 Azure HDInsight 的 SDK 将其手动删除。


八、Azure Batch

Azure Batch 用于在云中高效运行大规模并行高性能计算 (HPC) 应用程序。 可以在 Azure 机器学习管道中使用 AzureBatchStep 将作业提交到 Azure Batch 计算机池。

若要将 Azure Batch 附加为计算目标,必须使用 Azure 机器学习 SDK 并提供以下信息:

  • Azure Batch 计算名称:在工作区中用于计算的易记名称
  • Azure Batch 帐户名称:Azure Batch 帐户的名称
  • 资源组:包含 Azure Batch 帐户的资源组。

以下代码演示如何将 Azure Batch 附加为计算目标:

from azureml.core.compute import ComputeTarget, BatchCompute
from azureml.exceptions import ComputeTargetException
# Name to associate with new compute in workspace
batch_compute_name = 'mybatchcompute'
# Batch account details needed to attach as compute to workspace
batch_account_name = "<batch_account_name>"  # Name of the Batch account
# Name of the resource group which contains this account
batch_resource_group = "<batch_resource_group>"
try:
    # check if the compute is already attached
    batch_compute = BatchCompute(ws, batch_compute_name)
except ComputeTargetException:
    print('Attaching Batch compute...')
    provisioning_config = BatchCompute.attach_configuration(
        resource_group=batch_resource_group, account_name=batch_account_name)
    batch_compute = ComputeTarget.attach(
        ws, batch_compute_name, provisioning_config)
    batch_compute.wait_for_completion()
    print("Provisioning state:{}".format(batch_compute.provisioning_state))
    print("Provisioning errors:{}".format(batch_compute.provisioning_errors))
print("Using Batch compute:{}".format(batch_compute.cluster_resource_id))

请勿在工作区中为同一 Azure Batch 创建多个同步附件。 每个新附件都会破坏先前存在的附件。

九、Azure Databricks

Azure Databricks 是 Azure 云中基于 Apache Spark 的环境。 它可以用作 Azure 机器学习管道的计算目标。

Azure 机器学习无法创建 Azure Databricks 计算目标。 而必须由你自行创建一个 Azure Databricks 工作区,然后将其附加到 Azure 机器学习工作区。 若要创建工作区资源,请参阅在 Azure Databricks 中运行 Spark 作业文档。

若要从不同 Azure 订阅附加 Azure Databricks 工作区,你(你的 Microsoft Entra 帐户)必须被授予 Azure Databricks 工作区的“参与者”角色。 查看 Azure 门户中的访问权限。

要将 Azure Databricks 附加为计算目标,请提供以下信息:

  • Databricks 计算名称:要分配给此计算资源的名称。
  • Databricks 工作区名称:Azure Databricks 工作区的名称。
  • Databricks 访问令牌:用于对 Azure Databricks 进行身份验证的访问令牌。 若要生成访问令牌,请参阅身份验证文档。

以下代码演示如何使用 Azure 机器学习 SDK 将 Azure Databricks 附加为计算目标:

import os
from azureml.core.compute import ComputeTarget, DatabricksCompute
from azureml.exceptions import ComputeTargetException
databricks_compute_name = os.environ.get(
    "AML_DATABRICKS_COMPUTE_NAME", "<databricks_compute_name>")
databricks_workspace_name = os.environ.get(
    "AML_DATABRICKS_WORKSPACE", "<databricks_workspace_name>")
databricks_resource_group = os.environ.get(
    "AML_DATABRICKS_RESOURCE_GROUP", "<databricks_resource_group>")
databricks_access_token = os.environ.get(
    "AML_DATABRICKS_ACCESS_TOKEN", "<databricks_access_token>")
try:
    databricks_compute = ComputeTarget(
        workspace=ws, name=databricks_compute_name)
    print('Compute target already exists')
except ComputeTargetException:
    print('compute not found')
    print('databricks_compute_name {}'.format(databricks_compute_name))
    print('databricks_workspace_name {}'.format(databricks_workspace_name))
    print('databricks_access_token {}'.format(databricks_access_token))
    # Create attach config
    attach_config = DatabricksCompute.attach_configuration(resource_group=databricks_resource_group,
                                                           workspace_name=databricks_workspace_name,
                                                           access_token=databricks_access_token)
    databricks_compute = ComputeTarget.attach(
        ws,
        databricks_compute_name,
        attach_config
    )
    databricks_compute.wait_for_completion(True)

有关更详细的示例,请参阅 GitHub 上的 示例笔记本。

请勿在工作区中为同一 Azure Databricks 创建多个同步附件。 每个新附件都会破坏先前存在的附件。

十、Azure Data Lake Analytics

Azure Data Lake Analytics 是 Azure 云中的大数据分析平台。 它可以用作 Azure 机器学习管道的计算目标。

使用该平台之前,请先创建 Azure Data Lake Analytics 帐户。 若要创建此资源,请参阅 Azure Data Lake Analytics 入门文档。

若要将 Data Lake Analytics 附加为计算目标,必须使用 Azure 机器学习 SDK 并提供以下信息:

  • 计算名称:要分配给此计算资源的名称。
  • 资源组:包含 Data Lake Analytics 帐户的资源组。
  • 帐户名称:Data Lake Analytics 帐户名。

以下代码演示如何将 Data Lake Analytics 附加为计算目标:

import os
from azureml.core.compute import ComputeTarget, AdlaCompute
from azureml.exceptions import ComputeTargetException
adla_compute_name = os.environ.get(
    "AML_ADLA_COMPUTE_NAME", "<adla_compute_name>")
adla_resource_group = os.environ.get(
    "AML_ADLA_RESOURCE_GROUP", "<adla_resource_group>")
adla_account_name = os.environ.get(
    "AML_ADLA_ACCOUNT_NAME", "<adla_account_name>")
try:
    adla_compute = ComputeTarget(workspace=ws, name=adla_compute_name)
    print('Compute target already exists')
except ComputeTargetException:
    print('compute not found')
    print('adla_compute_name {}'.format(adla_compute_name))
    print('adla_resource_id {}'.format(adla_resource_group))
    print('adla_account_name {}'.format(adla_account_name))
    # create attach config
    attach_config = AdlaCompute.attach_configuration(resource_group=adla_resource_group,
                                                     account_name=adla_account_name)
    # Attach ADLA
    adla_compute = ComputeTarget.attach(
        ws,
        adla_compute_name,
        attach_config
    )
    adla_compute.wait_for_completion(True)

有关更详细的示例,请参阅 GitHub 上的 示例笔记本。

请勿在工作区中为同一 ADLA 创建多个同步附件。 每个新附件都会破坏先前存在的附件。

Azure 机器学习管道只能处理 Data Lake Analytics 帐户的默认数据存储中存储的数据。 如果需要处理的数据不在默认存储中,可以在训练之前使用 DataTransferStep复制数据。


十一、Azure 容器实例

Azure 容器实例 (ACI) 是在部署模型时动态创建的。 不能以任何其他方式创建 ACI 和将其附加到工作区。 有关详细信息,请参阅将模型部署到 Azure 容器实例。

十二、Kubernetes

Azure 机器学习提供了用于为训练和推理附加你自己的 Kubernetes 群集的选项。 请参阅配置 Kubernetes 群集以进行 Azure 机器学习。

若要从工作区分离 Kubernetes 群集,请使用以下方法:

compute_target.detach()
目录
相关文章
|
1天前
|
机器学习/深度学习 数据挖掘 定位技术
多元线性回归:机器学习中的经典模型探讨
多元线性回归是统计学和机器学习中广泛应用的回归分析方法,通过分析多个自变量与因变量之间的关系,帮助理解和预测数据行为。本文深入探讨其理论背景、数学原理、模型构建及实际应用,涵盖房价预测、销售预测和医疗研究等领域。文章还讨论了多重共线性、过拟合等挑战,并展望了未来发展方向,如模型压缩与高效推理、跨模态学习和自监督学习。通过理解这些内容,读者可以更好地运用多元线性回归解决实际问题。
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
1月前
如何看PAI产品下训练(train)模型任务的费用细节
PAI产品下训练(train)模型任务的费用细节
85 6
|
1月前
|
机器学习/深度学习 安全 PyTorch
FastAPI + ONNX 部署机器学习模型最佳实践
本文介绍了如何结合FastAPI和ONNX实现机器学习模型的高效部署。面对模型兼容性、性能瓶颈、服务稳定性和安全性等挑战,FastAPI与ONNX提供了高性能、易于开发维护、跨框架支持和活跃社区的优势。通过将模型转换为ONNX格式、构建FastAPI应用、进行性能优化及考虑安全性,可以简化部署流程,提升推理性能,确保服务的可靠性与安全性。最后,以手写数字识别模型为例,展示了完整的部署过程,帮助读者更好地理解和应用这些技术。
97 20
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
云上一键部署 DeepSeek-V3 模型,阿里云 PAI-Model Gallery 最佳实践
本文介绍了如何在阿里云 PAI 平台上一键部署 DeepSeek-V3 模型,通过这一过程,用户能够轻松地利用 DeepSeek-V3 模型进行实时交互和 API 推理,从而加速 AI 应用的开发和部署。
|
1月前
|
机器学习/深度学习 存储 设计模式
特征时序化建模:基于特征缓慢变化维度历史追踪的机器学习模型性能优化方法
本文探讨了数据基础设施设计中常见的一个问题:数据仓库或数据湖仓中的表格缺乏构建高性能机器学习模型所需的历史记录,导致模型性能受限。为解决这一问题,文章介绍了缓慢变化维度(SCD)技术,特别是Type II类型的应用。通过SCD,可以有效追踪维度表的历史变更,确保模型训练数据包含完整的时序信息,从而提升预测准确性。文章还从数据工程师、数据科学家和产品经理的不同视角提供了实施建议,强调历史数据追踪对提升模型性能和业务洞察的重要性,并建议采用渐进式策略逐步引入SCD设计模式。
103 8
特征时序化建模:基于特征缓慢变化维度历史追踪的机器学习模型性能优化方法
|
1月前
|
机器学习/深度学习 人工智能 算法
机器学习算法的优化与改进:提升模型性能的策略与方法
机器学习算法的优化与改进:提升模型性能的策略与方法
276 13
机器学习算法的优化与改进:提升模型性能的策略与方法
|
23天前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
2月前
|
Python
[oeasy]python055_python编程_容易出现的问题_函数名的重新赋值_print_int
本文介绍了Python编程中容易出现的问题,特别是函数名、类名和模块名的重新赋值。通过具体示例展示了将内建函数(如`print`、`int`、`max`)或模块名(如`os`)重新赋值为其他类型后,会导致原有功能失效。例如,将`print`赋值为整数后,无法再用其输出内容;将`int`赋值为整数后,无法再进行类型转换。重新赋值后,这些名称失去了原有的功能,可能导致程序错误。总结指出,已有的函数名、类名和模块名不适合覆盖赋新值,否则会失去原有功能。如果需要使用类似的变量名,建议采用其他命名方式以避免冲突。
52 14
|
2月前
|
分布式计算 大数据 数据处理
技术评测:MaxCompute MaxFrame——阿里云自研分布式计算框架的Python编程接口
随着大数据和人工智能技术的发展,数据处理的需求日益增长。阿里云推出的MaxCompute MaxFrame(简称“MaxFrame”)是一个专为Python开发者设计的分布式计算框架,它不仅支持Python编程接口,还能直接利用MaxCompute的云原生大数据计算资源和服务。本文将通过一系列最佳实践测评,探讨MaxFrame在分布式Pandas处理以及大语言模型数据处理场景中的表现,并分析其在实际工作中的应用潜力。
116 2

热门文章

最新文章

推荐镜像

更多