huggingface--transformers
e06fe8e8c6
Secret Leaks / trufflehog (push) Failing after 1s
Build documentation / build (push) Failing after 1s
Build documentation / build_other_lang (push) Failing after 0s
CodeQL Security Analysis / CodeQL Analysis (push) Failing after 0s
PR CI / pr-ci (push) Failing after 1s
Slow tests on important models (on Push - A10) / Get all modified files (push) Failing after 1s
Slow tests on important models (on Push - A10) / Model CI (push) Has been skipped
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
69 ่ก
8.1 KiB
Markdown
69 ่ก
8.1 KiB
Markdown
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations under the License.
|
|
|
|
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
|
rendered properly in your Markdown viewer.
|
|
|
|
-->
|
|
|
|
# ํจ๋ฉ๊ณผ ์๋ผ๋ด๊ธฐ[[padding-and-truncation]]
|
|
|
|
๋ฐฐ์น ์
๋ ฅ์ ๊ธธ์ด๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ๊ฐ ๋ง์์ ๊ณ ์ ํฌ๊ธฐ ํ
์๋ก ๋ณํํ ์ ์์ต๋๋ค. ํจ๋ฉ๊ณผ ์๋ผ๋ด๊ธฐ๋ ๋ค์ํ ๊ธธ์ด์ ๋ฐฐ์น์์ ์ง์ฌ๊ฐํ ํ
์๋ฅผ ์์ฑํ ์ ์๋๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์ ๋ต์
๋๋ค. ํจ๋ฉ์ ํน์ํ **ํจ๋ฉ ํ ํฐ**์ ์ถ๊ฐํ์ฌ ์งง์ ์ํ์ค๊ฐ ๋ฐฐ์น์์ ๊ฐ์ฅ ๊ธด ์ํ์ค ๋๋ ๋ชจ๋ธ์์ ํ์ฉํ๋ ์ต๋ ๊ธธ์ด์ ๋์ผํ ๊ธธ์ด๋ฅผ ๊ฐ๋๋ก ํฉ๋๋ค. ์๋ผ๋ด๊ธฐ๋ ๊ธด ์ํ์ค๋ฅผ ์๋ผ๋ด์ด ํจ๋ฉ๊ณผ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์ํ์ค์ ๊ธธ์ด๋ฅผ ๋์ผํ๊ฒ ํฉ๋๋ค.
|
|
|
|
๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ฐฐ์น์ ๊ฐ์ฅ ๊ธด ์ํ์ค์ ๊ธธ์ด๋ก ํจ๋ฉํ๊ณ ๋ชจ๋ธ์ด ํ์ฉํ ์ ์๋ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋ด๋ ๊ฒ์ด ์ ์๋ํฉ๋๋ค. ๊ทธ๋ฌ๋ ํ์ํ๋ค๋ฉด API๊ฐ ์ง์ํ๋ ๋ ๋ง์ ์ ๋ต์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํ์ํ ์ธ์๋ `padding`, `truncation`, `max_length` ์ธ ๊ฐ์ง์
๋๋ค.
|
|
|
|
`padding` ์ธ์๋ ํจ๋ฉ์ ์ ์ดํฉ๋๋ค. ๋ถ๋ฆฌ์ธ ๋๋ ๋ฌธ์์ด์ผ ์ ์์ต๋๋ค:
|
|
|
|
- `True` ๋๋ `'longest'`: ๋ฐฐ์น์์ ๊ฐ์ฅ ๊ธด ์ํ์ค๋ก ํจ๋ฉํฉ๋๋ค(๋จ์ผ ์ํ์ค๋ง ์ ๊ณตํ๋ ๊ฒฝ์ฐ ํจ๋ฉ์ด ์ ์ฉ๋์ง ์์ต๋๋ค).
|
|
- `'max_length'`: `max_length` ์ธ์๊ฐ ์ง์ ํ ๊ธธ์ด๋ก ํจ๋ฉํ๊ฑฐ๋, `max_length`๊ฐ ์ ๊ณต๋์ง ์์ ๊ฒฝ์ฐ(`max_length=None`) ๋ชจ๋ธ์์ ํ์ฉ๋๋ ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉํฉ๋๋ค. ๋จ์ผ ์ํ์ค๋ง ์ ๊ณตํ๋ ๊ฒฝ์ฐ์๋ ํจ๋ฉ์ด ์ ์ฉ๋ฉ๋๋ค.
|
|
- `False` ๋๋ `'do_not_pad'`: ํจ๋ฉ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ์ด๊ฒ์ด ๊ธฐ๋ณธ ๋์์
๋๋ค.
|
|
|
|
`truncation` ์ธ์๋ ์๋ผ๋ผ ๋ฐฉ๋ฒ์ ์ ํฉ๋๋ค. ๋ถ๋ฆฌ์ธ ๋๋ ๋ฌธ์์ด์ผ ์ ์์ต๋๋ค:
|
|
|
|
- `True` ๋๋ `longest_first`: `max_length` ์ธ์๊ฐ ์ง์ ํ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋ด๊ฑฐ๋,
|
|
`max_length`๊ฐ ์ ๊ณต๋์ง ์์ ๊ฒฝ์ฐ(`max_length=None`) ๋ชจ๋ธ์์ ํ์ฉ๋๋ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋
๋๋ค.
|
|
์ํ์ค ์์์ ๊ฐ์ฅ ๊ธด ์ํ์ค์ ํ ํฐ์ ์ ์ ํ ๊ธธ์ด์ ๋๋ฌํ ๋๊น์ง ํ๋์ฉ ์ ๊ฑฐํฉ๋๋ค.
|
|
- `'only_second'`: `max_length` ์ธ์๊ฐ ์ง์ ํ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋ด๊ฑฐ๋,
|
|
`max_length`๊ฐ ์ ๊ณต๋์ง ์์ ๊ฒฝ์ฐ(`max_length=None`) ๋ชจ๋ธ์์ ํ์ฉ๋๋ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋
๋๋ค.
|
|
์ํ์ค ์(๋๋ ์ํ์ค ์์ ๋ฐฐ์น)๊ฐ ์ ๊ณต๋ ๊ฒฝ์ฐ ์์ ๋ ๋ฒ์งธ ๋ฌธ์ฅ๋ง ์๋ผ๋
๋๋ค.
|
|
- `'only_first'`: `max_length` ์ธ์๊ฐ ์ง์ ํ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋ด๊ฑฐ๋,
|
|
`max_length`๊ฐ ์ ๊ณต๋์ง ์์ ๊ฒฝ์ฐ(`max_length=None`) ๋ชจ๋ธ์์ ํ์ฉ๋๋ ์ต๋ ๊ธธ์ด๋ก ์๋ผ๋
๋๋ค.
|
|
์ํ์ค ์(๋๋ ์ํ์ค ์์ ๋ฐฐ์น)๊ฐ ์ ๊ณต๋ ๊ฒฝ์ฐ ์์ ์ฒซ ๋ฒ์งธ ๋ฌธ์ฅ๋ง ์๋ผ๋
๋๋ค.
|
|
- `False` ๋๋ `'do_not_truncate'`: ์๋ผ๋ด๊ธฐ๋ฅผ ์ ์ฉํ์ง ์์ต๋๋ค. ์ด๊ฒ์ด ๊ธฐ๋ณธ ๋์์
๋๋ค.
|
|
|
|
`max_length` ์ธ์๋ ํจ๋ฉ ๋ฐ ์๋ผ๋ด๊ธฐ๋ฅผ ์ ์ฉํ ๊ธธ์ด๋ฅผ ์ ์ดํฉ๋๋ค. ์ด ์ธ์๋ ์ ์ ๋๋ `None`์ผ ์ ์์ผ๋ฉฐ, `None`์ผ ๊ฒฝ์ฐ ๋ชจ๋ธ์ด ํ์ฉํ ์ ์๋ ์ต๋ ๊ธธ์ด๋ก ๊ธฐ๋ณธ๊ฐ์ด ์ค์ ๋ฉ๋๋ค. ๋ชจ๋ธ์ ํน์ ํ ์ต๋ ์
๋ ฅ ๊ธธ์ด๊ฐ ์๋ ๊ฒฝ์ฐ `max_length`์ ๋ํ ์๋ผ๋ด๊ธฐ ๋๋ ํจ๋ฉ์ด ๋นํ์ฑํ๋ฉ๋๋ค.
|
|
|
|
๋ค์ ํ์๋ ํจ๋ฉ ๋ฐ ์๋ผ๋ด๊ธฐ๋ฅผ ์ค์ ํ๋ ๊ถ์ฅ ๋ฐฉ๋ฒ์ด ์์ฝ๋์ด ์์ต๋๋ค.
|
|
์
๋ ฅ์ผ๋ก ์ํ์ค ์์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ค์ ์์ ์์ `truncation=True`๋ฅผ `['only_first', 'only_second', 'longest_first']`์์ ์ ํํ `STRATEGY`, ์ฆ `truncation='only_second'` ๋๋ `truncation='longest_first'`๋ก ๋ฐ๊พธ๋ฉด ์์ ์ค๋ช
ํ ๋๋ก ์์ ๋ ์ํ์ค๊ฐ ์๋ฆฌ๋ ๋ฐฉ์์ ์ ์ดํ ์ ์์ต๋๋ค.
|
|
|
|
| ์๋ผ๋ด๊ธฐ | ํจ๋ฉ | ์ฌ์ฉ ๋ฐฉ๋ฒ |
|
|
|--------------------------------------|-----------------------------------|------------------------------------------------------------------------------------------|
|
|
| ์๋ผ๋ด๊ธฐ ์์ | ํจ๋ฉ ์์ | `tokenizer(batch_sentences)` |
|
|
| | ๋ฐฐ์น ๋ด ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding=True)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, padding='longest')` |
|
|
| | ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding='max_length')` |
|
|
| | ํน์ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding='max_length', max_length=42)` |
|
|
| | ๋ค์ํ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding=True, pad_to_multiple_of=8)` |
|
|
| ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ก ์๋ผ๋ด๊ธฐ | ํจ๋ฉ ์์ | `tokenizer(batch_sentences, truncation=True)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, truncation=STRATEGY)` |
|
|
| | ๋ฐฐ์น ๋ด ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding=True, truncation=True)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY)` |
|
|
| | ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding='max_length', truncation=True)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY)` |
|
|
| | ํน์ ๊ธธ์ด๋ก ํจ๋ฉ | ์ฌ์ฉ ๋ถ๊ฐ |
|
|
| ํน์ ๊ธธ์ด๋ก ์๋ผ๋ด๊ธฐ | ํจ๋ฉ ์์ | `tokenizer(batch_sentences, truncation=True, max_length=42)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, truncation=STRATEGY, max_length=42)` |
|
|
| | ๋ฐฐ์น ๋ด ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42)` |
|
|
| | ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ก ํจ๋ฉ | ์ฌ์ฉ ๋ถ๊ฐ |
|
|
| | ํน์ ๊ธธ์ด๋ก ํจ๋ฉ | `tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` ๋๋ |
|
|
| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42)` |
|