

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

# 用於微調的基礎模型和超參數
<a name="jumpstart-foundation-models-fine-tuning"></a>

基礎模型耗費大量計算，並且在一個大型的無標籤語料庫上進行過訓練。微調預先訓練過的基礎模型是一種經濟實惠的方式，可以利用其廣泛的功能，又能在您自己的小型語料庫上自訂模型。微調是一種涉及進一步訓練的自訂方法，並且會改變模型的權重。

有以下需求時，微調可能對您有用：
+ 根據特定業務需求自訂您的模型
+ 您的模型可以成功使用網域特定的語言，例如行業術語、技術術語或其他專業詞彙
+ 針對特定任務增強效能
+ 應用程式中的準確、相對和上下文感知回應
+ 更以事實為基礎，毒性更低，更符合特定要求的反應

根據您的使用案例和選擇的基礎模型，您可以採取兩種主要方法進行微調。

1. 如果您有興趣在特定網域資料上微調模型，請參閱[使用網域適應性微調大型語言模型 (LLM)](jumpstart-foundation-models-fine-tuning-domain-adaptation.md)。

1. 如果您對使用提示詞和回應範例的指令式微調感興趣，請參閱[使用提示指示微調大型語言模型 (LLM)](jumpstart-foundation-models-fine-tuning-instruction-based.md)。

## 可用於微調的基礎模型
<a name="jumpstart-foundation-models-fine-tuning-models"></a>

您可以微調下列任何 JumpStart 基礎模型：
+ Bloom 3B
+ Bloom 7B1
+ BloomZ 3B FP16
+ BloomZ 7B1 FP16
+ Code Llama 13B
+ Code Llama 13B Python
+ Code Llama 34B
+ Code Llama 34B Python
+ Code Llama 70B
+ Code Llama 70B Python
+ Code Llama 7B
+ Code Llama 7B Python
+ CyberAgentLM2-7B-Chat (CALM2-7B-Chat)
+ Falcon 40B BF16
+ Falcon 40B Instruct BF16
+ Falcon 7B BF16
+ Falcon 7B Instruct BF16
+ Flan-T5 基本
+ Flan-T5 大
+ Flan-T5 小
+ Flan-T5 XL
+ Flan-T5 XXL
+ Gemma 2B
+ Gemma 2B Instruct
+ Gemma 7B
+ Gemma 7B Instruct
+ GPT-2 XL
+ GPT-J 6B
+ GPT-Neo 1.3B
+ GPT-Neo 125M
+ GPT-NEO 2.7B
+ LightGPT Instruct 6B
+ 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
+ Mistral 7B
+ Mixtral 8x7B
+ Mixtral 8x7B Instruct
+ RedPajama INCITE Base 3B V1
+ RedPajama INCITE Base 7B V1
+ RedPajama INCITE Chat 3B V1
+ RedPajama INCITE Chat 7B V1
+ RedPajama INCITE Instruct 3B V1
+ RedPajama INCITE Instruct 7B V1
+ Stable Diffusion 2.1

## 通常支援的微調超參數
<a name="jumpstart-foundation-models-fine-tuning-hyperparameters"></a>

微調時，不同的基礎模型支援不同的超參數。以下是常見的支援超參數，可在訓練期間進一步自訂您的模型：


| 推論參數 | Description | 
| --- | --- | 
| `epoch` | 模型在訓練期間通過微調資料集的次數。必須是大於 1 的整數。 | 
| `learning_rate` |  處理完每批微調訓練範例後，模型權重更新的速率。必須是大於 0 的正浮點數。 | 
| `instruction_tuned` |  是否指示訓練模型。必須是 `'True'` 或 `'False'`。 | 
| `per_device_train_batch_size` |  用於訓練的每個 GPU 核心或 CPU 其批次大小。必須為正整數。 | 
| `per_device_eval_batch_size` |  用於評估的每個 GPU 核心或 CPU 其批次大小。必須為正整數。 | 
| `max_train_samples` |  為了偵錯或加速訓練，請將訓練範例的數量截斷至此值。值 -1 表示模型使用所有的訓練範例。必須為正整數或 -1。 | 
| `max_val_samples` |  為了偵錯或加速訓練，請將驗證範例的數量截斷至此值。值 -1 表示模型使用所有的驗證範例。必須為正整數或 -1。 | 
| `max_input_length` |  記號化後的總輸入序列長度上限。超過此長度的序列將被截斷。如果為 -1，則 `max_input_length` 會設定為 1024 和記號器所定義 `model_max_length` 的最小值。如果設定為正值，則 `max_input_length` 會設定為所提供值和記號器所定義 `model_max_length` 的最小值。必須為正整數或 -1。 | 
| `validation_split_ratio` |  如果沒有驗證管道，則訓練-驗證比率會從訓練資料中分割出來。必須介於 0 到 1 之間。 | 
| `train_data_split_seed` |  如果驗證資料不存在，這會固定將輸入訓練資料隨機分割至模型所使用的訓練和驗證資料。必須是整數。 | 
| `preprocessing_num_workers` |  用於預先處理的程序數目。如果為 `None`，則主要程序會用於預先處理。 | 
| `lora_r` |  低秩適應 (LoRA) r 值，做為權重更新的擴展因素。必須為正整數。 | 
| `lora_alpha` |  低秩適應 (LoRA) alpha 值，做為權重更新的擴展因素。通常為 `lora_r` 大小的 2 到 4 倍。必須為正整數。 | 
| `lora_dropout` |  低秩適應 (LoRA) 層的捨棄值必須是介於 0 到 1 之間的正浮點數。 | 
| `int8_quantization` |  如果為 `True`，則模型會以 8 位元精確度載入以進行訓練。 | 
| `enable_fsdp` |  如果為 `True`，則訓練會使用完全碎片資料平行處理。 | 

