本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在私有策畫模型中樞中,您可以使用模型參考執行微調訓練任務。模型參考指向 SageMaker AI 公有中樞中公開可用的 JumpStart 模型,但您可以根據特定使用案例根據自己的資料微調模型。在微調任務之後,您可以存取模型權重,然後您可以使用這些權重或部署到端點。
您可以使用 SageMaker Python SDK,在幾行程式碼中微調策劃的中樞模型。如需調整公開 JumpStart 模型的更多一般資訊,請參閱 用於微調的基礎模型和超參數。
先決條件
若要微調您策畫中樞中的 JumpStart 模型參考,請執行下列動作:
-
請確定使用者的 IAM 角色已連接 SageMaker AI
TrainHubModel
許可。如需詳細資訊,請參閱《IAM 使用者指南》中的新增和移除 IAM 身分許可。 AWS您應該將類似下列範例的政策連接至使用者的 IAM 角色:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "sagemaker:TrainHubModel", "Resource": "arn:aws:sagemaker:*:
<account-id>
:hub/*" } ] }注意
如果您的策劃中樞在帳戶之間共用,且中樞內容由另一個帳戶擁有,請確定您的
HubContent
(模型參考資源) 具有資源型 IAM 政策,該政策也會將TrainHubModel
許可授予請求帳戶,如下列範例所示。{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCrossAccountSageMakerAccess", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::
<account-to-grant-access-to>
:root" }, "Action": [ "sagemaker:TrainHubModel" ], "Resource": [ "<hub-content-arn>
" ] } ] } -
擁有私有策展中樞,其中包含您要微調的 JumpStart 模型模型參考。如需建立私有中樞的詳細資訊,請參閱 建立私有模型中樞。若要了解如何將公開可用的 JumpStart 模型新增至您的私有中樞,請參閱 將模型新增至私有中樞。
注意
您選擇的 JumpStart 模型應可微調。您可以使用預先訓練的模型資料表檢查內建演算法,來驗證模型
是否可微調。 -
擁有您想要用來微調模型的訓練資料集。資料集應採用您想要微調的模型的適當訓練格式。
微調精選中樞模型參考
下列程序說明如何使用 SageMaker Python SDK,在私有策劃的中樞中微調模型參考。
-
請確定您已安裝最新版本 (至少
2.242.0
) 的 SageMaker Python SDK。如需詳細資訊,請參閱使用 SageMaker Python SDK 的 2.x 版。 !pip install --upgrade sagemaker
-
從 SageMaker Python SDK 匯入 AWS SDK for Python (Boto3) 和您需要的模組。
import boto3 from sagemaker.jumpstart.estimator import JumpStartEstimator from sagemaker.session import Session
-
初始化 Boto3 工作階段、SageMaker AI 用戶端和 SageMaker Python SDK 工作階段。
sagemaker_client = boto3.Session(region_name=
<AWS-region>
).client("sagemaker") sm_session = Session(sagemaker_client=sagemaker_client) -
建立
JumpStartEstimator
並提供 JumpStart 模型 ID、包含模型參考的中樞名稱,以及 SageMaker Python SDK 工作階段。如需模型 IDs 的清單,請參閱具有預先訓練模型的內建演算法資料表。 或者,您可以在建立估算器時指定
instance_type
和instance_count
欄位。如果沒有,訓練任務會使用預設執行個體類型,並計算您正在使用的模型數量。您也可以選擇性地將 指定
output_path
至要存放微調模型權重的 Amazon S3 位置。如果您未指定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>
" ) -
使用您指定微調資料集位置的索引
training
鍵來建立字典。此範例指向 Amazon S3 URI。如果您有其他考量,例如使用本機模式或多個訓練資料通道,請參閱 SageMaker Python SDK 文件中的 JumpStartEstimator.fit()以取得詳細資訊。 training_input = { "training": "s3://
<your-fine-tuning-dataset>
" } -
呼叫估算器的
fit()
方法,並傳入您的訓練資料和 EULA 接受 (如適用)。注意
下列範例集
accept_eula=False.
您應該手動將值變更為True
,以接受 EULA。estimator.fit(inputs=training_input, accept_eula=False)
您的微調任務現在應該會開始。
您可以在 SageMaker AI 主控台或使用 ListTrainingJobs API 檢視訓練任務,以檢查微調任務。
您可以在 JumpStartEstimator
物件中output_path
指定的 Amazon S3 (區域的預設 SageMaker AI Amazon S3 儲存貯體,或指定的自訂 Amazon S3 路徑,如適用) 存取微調後的模型成品。