

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

# 微調彙整中樞模型
<a name="jumpstart-curated-hubs-fine-tune"></a>

在私有彙整模型中樞中，您可以使用模型參考執行微調訓練任務。模型參考指向 SageMaker AI 公有中樞內公開可用的 JumpStart 模型，但您可以針對特定使用案例根據自己的資料微調模型。微調任務後，您便可以存取模型權重，然後就能使用權重或部署到端點。

使用 SageMaker Python SDK，您可以僅用數行程式碼來微調彙整中樞模型。如需微調公開可用 JumpStart 模型的更多一般資訊，請參閱[用於微調的基礎模型和超參數](jumpstart-foundation-models-fine-tuning.md)。

## 先決條件
<a name="jumpstart-curated-hubs-fine-tune-prereqs"></a>

為了在彙整中樞內微調 JumpStart 模型參考，請執行下列動作：

1. 請確定使用者的 IAM 角色已連接 SageMaker AI `TrainHubModel` 許可。如需詳細資訊，請參閱《AWS IAM 使用者指南》**中的[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

   您應將如下列範例的政策連接至您使用者的 IAM 角色。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "VisualEditor0",
               "Effect": "Allow",
               "Action": "sagemaker:TrainHubModel",
               "Resource": "arn:aws:sagemaker:*:111122223333:hub/*"
           }
       ]
   }
   ```

------
**注意**  
如果您的彙整中樞是跨帳戶共用，且中樞內容是由另一個帳戶所擁有，請確定您的 `HubContent` (模型參考資源) 具有資源型 IAM 政策，該政策也會將 `TrainHubModel` 許可授予請求帳戶，如下列範例所示。  

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "AllowCrossAccountSageMakerAccess",
               "Effect": "Allow",
               "Principal": {
                   "AWS": "arn:aws:iam::111122223333:root"
               },
               "Action": [
                   "sagemaker:TrainHubModel"
               ],
               "Resource": [
                   "arn:aws:sagemaker:*:111122223333:hub/*"
               ]
           }
       ]
   }
   ```

1. 擁有私有彙整中樞，其中包含所要微調 JumpStart 模型的模型參考。如需建立私有中樞的詳細資訊，請參閱[建立私有模型中樞](jumpstart-curated-hubs-admin-guide-create.md)。若要了解如何將公開可用的 JumpStart 模型新增至私有中樞，請參閱[將模型新增至私有中樞](jumpstart-curated-hubs-admin-guide-add-models.md)。
**注意**  
您選擇的 JumpStart 模型應該可以微調。您可以透過查看[具備預先訓練模型表的內建演算法](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html)來確認某個模型是否可微調。

1. 具有您想要用來微調模型的訓練資料集。資料集應該採用適合所要微調模型的訓練格式。

## 微調彙整中樞模型參考
<a name="jumpstart-curated-hubs-fine-tune-pysdk"></a>

下列程序說明如何使用 SageMaker Python SDK 在私有彙整中樞內微調模型參考。

1. 請確定您已安裝最新版本的 SageMaker Python SDK (至少 `2.242.0` 版)。如需詳細資訊，請參閱[使用 SageMaker Python SDK 的版本 2.x](https://sagemaker.readthedocs.io/en/stable/v2.html)。

   ```
   !pip install --upgrade sagemaker
   ```

1. 從 SageMaker Python SDK 匯入您需要的 適用於 Python (Boto3) 的 AWS SDK 和模組。

   ```
   import boto3
   from sagemaker.jumpstart.estimator import JumpStartEstimator
   from sagemaker.session import Session
   ```

1. 初始化 Boto3 工作階段、SageMaker AI 用戶端和 SageMaker Python SDK 工作階段。

   ```
   sagemaker_client = boto3.Session(region_name=<AWS-region>).client("sagemaker")
   sm_session = Session(sagemaker_client=sagemaker_client)
   ```

1. 建立 `JumpStartEstimator` 並提供 JumpStart 模型 ID、包含模型參考的中樞名稱，以及 SageMaker Python SDK 工作階段。如需模型 ID 的清單，請參閱[具備預先訓練模型表的內建演算法](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html)。

   您可以選擇性地在建立估算器時指定 `instance_type` 和 `instance_count` 欄位。如果您沒有這麼做，訓練任務會使用您正使用模型的預設執行個體類型和計數。

   您也可以選擇性地指定您要存放經微調的模型權重之 Amazon S3 位置的 `output_path`。如果您未指定 `output_path`，則會將預設 SageMaker AI Amazon S3 儲存貯體用於您帳戶中的區域，並採用以下格式命名：`sagemaker-<region>-<account-id>`。

   ```
   estimator = JumpStartEstimator(
       model_id="meta-textgeneration-llama-3-2-1b",
       hub_name=<your-hub-name>,
       sagemaker_session=sm_session, # If you don't specify an existing session, a default one is created for you
       # Optional: specify your desired instance type and count for the training job
       # instance_type = "ml.g5.2xlarge"
       # instance_count = 1
       # Optional: specify a custom S3 location to store the fine-tuned model artifacts
       # output_path: "s3://<output-path-for-model-artifacts>"
   )
   ```

1. 使用 `training` 索引鍵建立一個字典，您會在該索引鍵中指定微調資料集的位置。此範例指向 Amazon S3 URI。如果您有其他考量，例如使用本機模式或多個訓練資料通道，請參閱 SageMaker Python SDK 文件中的 [ JumpStartEstimator.fit()](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.jumpstart.estimator.JumpStartEstimator.fit) 以取得詳細資訊。

   ```
   training_input = {
       "training": "s3://<your-fine-tuning-dataset>"
   }
   ```

1. 呼叫估算器的 `fit()` 方法，傳入您的訓練資料並接受 EULA (若適用的話)。
**注意**  
下列範例會設定 `accept_eula=False.`，您應該手動將其值變更為 `True`，以接受 EULA。

   ```
   estimator.fit(inputs=training_input, accept_eula=False)
   ```

您的微調任務現在應該開始了。

您可以檢查您的微調任務，方法是在 SageMaker AI 主控台中或使用 [ListTrainingJobs](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ListTrainingJobs.html) API 檢視您的訓練任務。

您可以在 `JumpStartEstimator` 物件中指定的 Amazon S3 `output_path` 處 (區域的預設 SageMaker AI Amazon S3 儲存貯體，或您指定的自訂 Amazon S3 路徑，以適用者為主) 存取經微調的模型成品。