

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

# 使用網域適應性微調大型語言模型 (LLM)
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation"></a>

網域適應性微調可讓您利用預先訓練的基礎模型，並使用有限的網域特定資料為特定任務進行調整。如果提示詞工程無法提供足夠的自訂功能，您可以使用網域調整微調，讓您的模型使用領域特定語言，例如產業術語、技術用語或其他專業資料。此微調程序會改變模型的權重。

若要在網域特定的資料集上微調模型：

1. 準備您的訓練資料。如需說明，請參閱[準備和上傳訓練資料以進行域自適應微調](#jumpstart-foundation-models-fine-tuning-domain-adaptation-prepare-data)。

1. 建立您的微調訓練任務。如需說明，請參閱[為指令微調建立訓練任務](#jumpstart-foundation-models-fine-tuning-domain-adaptation-train)。

您可以在 [範例筆記本](#jumpstart-foundation-models-fine-tuning-domain-adaptation-examples) 中找到端對端範例。

網域適應性微調適用於下列基礎模型：

**注意**  
某些 JumpStart 基礎模型 (例如 Llama 2 7B) 需要先接受終端使用者授權協議，才能微調和執行推論。如需詳細資訊，請參閱[終端使用者授權協議](jumpstart-foundation-models-choose.md#jumpstart-foundation-models-choose-eula)。
+ Bloom 3B
+ Bloom 7B1
+ BloomZ 3B FP16
+ BloomZ 7B1 FP16
+ GPT-2 XL
+ GPT-J 6B
+ GPT-Neo 1.3B
+ GPT-Neo 125M
+ GPT-NEO 2.7B
+ Llama 2 13B
+ Llama 2 13B Chat
+ Llama 2 13B Neuron
+ Llama 2 70B
+ Llama 2 70B Chat
+ Llama 2 7B
+ Llama 2 7B Chat
+ Llama 2 7B Neuron

## 準備和上傳訓練資料以進行域自適應微調
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation-prepare-data"></a>

域自適應微調的訓練資料可以 CSV、JSON 或 TXT 檔案格式提供。所有訓練資料都必須位於單一資料夾內的單一檔案中。

訓練資料取自 CSV 或 JSON 訓練資料檔案的**文字**欄。如果沒有任何欄的標籤為**文字**，則會從 CSV 或 JSON 訓練資料檔案的第一欄取得訓練資料。

以下是用於微調的 TXT 檔案內文範例：

```
This report includes estimates, projections, statements relating to our
business plans, objectives, and expected operating results that are “forward-
looking statements” within the meaning of the Private Securities Litigation
Reform Act of 1995, Section 27A of the Securities Act of 1933, and Section 21E
of ....
```

### 分割用於訓練和測試的資料
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation-split-data"></a>

您可以選擇性地提供另一個包含驗證資料的資料夾。此資料夾也應該包含一個 CSV、JSON 或 TXT 檔案。如果沒有提供驗證資料集，則會保留某個數量的訓練資料供驗證之用。當您選擇超參數來微調模型時，可以調整用於驗證的訓練資料百分比。

### 將微調資料上傳至 Amazon S3
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation-upload-data"></a>

將準備好的資料上傳至 Amazon Simple Storage Service (Amazon S3)，以便在微調 JumpStart 基礎模型時使用。您可以使用下列命令上傳資料：

```
from sagemaker.s3 import S3Uploader
import sagemaker
import random

output_bucket = sagemaker.Session().default_bucket()
local_data_file = "train.txt"
train_data_location = f"s3://{output_bucket}/training_folder"
S3Uploader.upload(local_data_file, train_data_location)
S3Uploader.upload("template.json", train_data_location)
print(f"Training data: {train_data_location}")
```

## 為指令微調建立訓練任務
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation-train"></a>

將資料上傳至 Amazon S3 之後，您可以微調和部署 JumpStart 基礎模型。若要在 Studio 中微調模型，請參閱[在 Studio 中微調模型](jumpstart-foundation-models-use-studio-updated-fine-tune.md)。若要使用 SageMaker Python SDK 微調模型，請參閱[使用 `JumpStartEstimator` 類別微調公開可用的基礎模型](jumpstart-foundation-models-use-python-sdk-estimator-class.md)。

## 範例筆記本
<a name="jumpstart-foundation-models-fine-tuning-domain-adaptation-examples"></a>

如需域自適應微調的詳細資訊，請參閱下列範例筆記本：
+ [SageMaker JumpStart 基礎模型 - 微調網域特定資料集上的文字產生 GPT-J 6B 模型](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/domain-adaption-finetuning-gpt-j-6b.html)
+ [在 JumpStart 微調 LLaMA 2 模型](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/llama-2-finetuning.html)