

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立模型
<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 SageMaker AI 主控台](https://console.aws.amazon.com/sagemaker/home)建立模型。

## 若要建立模型 (使用模型註冊表)
<a name="serverless-endpoints-create-model-registry"></a>

[模型註冊表](model-registry.md)是 SageMaker AI 的一項功能，可協助您編目和管理模型的版本，以便在機器學習 (ML) 管道中使用。若要將模型註冊表與無伺服器推論搭配使用，您必須先在模型註冊表的模型群組中，註冊模型版本。若要了解如何在模型註冊表中註冊模型，請遵循[建立模型群組](model-registry-model-group.md)和[註冊模型版本](model-registry-version.md)中的程序。

下列範例會要求您擁有已註冊模型版本的 ARN，並使用[適用於 Python 的AWS SDK (Boto3)](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) 來呼叫[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API。對於無伺服器推論，模型登錄檔目前僅支援適用於 Python 的 AWS SDK (Boto3)。例如，指定下列值：
+ 在 `model_name` 中，輸入模型的名稱。
+ 在 `sagemaker_role` 中，您可以透過本節步驟 4 中的預設 SageMaker AI 建立角色，或 [完成先決條件](serverless-endpoints-prerequisites.md) 區段的自訂 SageMaker AI IAM 角色。
+ 在`ModelPackageName`中，為您的模型版本指定 ARN，該 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 SDK (Boto3)](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) 來呼叫 [CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API。指定下列值：
+ 在 `sagemaker_role,` 中，您可以透過本節步驟 4 中的預設 SageMaker AI 建立角色，或 [完成先決條件](serverless-endpoints-prerequisites.md) 區段的自訂 SageMaker AI IAM 角色。
+ 在 `model_url` 中，為您的模型指定 Amazon S3 URI。
+ 在 `container` 中，擷取您要透過其 Amazon ECR 路徑使用的容器。此範例使用一個 SageMaker AI 提供的 XGBoost 容器。如果您尚未選取 SageMaker 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 SageMaker AI 主控台](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 路徑。映像檔必須是 SageMaker AI 提供的第一方映像檔 (例如 TensorFlow、XGBoost)，或是位於您建立端點的相同帳戶內 Amazon ECR 儲存庫中的映像。如果您沒有容器，請傳回[完成先決條件](serverless-endpoints-prerequisites.md)本節的步驟 6 以取得更多資訊。

1. 在**模型成品的位置**中，請將 Amazon S3 URI 輸入到您的機器學習 (ML) 模型。例如 `s3://amzn-s3-demo-bucket/models/model.tar.gz`。

1. (選用) 在**標籤**中，新增鍵值對以建立裝置的中繼資料。

1. 選擇**建立模型**。