

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# プロンプト指示を使用して大規模言語モデル (LLM) をファインチューニングする
<a name="jumpstart-foundation-models-fine-tuning-instruction-based"></a>

インストラクションベースの微調整では、ラベル付きの例を使用して、特定のタスクで事前トレーニング済み基盤モデルのパフォーマンスを向上させます。ラベル付きの例は、プロンプトとレスポンスのペアとしてフォーマットされ、インストラクションとしてフレーズ化されます。この微調整プロセスにより、モデルの重みが変わります。インストラクションベースの微調整についての詳細は、「[Introducing FLAN: More generalizable Language Models with Instruction Fine-Tuning](https://ai.googleblog.com/2021/10/introducing-flan-more-generalizable.html)」および「[Scaling Instruction-Finetuned Language Models](https://arxiv.org/abs/2210.11416)」という論文を参照してください。

FLAN-Tuned Language Net (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 基盤モデルのうち、インストラクションベースのファインチューニングに対応しているのは一部です。インストラクションベースの微調整は、以下の基盤モデルで使用可能です。

**注記**  
Llama 2 7B などの一部の JumpStart 基盤モデルでは、ファインチューニングや推論の実行前にエンドユーザー使用許諾契約への同意が必要です。詳細については、「[エンドユーザーライセンス契約](jumpstart-foundation-models-choose.md#jumpstart-foundation-models-choose-eula)」を参照してください。
+ Flan-T5 Base
+ Flan-T5 Large
+ Flan-T5 Small
+ 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 テキストファイル形式で用意する必要があります。各行はディクショナリです。すべてのトレーニングデータを 1 つのフォルダに含める必要があります。このフォルダに複数の .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>

オプションで、検証データを含む別のフォルダを指定できます。このフォルダにも、1 つ以上の .jsonl ファイルを含める必要があります。検証データセットが指定されていない場合、所定量のトレーニングデータの一部が検証目的で確保されます。検証に使用されるトレーニングデータの割合は、モデルのファインチューニング用の該当するハイパーパラメータで調整できます。

### Amazon S3 にファインチューニングデータをアップロードする
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-upload-data"></a>

JumpStart 基盤モデルのファインチューニング時に使用できるように、準備したデータを Amazon Simple Storage Service (Amazon S3) にアップロードします。次のコマンドを使用して、データをアップロードできます。

```
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>

インストラクションベースのファインチューニングの詳細については、次のサンプルノートブックを参照してください。
+ [Fine-tune LLaMA 2 models on JumpStart](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/llama-2-finetuning.html)
+ [Introduction to SageMaker JumpStart - Text Generation with Mistral models](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/mistral-7b-instruction-domain-adaptation-finetuning.html)
+ [Introduction to SageMaker JumpStart - Text Generation with Falcon models](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)