File size: 15,113 Bytes
a9bd396 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | <!--Copyright 2023 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.
-->
# ์ธ๊ณผ ์ธ์ด ๋ชจ๋ธ๋ง[[causal-language-modeling]]
[[open-in-colab]]
์ธ์ด ๋ชจ๋ธ๋ง์ ์ธ๊ณผ์ ์ธ์ด ๋ชจ๋ธ๋ง๊ณผ ๋ง์คํฌ๋ ์ธ์ด ๋ชจ๋ธ๋ง, ๋ ๊ฐ์ง ์ ํ์ผ๋ก ๋๋ฉ๋๋ค. ์ด ๊ฐ์ด๋์์๋ ์ธ๊ณผ์ ์ธ์ด ๋ชจ๋ธ๋ง์ ์ค๋ช
ํฉ๋๋ค.
์ธ๊ณผ ์ธ์ด ๋ชจ๋ธ์ ํ
์คํธ ์์ฑ์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค. ๋ ์ฐฝ์์ ์ธ ๋ฐฉํฅ์ผ๋ก ์์ฉํ ์ ์์ต๋๋ค.
์ง์ ์ฌ์ฉํ๋ฉฐ ์ฌ๋ฏธ์๋ ํ๊ตฌ๋ฅผ ํด๋ณด๊ฑฐ๋, Copilot ๋๋ CodeParrot์ ๊ฐ์ ์ง๋ฅํ ์ฝ๋ฉ ์ด์์คํดํธ์ ๊ธฐ๋ฐ์ด ๋๊ธฐ๋ ํฉ๋๋ค.
<Youtube id="Vpjb1lu0MDk"/>
์ธ๊ณผ ์ธ์ด ๋ชจ๋ธ๋ง์ ํ ํฐ ์ํ์ค์์ ๋ค์ ํ ํฐ์ ์์ธกํ๋ฉฐ, ๋ชจ๋ธ์ ์ผ์ชฝ์ ํ ํฐ์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค.
์ด๋ ๋ชจ๋ธ์ด ๋ฏธ๋์ ํ ํฐ์ ๋ณผ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ธ๊ณผ ์ธ์ด ๋ชจ๋ธ์ ์๋ก GPT-2๊ฐ ์์ฃ .
์ด ๊ฐ์ด๋์์๋ ๋ค์ ์์
์ ์ํํ๋ ๋ฐฉ๋ฒ์ ์๋ดํฉ๋๋ค:
1. [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) ๋ชจ๋ธ์ [ELI5](https://huggingface.co/datasets/eli5) ๋ฐ์ดํฐ ์ธํธ์ [r/askscience](https://www.reddit.com/r/askscience/) ํ์ ์งํฉ์ผ๋ก ๋ฏธ์ธ ์กฐ์
2. ๋ฏธ์ธ ์กฐ์ ๋ ๋ชจ๋ธ์ ์ถ๋ก ์ ์ฌ์ฉ
<Tip>
์ด ์์
๊ณผ ํธํ๋๋ ๋ชจ๋ ์ํคํ
์ฒ์ ์ฒดํฌํฌ์ธํธ๋ฅผ ๋ณด๋ ค๋ฉด [์์
ํ์ด์ง](https://huggingface.co/tasks/text-generation)๋ฅผ ํ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
</Tip>
์์ํ๊ธฐ ์ ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
```bash
pip install transformers datasets evaluate
```
์ปค๋ฎค๋ํฐ์ ๋ชจ๋ธ์ ์
๋ก๋ํ๊ณ ๊ณต์ ํ๊ธฐ ์ํด Hugging Face ๊ณ์ ์ ๋ก๊ทธ์ธํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ์๋ฆผ์ด ํ์๋๋ฉด ํ ํฐ์ ์
๋ ฅํ์ฌ ๋ก๊ทธ์ธํ์ธ์:
```py
>>> from huggingface_hub import notebook_login
>>> notebook_login()
```
## ELI5 ๋ฐ์ดํฐ ์ธํธ ๋ถ๋ฌ์ค๊ธฐ[[load-eli5-dataset]]
๋จผ์ , ๐ค Datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ r/askscience์ ์์ ํ์ ์งํฉ์ธ ELI5 ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ถ๋ฌ์ต๋๋ค.
์ด๋ฅผ ํตํด ์ ์ฒด ๋ฐ์ดํฐ ์ธํธ์์ ํ์ตํ๋ ๋ฐ ๋ ๋ง์ ์๊ฐ์ ํฌ์ํ๊ธฐ ์ ์, ์คํํด๋ด์ผ๋ก์จ ๋ชจ๋ ๊ฒ์ด ์๋ํ๋์ง ํ์ธํ ์ ์์ต๋๋ค.
```py
>>> from datasets import load_dataset
>>> eli5 = load_dataset("eli5", split="train_asks[:5000]")
```
๋ฐ์ดํฐ ์ธํธ์ `train_asks` ๋ถํ ์ [`~datasets.Dataset.train_test_split`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์ต ๋ฐ ํ
์คํธ ์ธํธ๋ก ๋ถํ ํฉ๋๋ค:
```py
>>> eli5 = eli5.train_test_split(test_size=0.2)
```
๊ทธ๋ฐ ๋ค์ ์์ ๋ฅผ ์ดํด๋ณด์ธ์:
```py
>>> eli5["train"][0]
{'answers': {'a_id': ['c3d1aib', 'c3d4lya'],
'score': [6, 3],
'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.",
"Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]},
'answers_urls': {'url': []},
'document': '',
'q_id': 'nyxfp',
'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?',
'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']},
'subreddit': 'askscience',
'title': 'Few questions about this space walk photograph.',
'title_urls': {'url': []}}
```
๋ง์ ๋ณด์ผ ์ ์์ง๋ง, ์ค์ ๋ก๋ `text` ํ๋๋ง ์ค์ํฉ๋๋ค. ์ธ์ด ๋ชจ๋ธ๋ง ์์
์ ์ฅ์ ์ ๋ ์ด๋ธ์ด ํ์ํ์ง ์๋ค๋ ๊ฒ์
๋๋ค. ๋ค์ ๋จ์ด *์์ฒด๊ฐ* ๋ ์ด๋ธ์
๋๋ค. (์ด๋ ๊ฒ ๋ ์ด๋ธ์ ์ ๊ณตํ์ง ์์๋ ๋๋ ํ์ต์ ๋น์ง๋ ํ์ต์ด๋ผ๊ณ ์ผ์ปซ์ต๋๋ค)
## ์ ์ฒ๋ฆฌ[[preprocess]]
<Youtube id="ma1TrR7gE7I"/>
๋ค์ ๋จ๊ณ๋ `text` ํ๋๋ฅผ ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด DistilGPT2 ํ ํฌ๋์ด์ ๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ์
๋๋ค.
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
```
์์ ์์ ์์ ์ ์ ์๋ฏ์ด, `text` ํ๋๋ `answers` ์๋์ ์ค์ฒฉ๋์ด ์์ต๋๋ค. ๋ฐ๋ผ์ [`flatten`](https://huggingface.co/docs/datasets/process#flatten) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ฒฉ ๊ตฌ์กฐ์์ `text` ํ์ ํ๋๋ฅผ ์ถ์ถํด์ผ ํฉ๋๋ค.
```py
>>> eli5 = eli5.flatten()
>>> eli5["train"][0]
{'answers.a_id': ['c3d1aib', 'c3d4lya'],
'answers.score': [6, 3],
'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.",
"Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"],
'answers_urls.url': [],
'document': '',
'q_id': 'nyxfp',
'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?',
'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'],
'subreddit': 'askscience',
'title': 'Few questions about this space walk photograph.',
'title_urls.url': []}
```
๊ฐ ํ์ ํ๋๋ ์ด์ `answers` ์ ๋์ฌ๋ฅผ ๊ฐ์ง ๋ณ๋์ ์ด๋ก ๋๋์์ผ๋ฉฐ, `text` ํ๋๋ ์ด์ ๋ฆฌ์คํธ์
๋๋ค. ๊ฐ ๋ฌธ์ฅ์ ๊ฐ๋ณ์ ์ผ๋ก ํ ํฐํํ๋ ๋์ , ๋จผ์ ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ํ๊บผ๋ฒ์ ํ ํฐํํ ์ ์์ต๋๋ค.
๋ค์์ ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ๊ฒฐํฉํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ ํฐํํ๋ ์ฒซ ๋ฒ์งธ ์ ์ฒ๋ฆฌ ํจ์์
๋๋ค:
```py
>>> def preprocess_function(examples):
... return tokenizer([" ".join(x) for x in examples["answers.text"]])
```
์ด ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ ์ฒด ๋ฐ์ดํฐ ์ธํธ์ ์ ์ฉํ๋ ค๋ฉด ๐ค Datasets [`~datasets.Dataset.map`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์. `batched=True`๋ก ์ค์ ํ์ฌ ๋ฐ์ดํฐ์
์ ์ฌ๋ฌ ์์๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๊ณ , `num_proc`๋ฅผ ์ฆ๊ฐ์์ผ ํ๋ก์ธ์ค ์๋ฅผ ๋๋ฆด ์ ์์ต๋๋ค. ํ์ ์๋ ์ด์ ์ ๊ฑฐํ์ธ์:
```py
>>> tokenized_eli5 = eli5.map(
... preprocess_function,
... batched=True,
... num_proc=4,
... remove_columns=eli5["train"].column_names,
... )
```
์ด์ ๋ฐ์ดํฐ ์ธํธ๋ ์ํ์ค๊ฐ ํ ํฐํ๋์ง๋ง, ์ผ๋ถ ์ํ์ค๋ ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ณด๋ค ๊ธธ ์ ์์ต๋๋ค.
์ด์ ๋ ๋ฒ์งธ ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ
- ๋ชจ๋ ์ํ์ค๋ฅผ ์ฐ๊ฒฐํ๊ณ ,
- `block_size`๋ก ์ ์๋ ๊ธธ์ด๋ก ์ฐ๊ฒฐ๋ ์ํ์ค๋ฅผ ์ฌ๋ฌ ๊ฐ์ ์งง์ ๋ฌถ์์ผ๋ก ๋๋๋๋ค. ์ด ๊ฐ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด์ GPU RAM์ ๊ณ ๋ คํด ์ถฉ๋ถํ ์งง์์ผ ํฉ๋๋ค.
```py
>>> block_size = 128
>>> def group_texts(examples):
... # Concatenate all texts.
... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()}
... total_length = len(concatenated_examples[list(examples.keys())[0]])
... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
... # customize this part to your needs.
... if total_length >= block_size:
... total_length = (total_length // block_size) * block_size
... # Split by chunks of block_size.
... result = {
... k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
... for k, t in concatenated_examples.items()
... }
... result["labels"] = result["input_ids"].copy()
... return result
```
์ ์ฒด ๋ฐ์ดํฐ ์ธํธ์ `group_texts` ํจ์๋ฅผ ์ ์ฉํ์ธ์:
```py
>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4)
```
๊ทธ๋ฐ ๋ค์ [`DataCollatorForLanguageModeling`]์ ์ฌ์ฉํ์ฌ ์์ ์ ๋ฐฐ์น๋ฅผ ๋ง๋ญ๋๋ค. ๋ฐ์ดํฐ ์ธํธ ์ ์ฒด๋ฅผ ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉํ๋ ๊ฒ๋ณด๋ค, ์ทจํฉ ๋จ๊ณ์์ ๊ฐ ๋ฐฐ์น์ ์ต๋ ๊ธธ์ด๋ก ๋ฌธ์ฅ์ *๋์ ์ผ๋ก ํจ๋ฉ*ํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์
๋๋ค.
ํจ๋ฉ ํ ํฐ์ผ๋ก ์ข
๊ฒฐ ํ ํฐ์ ์ฌ์ฉํ๊ณ `mlm=False`๋ก ์ค์ ํ์ธ์. ์ด๋ ๊ฒ ํ๋ฉด ์
๋ ฅ์ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ์ฉ ์ํํธํ ๊ฐ์ ๋ ์ด๋ธ๋ก ์ฌ์ฉํฉ๋๋ค:
```py
>>> from transformers import DataCollatorForLanguageModeling
>>> tokenizer.pad_token = tokenizer.eos_token
>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
```
## ํ๋ จ[[train]]
<Tip>
[`Trainer`]๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ชจ๋ฅด์ ๋ค๋ฉด [๊ธฐ๋ณธ ํํ ๋ฆฌ์ผ](../training#train-with-pytorch-trainer)์ ํ์ธํด๋ณด์ธ์!
</Tip>
์ด์ ๋ชจ๋ธ์ ํ๋ จํ๊ธฐ ์ค๋น๊ฐ ๋์์ต๋๋ค! [`AutoModelForCausalLM`]๋ฅผ ์ฌ์ฉํ์ฌ DistilGPT2๋ฅผ ๋ถ๋ฌ์ต๋๋ค:
```py
>>> from transformers import AutoModelForCausalLM, TrainingArguments, Trainer
>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
```
์ฌ๊ธฐ๊น์ง ์งํํ๋ฉด ์ธ ๋จ๊ณ๋ง ๋จ์์ต๋๋ค:
1. [`TrainingArguments`]์์ ํ๋ จ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ์ธ์. `output_dir`์ ์ ์ผํ ํ์ ๋งค๊ฐ๋ณ์๋ก, ๋ชจ๋ธ์ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํฉ๋๋ค. (๋จผ์ Hugging Face์ ๋ก๊ทธ์ธ ํ์) `push_to_hub=True`๋ก ์ค์ ํ์ฌ ์ด ๋ชจ๋ธ์ ํ๋ธ์ ์
๋ก๋ํ ์ ์์ต๋๋ค.
2. ํ๋ จ ์ธ์๋ฅผ [`Trainer`]์ ๋ชจ๋ธ, ๋ฐ์ดํฐ ์ธํธ ๋ฐ ๋ฐ์ดํฐ ์ฝ๋ ์ดํฐ์ ํจ๊ป ์ ๋ฌํ์ธ์.
3. [`~Trainer.train`]์ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ์ธ์.
```py
>>> training_args = TrainingArguments(
... output_dir="my_awesome_eli5_clm-model",
... eval_strategy="epoch",
... learning_rate=2e-5,
... weight_decay=0.01,
... push_to_hub=True,
... )
>>> trainer = Trainer(
... model=model,
... args=training_args,
... train_dataset=lm_dataset["train"],
... eval_dataset=lm_dataset["test"],
... data_collator=data_collator,
... )
>>> trainer.train()
```
ํ๋ จ์ด ์๋ฃ๋๋ฉด [`~transformers.Trainer.evaluate`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ๊ฐํ๊ณ ํผํ๋ ์ํฐ๋ฅผ ์ป์ ์ ์์ต๋๋ค:
```py
>>> import math
>>> eval_results = trainer.evaluate()
>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}")
Perplexity: 49.61
```
๊ทธ๋ฐ ๋ค์ [`~transformers.Trainer.push_to_hub`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ๋ธ์ ๊ณต์ ํ์ธ์. ์ด๋ ๊ฒ ํ๋ฉด ๋๊ตฌ๋ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
```py
>>> trainer.push_to_hub()
```
<Tip>
์ธ๊ณผ ์ธ์ด ๋ชจ๋ธ๋ง์ ์ํด ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ ์์ธํ ์์ ๋ ํด๋นํ๋ [PyTorch ๋
ธํธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) ๋๋ [TensorFlow ๋
ธํธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์ ์ฐธ์กฐํ์ธ์.
</Tip>
## ์ถ๋ก [[inference]]
์ข์์, ์ด์ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ์ผ๋ฏ๋ก ์ถ๋ก ์ ์ฌ์ฉํ ์ ์์ต๋๋ค!
์์ฑํ ํ
์คํธ๋ฅผ ์ํ ํ๋กฌํํธ๋ฅผ ๋ง๋ค์ด๋ณด์ธ์:
```py
>>> prompt = "Somatic hypermutation allows the immune system to"
```
์ถ๋ก ์ ์ํด ๋ฏธ์ธ ์กฐ์ ๋ ๋ชจ๋ธ์ ๊ฐ๋จํ ์ฌ์ฉํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ [`pipeline`]์์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ๋ชจ๋ธ๊ณผ ํจ๊ป ํ
์คํธ ์์ฑ์ ์ํ `pipeline`์ ์ธ์คํด์คํํ๊ณ ํ
์คํธ๋ฅผ ์ ๋ฌํ์ธ์:
```py
>>> from transformers import pipeline
>>> generator = pipeline("text-generation", model="my_awesome_eli5_clm-model")
>>> generator(prompt)
[{'generated_text': "Somatic hypermutation allows the immune system to be able to effectively reverse the damage caused by an infection.\n\n\nThe damage caused by an infection is caused by the immune system's ability to perform its own self-correcting tasks."}]
```
ํ
์คํธ๋ฅผ ํ ํฐํํ๊ณ `input_ids`๋ฅผ PyTorch ํ
์๋ก ๋ฐํํ์ธ์:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_clm-model")
>>> inputs = tokenizer(prompt, return_tensors="pt").input_ids
```
[`~generation.GenerationMixin.generate`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ๋ฅผ ์์ฑํ์ธ์. ์์ฑ์ ์ ์ดํ๋ ๋ค์ํ ํ
์คํธ ์์ฑ ์ ๋ต๊ณผ ๋งค๊ฐ๋ณ์์ ๋ํ ์์ธํ ๋ด์ฉ์ [ํ
์คํธ ์์ฑ ์ ๋ต](../generation_strategies) ํ์ด์ง๋ฅผ ํ์ธํ์ธ์.
```py
>>> from transformers import AutoModelForCausalLM
>>> model = AutoModelForCausalLM.from_pretrained("my_awesome_eli5_clm-model")
>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95)
```
์์ฑ๋ ํ ํฐ ID๋ฅผ ๋ค์ ํ
์คํธ๋ก ๋์ฝ๋ฉํ์ธ์:
```py
>>> tokenizer.batch_decode(outputs, skip_special_tokens=True)
["Somatic hypermutation allows the immune system to react to drugs with the ability to adapt to a different environmental situation. In other words, a system of 'hypermutation' can help the immune system to adapt to a different environmental situation or in some cases even a single life. In contrast, researchers at the University of Massachusetts-Boston have found that 'hypermutation' is much stronger in mice than in humans but can be found in humans, and that it's not completely unknown to the immune system. A study on how the immune system"]
```
|