Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门。本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
XGBoost 算法
XGBoost (eXtreme Gradient Boosting) 是梯度提升树算法的一种流行且高效的开源实施。梯度提升是一种指导式学习算法,它尝试将一组较简单且较弱模型的一系列估计值结合在一起,从而准确地预测目标变量。XGBoost 算法在机器学习竞赛中表现良好,因为它可以强大地处理各种数据类型、关系、分布以及您可以微调的各种超参数。您可以使用 XGBoost 来处理回归、分类(二进制和多类)和排名问题。
您可以使用新版本的 XGBoost 算法作为 Amazon。 SageMaker 内置算法或作为框架以在本地环境中运行训练脚本。与原始版本相比,此实施占用内存更少,并且具有更好的日志记录、改进的超参数验证以及一组扩展的指标。它提供了一个 XGBoost estimator
,用于在托管的 XGBoost 环境中执行训练脚本。当前版本 SageMaker XGBoost 基于原始 XGBoost 版本 0.90、1.0、1.2 和 1.3。
支持的版本
xgBoost 0.90 版本将于 2021 年 12 月 31 日弃用。对 xgBoost 0.90 的安全更新或错误修复的支持将在此日期之后停止。强烈建议在弃用之前将 xgBoost 版本升级到其中一个较新版本(1.0-1 或更高版本)。
SageMaker Python SDK v1 支持 xgBoost 的最高版本 1.0-1。对于 xgBoost 的更高版本,请使用 SageMaker Python 开发工具包 v2。有关更多信息,请参阅来自的开发工具包 v1 到 SDK v2.
不支持 xgBoost 1.1 SageMaker 因为当测试输入的功能少于 LIBSVM 输入中的训练数据时,xgBoost 1.1 的运行预测功能不佳。该功能已在 xgBoost 1.2 中恢复。考虑使用 SageMaker xgBoost 1.2-2 或更高版本。
如何使用 SageMaker XGBoost
使用 SageMaker,您可以使用 XGBoost 作为内置算法或框架。通过将 XGBoost 用作框架,您可以自定义自己的训练脚本,从而具有更大的灵活性并可以访问更高级的方案(例如 k 折交叉验证)。
-
使用 XGBoost 作为框架
使用 XGBoost 作为框架来运行可以将附加数据处理合并到训练作业中的自定义训练脚本。在下面的代码示例中,您可以找到如何 SageMaker Python 开发工具包以它提供其他框架 API(如 TensorFlow、MXNet 和 PyTorch)的方式提供 XGBoost API 作为框架。
- SageMaker Python SDK v1
-
import boto3
import sagemaker
from sagemaker.xgboost.estimator import XGBoost
from sagemaker.session import s3_input, Session
# initialize hyperparameters
hyperparameters = {
"max_depth":"5",
"eta":"0.2",
"gamma":"4",
"min_child_weight":"6",
"subsample":"0.7",
"verbosity":"1",
"objective":"reg:linear",
"num_round":"50"}
# set an output path where the trained model will be saved
bucket = sagemaker.Session().default_bucket()
prefix = 'DEMO-xgboost-as-a-framework'
output_path = 's3://{}/{}/{}/output'.format(bucket, prefix, 'abalone-xgb-framework')
# construct a SageMaker XGBoost estimator
# specify the entry_point to your xgboost training script
estimator = XGBoost(entry_point = "your_xgboost_abalone_script.py
",
framework_version='1.0-1
',
hyperparameters=hyperparameters,
role=sagemaker.get_execution_role(),
train_instance_count=1,
train_instance_type='ml.m5.2xlarge',
output_path=output_path)
# define the data type and paths to the training and validation datasets
content_type = "libsvm"
train_input = s3_input("s3://{}/{}/{}/".format(bucket, prefix, 'train'), content_type=content_type)
validation_input = s3_input("s3://{}/{}/{}/".format(bucket, prefix, 'validation'), content_type=content_type)
# execute the XGBoost training job
estimator.fit({'train': train_input, 'validation': validation_input})
- SageMaker Python SDK v2
-
import boto3
import sagemaker
from sagemaker.xgboost.estimator import XGBoost
from sagemaker.session import Session
from sagemaker.inputs import TrainingInput
# initialize hyperparameters
hyperparameters = {
"max_depth":"5",
"eta":"0.2",
"gamma":"4",
"min_child_weight":"6",
"subsample":"0.7",
"verbosity":"1",
"objective":"reg:linear",
"num_round":"50"}
# set an output path where the trained model will be saved
bucket = sagemaker.Session().default_bucket()
prefix = 'DEMO-xgboost-as-a-framework'
output_path = 's3://{}/{}/{}/output'.format(bucket, prefix, 'abalone-xgb-framework')
# construct a SageMaker XGBoost estimator
# specify the entry_point to your xgboost training script
estimator = XGBoost(entry_point = "your_xgboost_abalone_script.py
",
framework_version='1.2-2
',
hyperparameters=hyperparameters,
role=sagemaker.get_execution_role(),
instance_count=1,
instance_type='ml.m5.2xlarge',
output_path=output_path)
# define the data type and paths to the training and validation datasets
content_type = "libsvm"
train_input = TrainingInput("s3://{}/{}/{}/".format(bucket, prefix, 'train'), content_type=content_type)
validation_input = TrainingInput("s3://{}/{}/{}/".format(bucket, prefix, 'validation'), content_type=content_type)
# execute the XGBoost training job
estimator.fit({'train': train_input, 'validation': validation_input})
对于 end-to-end 使用示例 SageMaker XGBoost 作为框架,请参阅Amazon 的回归 SageMaker XGBoost
-
使用 XGBoost 作为内置算法
使用 XGBoost 内置算法构建 XGBoost 训练容器,如以下代码示例所示。您可以使用 SageMaker image_uris.retrieve
API(或get_image_uri
如果使用 API亚马逊 SageMaker Python 开发工具包版本 1)。如果要确保 image_uris.retrieve
API 找到正确的 URI,请参阅内置算法的常用参数,并从内置算法映像 URI 和可用区域的完整列表中查找 xgboost
。
指定 XGBoost 映像 URI 后,您可以使用 XGBoost 容器构建估算程序。 SageMaker 评估程序 API 并启动训练作业。此 XGBoost 内置算法模式不包含您自己的 XGBoost 训练脚本,而是直接在输入数据集上运行。
- SageMaker Python SDK v1
-
import sagemaker
import boto3
from sagemaker.amazon.amazon_estimator import get_image_uri
from sagemaker.session import s3_input, Session
# initialize hyperparameters
hyperparameters = {
"max_depth":"5",
"eta":"0.2",
"gamma":"4",
"min_child_weight":"6",
"subsample":"0.7",
"objective":"reg:squarederror",
"num_round":"50"}
# set an output path where the trained model will be saved
bucket = sagemaker.Session().default_bucket()
prefix = 'DEMO-xgboost-as-a-built-in-algo'
output_path = 's3://{}/{}/{}/output'.format(bucket, prefix, 'abalone-xgb-built-in-algo')
# this line automatically looks for the XGBoost image URI and builds an XGBoost container.
# specify the repo_version depending on your preference.
xgboost_container = get_image_uri(boto3.Session().region_name,
'xgboost',
repo_version='1.0-1
')
# construct a SageMaker estimator that calls the xgboost-container
estimator = sagemaker.estimator.Estimator(image_name=xgboost_container,
hyperparameters=hyperparameters,
role=sagemaker.get_execution_role(),
train_instance_count=1,
train_instance_type='ml.m5.2xlarge',
train_volume_size=5, # 5 GB
output_path=output_path)
# define the data type and paths to the training and validation datasets
content_type = "libsvm"
train_input = s3_input("s3://{}/{}/{}/".format(bucket, prefix, 'train'), content_type=content_type)
validation_input = s3_input("s3://{}/{}/{}/".format(bucket, prefix, 'validation'), content_type=content_type)
# execute the XGBoost training job
estimator.fit({'train': train_input, 'validation': validation_input})
- SageMaker Python SDK v2
-
import sagemaker
import boto3
from sagemaker import image_uris
from sagemaker.session import Session
from sagemaker.inputs import TrainingInput
# initialize hyperparameters
hyperparameters = {
"max_depth":"5",
"eta":"0.2",
"gamma":"4",
"min_child_weight":"6",
"subsample":"0.7",
"objective":"reg:squarederror",
"num_round":"50"}
# set an output path where the trained model will be saved
bucket = sagemaker.Session().default_bucket()
prefix = 'DEMO-xgboost-as-a-built-in-algo'
output_path = 's3://{}/{}/{}/output'.format(bucket, prefix, 'abalone-xgb-built-in-algo')
# this line automatically looks for the XGBoost image URI and builds an XGBoost container.
# specify the repo_version depending on your preference.
xgboost_container = sagemaker.image_uris.retrieve("xgboost", region, "1.2-2
")
# construct a SageMaker estimator that calls the xgboost-container
estimator = sagemaker.estimator.Estimator(image_uri=xgboost_container,
hyperparameters=hyperparameters,
role=sagemaker.get_execution_role(),
instance_count=1,
instance_type='ml.m5.2xlarge',
volume_size=5, # 5 GB
output_path=output_path)
# define the data type and paths to the training and validation datasets
content_type = "libsvm"
train_input = TrainingInput("s3://{}/{}/{}/".format(bucket, prefix, 'train'), content_type=content_type)
validation_input = TrainingInput("s3://{}/{}/{}/".format(bucket, prefix, 'validation'), content_type=content_type)
# execute the XGBoost training job
estimator.fit({'train': train_input, 'validation': validation_input})
有关如何将 XGBoost 设置为内置算法的更多信息,请参阅以下笔记本示例。
梯度提升对表格数据进行操作,其中行表示观察、一个列表示目标变量或标签,其余列表示特征。
这些区域有: SageMaker 的 XGBoost 实现针对训练和推理支持 CSV 和 libsvm 格式:
对于 CSV 训练,算法假定目标变量在第一列中,而 CSV 没有标头记录。
对于 CSV 推理,算法假定 CSV 输入没有标签列。
对于 libsvm 训练,算法假定标签在第一列中。后续的列包含特征的索引值对(从零开始)。因此,每一行的格式为:<label> <index0>:<value0> <index1>:<value1> ... 对于 libsvm 的推理请求可能没有 libsvm 格式的标签。
这与其他不同 SageMaker 算法,它使用 protobuf 训练输入格式来保持与标准 XGBoost 数据格式更高的一致性。
对于 CSV 训练输入模式,可用于算法的总内存 (实例计数 * InstanceType
中的可用内存) 必须能够容纳训练数据集。对于 libsvm 训练输入模式,它不是必需的,但我们建议使用它。
SageMaker XGBoost 使用 Python pickle 模块来序列化/反序列化模型,这可用于保存/加载模型。
使用训练过的模型 SageMaker 开源 xgBoost 中的 xgBoost
使用实例权重支持区分标记数据点的重要性
-
SageMaker XGBoost 允许客户通过向各个实例分配权重值来区分标记数据点的重要性。对于 text/libsvm 输入,客户可以通过在标签之后附加权重值,将权重值分配给数据实例。例如,label:weight idx_0:val_0 idx_1:val_1...
。对于 text/csv 输入,客户需要在参数中打开 csv_weights
标志,并将权重值附加到标签之后的列中。例如:label,weight,val_0,val_1,...
)。
XGBoost 算法的 EC2 实例推荐
SageMaker XGBoost 1.0-1 或更低版本目前仅使用 CPU 进行训练。它是一种内存限制型(而不是计算限制型)算法。因此,通用计算实例(例如 M5)是比计算优化的实例(例如 C4)更适合的选择。此外,我们建议您在选定的实例中有足够的总内存来保存训练数据。尽管它支持使用磁盘空间来处理无法放入主内存的数据 ( out-of-core 在 libsvm 输入模式下可用的功能),在磁盘上写入缓存文件会减慢算法处理时间。
SageMaker XGBoost 1.2 或更高版本支持单实例 GPU 训练。尽管每实例成本较高,但 GPU 训练的速度更快,因此更经济高效。要利用 GPU 训练,请将实例类型指定为 GPU 实例之一(例如 P3),然后将tree_method
超参数gpu_hist
在你现有的 xgBoost 脚本中。 SageMaker 目前不支持多 GPU 训练。
XGBoost 示例笔记本
下表概述了解决亚马逊不同使用案例的各种样本笔记本 SageMaker XGBoost 算法。
有关如何创建和访问可用于在 SageMaker 中运行示例的 Jupyter 笔记本实例的说明,请参阅。使用 Amazon SageMaker 笔记本实例. 创建笔记本实例并打开该实例后,请选择SageMaker 示例选项卡以查看所有 SageMaker 示例。使用线性学习算法的主题建模示例笔记本位于 Amazon 算法简介部分中。要打开笔记本,请选择其 Use (使用) 选项卡,然后选择 Create copy (创建副本)。