

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建模型
<a name="serverless-endpoints-create-model"></a>

要创建模型，您必须提供模型构件和容器映像的位置。您也可以使用 “模型[注册表” 中的SageMaker 模型](model-registry.md)版本。以下各节中的示例向您展示了如何使用 [CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html)API、模型注册表和 [Amazon A SageMaker I 控制台](https://console.aws.amazon.com/sagemaker/home)创建模型。

## 创建模型（使用模型注册表）
<a name="serverless-endpoints-create-model-registry"></a>

[模型注册表](model-registry.md)是 SageMaker AI 的一项功能，可帮助您对模型的版本进行编目和管理，以便在机器学习管道中使用。要将模型注册表与无服务器推理一起使用，必须先在模型注册表模型组中注册模型版本。要了解如何在模型注册表中注册模型，请按照[创建模型组](model-registry-model-group.md)和[注册模型版本](model-registry-version.md)中的步骤操作。

以下示例要求您拥有已注册模型版本的 ARN，并使用适用于 [Python 的AWS SDK (Boto3) 来调](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)用 API。[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html)对于无服务器推理，目前只有适用于 Python 的 AWS SDK (Boto3) 支持模型注册表。在示例中，请指定以下值：
+ 对于 `model_name`，输入模型的名称。
+ 对于`sagemaker_role`，您可以使用本[完成 先决条件](serverless-endpoints-prerequisites.md)节步骤 4 中的默认 A SageMaker SageMaker I 创建角色或自定义 AI IAM 角色。
+ 对于 `ModelPackageName`，指定模型版本的 ARN，该版本必须注册到模型注册表中的模型组。

```
#Setup
import boto3
import sagemaker
region = boto3.Session().region_name
client = boto3.client("sagemaker", region_name=region)

#Role to give SageMaker AI permission to access AWS services.
sagemaker_role = sagemaker.get_execution_role()

#Specify a name for the model
model_name = "<name-for-model>"

#Specify a Model Registry model version
container_list = [
    {
        "ModelPackageName": <model-version-arn>
     }
 ]

#Create the model
response = client.create_model(
    ModelName = model_name,
    ExecutionRoleArn = sagemaker_role,
    container_list
)
```

## 创建模型（使用 API）
<a name="serverless-endpoints-create-model-api"></a>

以下示例使用适用于 [Python 的AWS 软件开发工具包 (Boto3) 来](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)调用 API。[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html)指定以下值：
+ 因为`sagemaker_role,`您可以使用本[完成 先决条件](serverless-endpoints-prerequisites.md)节步骤 4 中的默认 A SageMaker SageMaker I 创建角色或自定义 AI IAM 角色。
+ 对于 `model_url`，指定模型的 Amazon S3 URI。
+ 对于 `container`，通过 Amazon ECR 路径检索要使用的容器。此示例使用 A SageMaker I 提供的 XGBoost 容器。如果您尚未选择 A SageMaker I 容器或自带 AI 容器，请参阅[完成 先决条件](serverless-endpoints-prerequisites.md)本节的第 6 步了解更多信息。
+ 对于 `model_name`，输入模型的名称。

```
#Setup
import boto3
import sagemaker
region = boto3.Session().region_name
client = boto3.client("sagemaker", region_name=region)

#Role to give SageMaker AI permission to access AWS services.
sagemaker_role = sagemaker.get_execution_role()

#Get model from S3
model_url = "s3://amzn-s3-demo-bucket/models/model.tar.gz"

#Get container image (prebuilt example)
from sagemaker import image_uris
container = image_uris.retrieve("xgboost", region, "0.90-1")

#Create model
model_name = "<name-for-model>"

response = client.create_model(
    ModelName = model_name,
    ExecutionRoleArn = sagemaker_role,
    Containers = [{
        "Image": container,
        "Mode": "SingleModel",
        "ModelDataUrl": model_url,
    }]
)
```

## 创建模型（使用控制台）
<a name="serverless-endpoints-create-model-console"></a>

1. 登录 [Amazon A SageMaker I 控制台](https://console.aws.amazon.com/sagemaker/home)。

1. 在导航选项卡中，选择**推理**。

1. 接下来，选择**模型**。

1. 选择**创建模型**。

1. 在**模型名称**中，输入您的账户所独有的模型名称，然后 AWS 区域。

1. 对于 **IAM 角色**，请选择您已经创建的 IAM 角色（请参阅[完成 先决条件](serverless-endpoints-prerequisites.md)），或者允许 SageMaker AI 为您创建一个。

1. 在**容器定义 1** 中，对于**容器输入选项**，选择**提供模型构件和输入位置**。

1. 对于**提供模型构件和推理映像选项**，选择**使用单个模型**。

1. 对于**推理代码映像的位置**，输入指向容器的 Amazon ECR 路径。该映像必须是 A SageMaker I 提供的第一方映像（例如 XGBoost） TensorFlow，或者是驻留在您创建终端节点时所在的 Amazon ECR 存储库中的映像。如果没有容器，请返回[完成 先决条件](serverless-endpoints-prerequisites.md)部分的步骤 6 以了解更多信息。

1. 对于**模型构件的位置**，输入 ML 模型的 Amazon S3 URI。例如 `s3://amzn-s3-demo-bucket/models/model.tar.gz`。

1. （可选）对于**标签**，添加键值对来为模型创建元数据。

1. 选择**创建模型**。