
* udpaet * update * Update docs/source/ja/autoclass_tutorial.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * add codes workflows/build_pr_documentation.yml * Create preprocessing.md * added traning.md * Create Model_sharing.md * add quicktour.md * new * ll * Create benchmark.md * Create Tensorflow_model * add * add community.md * add create_a_model * create custom_model.md * create_custom_tools.md * create fast_tokenizers.md * create * add * Update docs/source/ja/_toctree.yml Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * md * add * commit * add * h * Update docs/source/ja/peft.md Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> * Update docs/source/ja/_toctree.yml Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> * Update docs/source/ja/_toctree.yml Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> * Suggested Update * add perf_train_gpu_one.md * added perf based MD files * Modify toctree.yml and Add transmartion to md codes * Add `serialization.md` and edit `_toctree.yml` * add task summary and tasks explained * Add and Modify files starting from T * Add testing.md * Create main_classes files * delete main_classes folder * Add toctree.yml * Update llm_tutorail.md * Update docs/source/ja/_toctree.yml Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update misspelled filenames * Update docs/source/ja/_toctree.yml Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/_toctree.yml * Update docs/source/ja/_toctree.yml * missplled file names inmrpovements * Update _toctree.yml * close tip block * close another tip block * Update docs/source/ja/quicktour.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/pipeline_tutorial.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/pipeline_tutorial.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/preprocessing.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/peft.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/add_new_model.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/testing.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/task_summary.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/tasks_explained.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update glossary.md * Update docs/source/ja/transformers_agents.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/llm_tutorial.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/create_a_model.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/torchscript.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/benchmarks.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/troubleshooting.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/troubleshooting.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/troubleshooting.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update docs/source/ja/add_new_model.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * Update perf_torch_compile.md * Update Year to default in en documentation * Final Update --------- Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
8.1 KiB
Padding and truncation
バッチ入力はしばしば異なる長さであり、固定サイズのテンソルに変換できないため、変動する長さのバッチから長方形のテンソルを作成するための戦略として、パディングと切り詰めがあります。パディングは、短いシーケンスがバッチ内の最長シーケンスまたはモデルが受け入れる最大長と同じ長さになるように、特別なパディングトークンを追加します。切り詰めは、長いシーケンスを切り詰めることで逆方向に機能します。
ほとんどの場合、バッチを最長シーケンスの長さにパディングし、モデルが受け入れる最大長に切り詰めることで、うまく動作します。ただし、APIはそれ以上の戦略もサポートしています。必要な3つの引数は次のとおりです:padding
、truncation
、および max_length
。
padding
引数はパディングを制御します。ブール値または文字列であることができます:
True
または'longest'
:バッチ内の最長シーケンスにパディングを追加します(シーケンスが1つしか提供されない場合、パディングは適用されません)。max_length'
:max_length
引数で指定された長さまでパディングを追加します。またはmax_length
が提供されていない場合はモデルが受け入れる最大長(max_length=None
)。シーケンスが1つしか提供されている場合でも、パディングは適用されます。False
または'do_not_pad'
:パディングは適用されません。これがデフォルトの動作です。
truncation
引数は切り詰めを制御します。ブール値または文字列であることができます:
True
または'longest_first'
:最大長をmax_length
引数で指定するか、モデルが受け入れる最大長(max_length=None
)まで切り詰めます。これはトークンごとに切り詰め、適切な長さに達するまでペア内の最長シーケンスからトークンを削除します。'only_second'
:最大長をmax_length
引数で指定するか、モデルが受け入れる最大長(max_length=None
)まで切り詰めます。これはペアの2番目の文だけを切り詰めます(シーケンスのペアまたはシーケンスのバッチのペアが提供された場合)。'only_first'
:最大長をmax_length
引数で指定するか、モデルが受け入れる最大長(max_length=None
)まで切り詰めます。これはペアの最初の文だけを切り詰めます(シーケンスのペアまたはシーケンスのバッチのペアが提供された場合)。False
または'do_not_truncate'
:切り詰めは適用されません。これがデフォルトの動作です。
max_length
引数はパディングと切り詰めの長さを制御します。整数またはNone
であり、この場合、モデルが受け入れる最大入力長にデフォルトで設定されます。モデルに特定の最大入力長がない場合、max_length
への切り詰めまたはパディングは無効になります。
以下の表は、パディングと切り詰めを設定する推奨方法を要約しています。以下の例のいずれかで入力シーケンスのペアを使用する場合、truncation=True
を['only_first', 'only_second', 'longest_first']
で選択したSTRATEGY
に置き換えることができます。つまり、truncation='only_second'
またはtruncation='longest_first'
を使用して、ペア内の両方のシーケンスを前述のように切り詰める方法を制御できます。
Truncation | Padding | Instruction |
---|---|---|
no truncation | no padding | tokenizer(batch_sentences) |
padding to max sequence in batch | tokenizer(batch_sentences, padding=True) or |
|
tokenizer(batch_sentences, padding='longest') |
||
padding to max model input length | tokenizer(batch_sentences, padding='max_length') |
|
padding to specific length | tokenizer(batch_sentences, padding='max_length', max_length=42) |
|
padding to a multiple of a value | `tokenizer(batch_sentences, padding=True, pad_to_multiple_of=8) | |
truncation to max model input length | no padding | tokenizer(batch_sentences, truncation=True) or |
tokenizer(batch_sentences, truncation=STRATEGY) |
||
padding to max sequence in batch | tokenizer(batch_sentences, padding=True, truncation=True) or |
|
tokenizer(batch_sentences, padding=True, truncation=STRATEGY) |
||
padding to max model input length | tokenizer(batch_sentences, padding='max_length', truncation=True) or |
|
tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY) |
||
padding to specific length | Not possible | |
truncation to specific length | no padding | tokenizer(batch_sentences, truncation=True, max_length=42) or |
tokenizer(batch_sentences, truncation=STRATEGY, max_length=42) |
||
padding to max sequence in batch | tokenizer(batch_sentences, padding=True, truncation=True, max_length=42) or |
|
tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42) |
||
padding to max model input length | Not possible | |
padding to specific length | tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42) or |
|
tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42) |