如何在 AWS Sagemaker 中查找不同区域的 XGBoost 容器

How to find XGBoost containers for different regions in AWS Sagemaker

我正在尝试按照 AWS Sagemaker 教程使用 Jupyter 笔记本环境训练机器学习模型。

根据教程,我应该复制以下代码并运行它来导入所需的库并设置环境变量。

# import libraries
import boto3, re, sys, math, json, os, sagemaker, urllib.request
from sagemaker import get_execution_role
import numpy as np                                
import pandas as pd                               
import matplotlib.pyplot as plt                   
from IPython.display import Image                 
from IPython.display import display               
from time import gmtime, strftime                 
from sagemaker.predictor import csv_serializer   

# Define IAM role
role = get_execution_role()
prefix = 'sagemaker/DEMO-xgboost-dm'
containers = {'us-west-2': '433757028032.dkr.ecr.us-west-2.amazonaws.com/xgboost:latest',
              'us-east-1': '811284229777.dkr.ecr.us-east-1.amazonaws.com/xgboost:latest',
              'us-east-2': '825641698319.dkr.ecr.us-east-2.amazonaws.com/xgboost:latest',
              'eu-west-1': '685385470294.dkr.ecr.eu-west-1.amazonaws.com/xgboost:latest'} # each region has its XGBoost container
my_region = boto3.session.Session().region_name # set the region of the instance
print("Success - the MySageMakerInstance is in the " + my_region + " region. You will use the " + containers[my_region] + " container for your SageMaker endpoint.")

预期结果如下。

但是,我遇到了这个错误。

KeyError Traceback (most recent call last) in () 18 'eu-west-1': '685385470294.dkr.ecr.eu-west-1.amazonaws.com/xgboost:latest'} # each region has its XGBoost container 19 my_region = boto3.session.Session().region_name # set the region of the instance ---> 20 print("Success - the MySageMakerInstance is in the " + my_region + " region. You will use the " + containers[my_region] + " container for your SageMaker endpoint.")

KeyError: 'ap-northeast-2'

我认为这是因为我的地区是 "ap-northeast-2"。 我觉得我需要为我所在的地区更换容器。

如果我的猜测是正确的,我如何找到我所在地区的容器?
此外,我是否忽略了其他任何内容?

我希望你的理由是正确的。代码中没有您所在地区的条目。我不知道每个地区是否有这些容器的列表。也就是说,您可以在 ECR(弹性容器注册表)中找到它们。

请记住,您可以通过切换到受支持的区域之一来快速解决此问题。否则:

如果 AWS 在您所在的地区没有公开列出的容器,您可以自己在 AWS 中使用 ECR 注册容器。您需要使用 AWS CLI 和 docker 登录来登录 ECR。

您可以使用命令 aws ecr get-login --region ap-northeast-2 来获取 docker 登录所需的令牌。

然后,克隆此存储库:https://github.com/aws/sagemaker-xgboost-container

您可以在本地构建此映像并将其推送到 ECR。之后,登录到 AWS 控制台(或使用 AWS CLI)并找到图像的 ARN。它应该与代码中其他格式相匹配。

之后,只需在 containers 变量的代码中添加另一个 key/value 条目并使用 'ap-northeast-2': '<ARN of the docker image>'

在 XGBoost (0.72) 中找到您的训练图像和推理图像注册表路径 link: https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html

在你的情况下它将是 'ap-northeast-2'。将它与相关 ecr.It 添加到你的容器中应该可以正常工作。