您在 Studio 中微調模型時，可以指定超參數值。如需詳細資訊，請參閱[在 Studio 中微調模型](jumpstart-foundation-models-use-studio-updated-fine-tune.md)。

您在使用 SageMaker Python SDK 微調模型時，也可以覆寫預設超參數值。如需詳細資訊，請參閱[使用 `JumpStartEstimator` 類別微調公開可用的基礎模型](jumpstart-foundation-models-use-python-sdk-estimator-class.md)。

# 使用網域適應性微調大型語言模型 (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)

# 使用提示指示微調大型語言模型 (LLM)
<a name="jumpstart-foundation-models-fine-tuning-instruction-based"></a>

指令式微調使用標籤的範例來改善預先訓練的基礎模型在特定任務上的效能。帶有標籤的範例依指令被格式化為提示、回應和用詞。此微調程序會改變模型的權重。有關指令式微調的更多資訊，請參閱文獻[FLAN 簡介：更通用的指令微調語言模型](https://ai.googleblog.com/2021/10/introducing-flan-more-generalizable.html)和[擴展指令微調的語言模型。](https://arxiv.org/abs/2210.11416)

微調語言網路 (FLAN) 模型使用指令調整，讓模型更適合解決一般下游 NLP 任務。Amazon SageMaker JumpStart 在 FLAN 模型系列中提供了許多基礎模型。例如，FLAN-T5 模型會針對各種任務進行指令微調，以提升各種常見使用案例的零樣本表現。透過額外的資料和微調，指令式模型可以進一步適應訓練期間沒考慮到的更具體任務。

若要使用提示-回應對任務指示微調特定任務上的 LLM：

1. 在 JSON 檔案中準備您的指示。如需提示-回應對檔案所需格式和資料資料夾結構的詳細資訊，請參閱[準備和上傳訓練資料以進行指令微調](#jumpstart-foundation-models-fine-tuning-instruction-based-prepare-data)。

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

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

只有一部分的 JumpStart 基礎模型與指令微調相容。指令式網域適應性微調適用於下列基礎模型：

**注意**  
某些 JumpStart 基礎模型 (例如 Llama 2 7B) 需要先接受終端使用者授權協議，才能微調和執行推論。如需詳細資訊，請參閱[終端使用者授權協議](jumpstart-foundation-models-choose.md#jumpstart-foundation-models-choose-eula)。
+ Flan-T5 基本
+ Flan-T5 大
+ Flan-T5 小
+ Flan-T5 XL
+ Flan-T5 XXL
+ 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
+ Mistral 7B
+ RedPajama INCITE Base 3B V1
+ RedPajama INCITE Base 7B V1
+ RedPajama INCITE Chat 3B V1
+ RedPajama INCITE Chat 7B V1
+ RedPajama INCITE Instruct 3B V1
+ RedPajama INCITE Instruct 7B V1

## 準備和上傳訓練資料以進行指令微調
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-prepare-data"></a>

指令微調的訓練資料必須以 JSON Lines 文字檔案格式提供，其中每一行都是字典。所有訓練資料都必須位於單一資料夾中。資料夾可以包含多個 .jsonl 檔案。

訓練資料夾也可以包含範本 JSON 檔案 (`template.json`)，該檔案描述資料的輸入和輸出格式。如果未提供任何範本檔案，則會使用下列範本檔案：

```
{
  "prompt": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{context}",
  "completion": "{response}"
}
```

根據 `template.json` 檔案，訓練資料的每個 .jsonl 項目都必須包含 `{instruction}`、`{context}`和 `{response}` 欄位。

如果您提供自訂範本 JSON 檔案，請使用 `"prompt"` 和 `"completion"` 金鑰來定義您自己的必要欄位。根據下列自訂範本 JSON 檔案，訓練資料的每個 .jsonl 項目都必須包含 `{question}`、`{context}` 和 `{answer}` 欄位：

```
{
  "prompt": "question: {question} context: {context}",
  "completion": "{answer}"
}
```

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

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

### 將微調資料上傳至 Amazon S3
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-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.jsonl"
train_data_location = f"s3://{output_bucket}/dolly_dataset"
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-instruction-based-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-instruction-based-examples"></a>

如需指令微調的詳細資訊，請參閱下列範例筆記本：
+ [在 JumpStart 微調 LLaMA 2 模型](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/llama-2-finetuning.html)
+ [SageMaker JumpStart 簡介 - 使用 Mistral 模型產生文字](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/mistral-7b-instruction-domain-adaptation-finetuning.html)
+ [SageMaker JumpStart 簡介 - 使用 Falcon 模型產生文字](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/falcon-7b-instruction-domain-adaptation-finetuning.html)
+ [SageMaker JumpStart 基礎模型 - HuggingFace Text2Text 指令微調](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/instruction-fine-tuning-flan-t5.html)