8.1 KiB
ํจ๋ฉ๊ณผ ์๋ผ๋ด๊ธฐ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) |