Spaces:
Runtime error
Runtime error
File size: 21,042 Bytes
96e9536 |
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
<!--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.
-->
# ์ฌ์ ํ์ต๋ ๋ชจ๋ธ ๋ฏธ์ธ ํ๋ํ๊ธฐ[[finetune-a-pretrained-model]]
[[open-in-colab]]
์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ฌ์ฉํ๋ฉด ์๋นํ ์ด์ ์ด ์์ต๋๋ค. ๊ณ์ฐ ๋น์ฉ๊ณผ ํ์๋ฐ์๊ตญ์ ์ค์ด๊ณ , ์ฒ์๋ถํฐ ๋ชจ๋ธ์ ํ์ต์ํฌ ํ์ ์์ด ์ต์ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๐ค Transformers๋ ๋ค์ํ ์์
์ ์ํด ์ฌ์ ํ์ต๋ ์์ฒ ๊ฐ์ ๋ชจ๋ธ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ์์ ์ ์์
๊ณผ ๊ด๋ จ๋ ๋ฐ์ดํฐ์
์ ์ฌ์ฉํด ํ์ตํฉ๋๋ค. ์ด๊ฒ์ ๋ฏธ์ธ ํ๋์ด๋ผ๊ณ ํ๋ ๋งค์ฐ ๊ฐ๋ ฅํ ํ๋ จ ๊ธฐ๋ฒ์
๋๋ค. ์ด ํํ ๋ฆฌ์ผ์์๋ ๋น์ ์ด ์ ํํ ๋ฅ๋ฌ๋ ํ๋ ์์ํฌ๋ก ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํฉ๋๋ค:
* ๐ค Transformers๋ก ์ฌ์ ํ์ต๋ ๋ชจ๋ธ ๋ฏธ์ธ ํ๋ํ๊ธฐ [`Trainer`].
* Keras๋ฅผ ์ฌ์ฉํ์ฌ TensorFlow์์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ.
* ๊ธฐ๋ณธ PyTorch์์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ.
<a id='data-processing'></a>
## ๋ฐ์ดํฐ์
์ค๋น[[prepare-a-dataset]]
<Youtube id="_BZearw7f0w"/>
์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ ์ํด์ ๋ฐ์ดํฐ์
์ ๋ค์ด๋ก๋ํ๊ณ ํ๋ จํ ์ ์๋๋ก ์ค๋นํ์ธ์. ์ด์ ํํ ๋ฆฌ์ผ์์ ํ๋ จ์ ์ํด ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ๋๋ ธ๋๋ฐ, ์ง๊ธ์ด ๋ฐฐ์ธ ๊ฑธ ๋์ง์ ๊ธฐํ์
๋๋ค!
๋จผ์ [Yelp ๋ฆฌ๋ทฐ](https://huggingface.co/datasets/yelp_review_full) ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ก๋ํฉ๋๋ค:
```py
>>> from datasets import load_dataset
>>> dataset = load_dataset("yelp_review_full")
>>> dataset["train"][100]
{'label': 0,
'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'}
```
ํ
์คํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์๋ก ๋ค๋ฅธ ๊ธธ์ด์ ์ํ์ค ํจ๋ฉ ๋ฐ ์๋ผ๋ด๊ธฐ ์ ๋ต์ ํฌํจํ๋ ค๋ฉด ํ ํฌ๋์ด์ ๊ฐ ํ์ํฉ๋๋ค. ๋ฐ์ดํฐ์
์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ ค๋ฉด ๐ค Dataset [`map`](https://huggingface.co/docs/datasets/process.html#map) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ฒด ๋ฐ์ดํฐ์
์ ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ ์ฉํ์ธ์:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
>>> def tokenize_function(examples):
... return tokenizer(examples["text"], padding="max_length", truncation=True)
>>> tokenized_datasets = dataset.map(tokenize_function, batched=True)
```
ํ์ํ ๊ฒฝ์ฐ ๋ฏธ์ธ ํ๋์ ์ํด ๋ฐ์ดํฐ์
์ ์์ ๋ถ๋ถ ์งํฉ์ ๋ง๋ค์ด ๋ฏธ์ธ ํ๋ ์์
์๊ฐ์ ์ค์ผ ์ ์์ต๋๋ค:
```py
>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000))
>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000))
```
<a id='trainer'></a>
## Train
์ฌ๊ธฐ์๋ถํฐ๋ ์ฌ์ฉํ๋ ค๋ ํ๋ ์์ํฌ์ ํด๋นํ๋ ์น์
์ ๋ฐ๋ผ์ผ ํฉ๋๋ค. ์ค๋ฅธ์ชฝ ์ฌ์ด๋๋ฐ์ ๋งํฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ํ๋ ์์ํฌ๋ก ์ด๋ํ ์ ์์ผ๋ฉฐ, ํน์ ํ๋ ์์ํฌ์ ๋ชจ๋ ์ฝํ
์ธ ๋ฅผ ์จ๊ธฐ๋ ค๋ฉด ํด๋น ํ๋ ์์ํฌ ๋ธ๋ก์ ์ค๋ฅธ์ชฝ ์๋จ์ ์๋ ๋ฒํผ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค!
<frameworkcontent>
<pt>
<Youtube id="nvBXf7s7vTI"/>
## ํ์ดํ ์น Trainer๋ก ํ๋ จํ๊ธฐ[[train-with-pytorch-trainer]]
๐ค Transformers๋ ๐ค Transformers ๋ชจ๋ธ ํ๋ จ์ ์ต์ ํ๋ [`Trainer`] ํด๋์ค๋ฅผ ์ ๊ณตํ์ฌ ํ๋ จ ๋ฃจํ๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ ๋ ์ฝ๊ฒ ํ๋ จ์ ์์ํ ์ ์์ต๋๋ค. [`Trainer`] API๋ ๋ก๊น
(logging), ๊ฒฝ์ฌ ๋์ (gradient accumulation), ํผํฉ ์ ๋ฐ๋(mixed precision) ๋ฑ ๋ค์ํ ํ๋ จ ์ต์
๊ณผ ๊ธฐ๋ฅ์ ์ง์ํฉ๋๋ค.
๋จผ์ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๊ณ ์์๋๋ ๋ ์ด๋ธ ์๋ฅผ ์ง์ ํฉ๋๋ค. Yelp ๋ฆฌ๋ทฐ [๋ฐ์ดํฐ์
์นด๋](https://huggingface.co/datasets/yelp_review_full#data-fields)์์ 5๊ฐ์ ๋ ์ด๋ธ์ด ์์์ ์ ์ ์์ต๋๋ค:
```py
>>> from transformers import AutoModelForSequenceClassification
>>> model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
```
<Tip>
์ฌ์ ํ๋ จ๋ ๊ฐ์ค์น ์ค ์ผ๋ถ๊ฐ ์ฌ์ฉ๋์ง ์๊ณ ์ผ๋ถ ๊ฐ์ค์น๊ฐ ๋ฌด์์๋ก ํ์๋๋ค๋ ๊ฒฝ๊ณ ๊ฐ ํ์๋ฉ๋๋ค.
๊ฑฑ์ ๋ง์ธ์. ์ด๊ฒ์ ์ฌ๋ฐ๋ฅธ ๋์์
๋๋ค! ์ฌ์ ํ์ต๋ BERT ๋ชจ๋ธ์ ํค๋๋ ํ๊ธฐ๋๊ณ ๋ฌด์์๋ก ์ด๊ธฐํ๋ ๋ถ๋ฅ ํค๋๋ก ๋์ฒด๋ฉ๋๋ค. ์ด์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ง์์ผ๋ก ์ํ์ค ๋ถ๋ฅ ์์
์ ์ํ ์๋ก์ด ๋ชจ๋ธ ํค๋๋ฅผ ๋ฏธ์ธ ํ๋ ํฉ๋๋ค.
</Tip>
### ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋ จ[[training-hyperparameters]]
๋ค์์ผ๋ก ์ ํ ์ ์๋ ๋ชจ๋ ํ์ดํผํ๋ผ๋ฏธํฐ์ ๋ค์ํ ํ๋ จ ์ต์
์ ํ์ฑํํ๊ธฐ ์ํ ํ๋๊ทธ๋ฅผ ํฌํจํ๋ [`TrainingArguments`] ํด๋์ค๋ฅผ ์์ฑํฉ๋๋ค.
์ด ํํ ๋ฆฌ์ผ์์๋ ๊ธฐ๋ณธ ํ๋ จ [ํ์ดํผํ๋ผ๋ฏธํฐ](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments)๋ก ์์ํ์ง๋ง, ์์ ๋กญ๊ฒ ์คํํ์ฌ ์ฌ๋ฌ๋ถ๋ค์๊ฒ ๋ง๋ ์ต์ ์ ์ค์ ์ ์ฐพ์ ์ ์์ต๋๋ค.
ํ๋ จ์์ ์ฒดํฌํฌ์ธํธ(checkpoints)๋ฅผ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํฉ๋๋ค:
```py
>>> from transformers import TrainingArguments
>>> training_args = TrainingArguments(output_dir="test_trainer")
```
### ํ๊ฐ ํ๊ธฐ[[evaluate]]
[`Trainer`]๋ ํ๋ จ ์ค์ ๋ชจ๋ธ ์ฑ๋ฅ์ ์๋์ผ๋ก ํ๊ฐํ์ง ์์ต๋๋ค. ํ๊ฐ ์งํ๋ฅผ ๊ณ์ฐํ๊ณ ๋ณด๊ณ ํ ํจ์๋ฅผ [`Trainer`]์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
[๐ค Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ [`evaluate.load`](https://huggingface.co/spaces/evaluate-metric/accuracy) ํจ์๋ก ๋ก๋ํ ์ ์๋ ๊ฐ๋จํ [`accuracy`]ํจ์๋ฅผ ์ ๊ณตํฉ๋๋ค (์์ธํ ๋ด์ฉ์ [๋๋ฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ์ธ์):
```py
>>> import numpy as np
>>> import evaluate
>>> metric = evaluate.load("accuracy")
```
`metric`์์ [`~evaluate.compute`]๋ฅผ ํธ์ถํ์ฌ ์์ธก์ ์ ํ๋๋ฅผ ๊ณ์ฐํฉ๋๋ค. ์์ธก์ `compute`์ ์ ๋ฌํ๊ธฐ ์ ์ ์์ธก์ ๋ก์ง์ผ๋ก ๋ณํํด์ผ ํฉ๋๋ค(๋ชจ๋ ๐ค Transformers ๋ชจ๋ธ์ ๋ก์ง์ผ๋ก ๋ฐํํ๋ค๋ ์ ์ ๊ธฐ์ตํ์ธ์):
```py
>>> def compute_metrics(eval_pred):
... logits, labels = eval_pred
... predictions = np.argmax(logits, axis=-1)
... return metric.compute(predictions=predictions, references=labels)
```
๋ฏธ์ธ ํ๋ ์ค์ ํ๊ฐ ์งํ๋ฅผ ๋ชจ๋ํฐ๋งํ๋ ค๋ฉด ํ๋ จ ์ธ์์ `evaluation_strategy` ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ํ์ฌ ๊ฐ ์ํญ์ด ๋๋ ๋ ํ๊ฐ ์งํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค:
```py
>>> from transformers import TrainingArguments, Trainer
>>> training_args = TrainingArguments(output_dir="test_trainer", evaluation_strategy="epoch")
```
### ํ๋ จ ํ๊ธฐ[[trainer]]
๋ชจ๋ธ, ํ๋ จ ์ธ์, ํ๋ จ ๋ฐ ํ
์คํธ ๋ฐ์ดํฐ์
, ํ๊ฐ ํจ์๊ฐ ํฌํจ๋ [`Trainer`] ๊ฐ์ฒด๋ฅผ ๋ง๋ญ๋๋ค:
```py
>>> trainer = Trainer(
... model=model,
... args=training_args,
... train_dataset=small_train_dataset,
... eval_dataset=small_eval_dataset,
... compute_metrics=compute_metrics,
... )
```
๊ทธ๋ฆฌ๊ณ [`~transformers.Trainer.train`]์ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํฉ๋๋ค:
```py
>>> trainer.train()
```
</pt>
<tf>
<a id='keras'></a>
<Youtube id="rnTGBy2ax1c"/>
## Keras๋ก ํ
์ํ๋ก์ฐ ๋ชจ๋ธ ํ๋ จํ๊ธฐ[[train-a-tensorflow-model-with-keras]]
Keras API๋ฅผ ์ฌ์ฉํ์ฌ ํ
์ํ๋ก์ฐ์์ ๐ค Transformers ๋ชจ๋ธ์ ํ๋ จํ ์๋ ์์ต๋๋ค!
### Keras์ฉ ๋ฐ์ดํฐ ๋ก๋[[loading-data-for-keras]]
Keras API๋ก ๐ค Transformers ๋ชจ๋ธ์ ํ์ต์ํค๋ ค๋ฉด ๋ฐ์ดํฐ์
์ Keras๊ฐ ์ดํดํ ์ ์๋ ํ์์ผ๋ก ๋ณํํด์ผ ํฉ๋๋ค.
๋ฐ์ดํฐ ์ธํธ๊ฐ ์์ ๊ฒฝ์ฐ, ์ ์ฒด๋ฅผ NumPy ๋ฐฐ์ด๋ก ๋ณํํ์ฌ Keras๋ก ์ ๋ฌํ๋ฉด ๋ฉ๋๋ค.
๋ ๋ณต์กํ ์์
์ ์ํํ๊ธฐ ์ ์ ๋จผ์ ์ด ์์
์ ์๋ํด ๋ณด๊ฒ ์ต๋๋ค.
๋จผ์ ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ก๋ํฉ๋๋ค. [GLUE ๋ฒค์น๋งํฌ](https://huggingface.co/datasets/glue)์ CoLA ๋ฐ์ดํฐ ์ธํธ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
๊ฐ๋จํ ๋ฐ์ด๋๋ฆฌ ํ
์คํธ ๋ถ๋ฅ ์์
์ด๋ฏ๋ก ์ง๊ธ์ ํ๋ จ ๋ฐ์ดํฐ ๋ถํ ๋ง ์ฌ์ฉํฉ๋๋ค.
```py
from datasets import load_dataset
dataset = load_dataset("glue", "cola")
dataset = dataset["train"] # Just take the training split for now
```
๋ค์์ผ๋ก ํ ํฌ๋์ด์ ๋ฅผ ๋ก๋ํ๊ณ ๋ฐ์ดํฐ๋ฅผ NumPy ๋ฐฐ์ด๋ก ํ ํฐํํฉ๋๋ค. ๋ ์ด๋ธ์ ์ด๋ฏธ 0๊ณผ 1๋ก ๋ ๋ฆฌ์คํธ์ด๊ธฐ ๋๋ฌธ์ ํ ํฐํํ์ง ์๊ณ ๋ฐ๋ก NumPy ๋ฐฐ์ด๋ก ๋ณํํ ์ ์์ต๋๋ค!
```py
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
tokenized_data = tokenizer(dataset["sentence"], return_tensors="np", padding=True)
# Tokenizer returns a BatchEncoding, but we convert that to a dict for Keras
tokenized_data = dict(tokenized_data)
labels = np.array(dataset["label"]) # Label is already an array of 0 and 1
```
๋ง์ง๋ง์ผ๋ก ๋ชจ๋ธ์ ๋ก๋, [`compile`](https://keras.io/api/models/model_training_apis/#compile-method), [`fit`](https://keras.io/api/models/model_training_apis/#fit-method)ํฉ๋๋ค:
```py
from transformers import TFAutoModelForSequenceClassification
from tensorflow.keras.optimizers import Adam
# Load and compile our model
model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-cased")
# Lower learning rates are often better for fine-tuning transformers
model.compile(optimizer=Adam(3e-5))
model.fit(tokenized_data, labels)
```
<Tip>
๋ชจ๋ธ์ `compile()`ํ ๋ ์์ค ์ธ์๋ฅผ ๋ชจ๋ธ์ ์ ๋ฌํ ํ์๊ฐ ์์ต๋๋ค!
์ด ์ธ์๋ฅผ ๋น์๋๋ฉด ํ๊น
ํ์ด์ค ๋ชจ๋ธ์ ์์
๊ณผ ๋ชจ๋ธ ์ํคํ
์ฒ์ ์ ํฉํ ์์ค์ ์๋์ผ๋ก ์ ํํฉ๋๋ค.
์ํ๋ค๋ฉด ์ธ์ ๋ ์ง ์ง์ ์์ค์ ์ง์ ํ์ฌ ์ด๋ฅผ ์ฌ์ ์ํ ์ ์์ต๋๋ค!
</Tip>
์ด ์ ๊ทผ ๋ฐฉ์์ ์๊ท๋ชจ ๋ฐ์ดํฐ ์งํฉ์์๋ ์ ์๋ํ์ง๋ง, ๋๊ท๋ชจ ๋ฐ์ดํฐ ์งํฉ์์๋ ๋ฌธ์ ๊ฐ ๋ ์ ์์ต๋๋ค. ์ ๊ทธ๋ด๊น์?
ํ ํฐํ๋ ๋ฐฐ์ด๊ณผ ๋ ์ด๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ์์ ํ ๋ก๋ํ๊ณ NumPy๋ "๋ค์ญ๋ ์ญํ" ๋ฐฐ์ด์ ์ฒ๋ฆฌํ์ง ์๊ธฐ ๋๋ฌธ์,
๋ชจ๋ ํ ํฐํ๋ ์ํ์ ์ ์ฒด ๋ฐ์ดํฐ์
์์ ๊ฐ์ฅ ๊ธด ์ํ์ ๊ธธ์ด๋งํผ ํจ๋ฉํด์ผ ํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋ฐฐ์ด์ด ํจ์ฌ ๋ ์ปค์ง๊ณ ์ด ํจ๋ฉ ํ ํฐ์ผ๋ก ์ธํด ํ์ต ์๋๋ ๋๋ ค์ง๋๋ค!
### ๋ฐ์ดํฐ๋ฅผ tf.data.Dataset์ผ๋ก ๋ก๋ํ๊ธฐ[[loading-data-as-a-tfdatadataset]]
ํ์ต ์๋๊ฐ ๋๋ ค์ง๋ ๊ฒ์ ํผํ๋ ค๋ฉด ๋ฐ์ดํฐ๋ฅผ `tf.data.Dataset`์ผ๋ก ๋ก๋ํ ์ ์์ต๋๋ค. ์ํ๋ค๋ฉด ์ง์
`tf.data` ํ์ดํ๋ผ์ธ์ ์ง์ ์์ฑํ ์๋ ์์ง๋ง, ์ด ์์
์ ๊ฐํธํ๊ฒ ์ํํ๋ ์ ์๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค:
- [`~TFPreTrainedModel.prepare_tf_dataset`]: ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ์ด ๋ฐฉ๋ฒ์ ๊ถ์ฅํฉ๋๋ค. ๋ชจ๋ธ์ ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์ ๋ชจ๋ธ์ ๊ฒ์ฌํ์ฌ ๋ชจ๋ธ ์
๋ ฅ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ด์ ์๋์ผ๋ก ํ์
ํ๊ณ
๋๋จธ์ง๋ ๋ฒ๋ ค์ ๋ ๋จ์ํ๊ณ ์ฑ๋ฅ์ด ์ข์ ๋ฐ์ดํฐ ์งํฉ์ ๋ง๋ค ์ ์์ต๋๋ค.
- [`~datasets.Dataset.to_tf_dataset`]: ์ด ๋ฐฉ๋ฒ์ ์ข ๋ ๋ฎ์ ์์ค์ด๋ฉฐ, ํฌํจํ '์ด'๊ณผ '๋ ์ด๋ธ'์ ์ ํํ ์ง์ ํ์ฌ
๋ฐ์ดํฐ์
์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ ํํ ์ ์ดํ๊ณ ์ถ์ ๋ ์ ์ฉํ๋ฉฐ, ํฌํจํ 'columns'๊ณผ 'label_cols'์ ์ ํํ ์ง์ ํ ์ ์์ต๋๋ค.
[`~TFPreTrainedModel.prepare_tf_dataset`]์ ์ฌ์ฉํ๋ ค๋ฉด ๋จผ์ ๋ค์ ์ฝ๋ ์ํ๊ณผ ๊ฐ์ด ํ ํฌ๋์ด์ ์ถ๋ ฅ์ ๋ฐ์ดํฐ ์ธํธ์ ์ด๋ก ์ถ๊ฐํด์ผ ํฉ๋๋ค:
```py
def tokenize_dataset(data):
# Keys of the returned dictionary will be added to the dataset as columns
return tokenizer(data["text"])
dataset = dataset.map(tokenize_dataset)
```
ํ๊น
ํ์ด์ค ๋ฐ์ดํฐ์
์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋์คํฌ์ ์ ์ฅ๋๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๋๋ฆฌ์ง ์๋๋ค๋ ์ ์ ๊ธฐ์ตํ์ธ์!
์ด์ด ์ถ๊ฐ๋๋ฉด ๋ฐ์ดํฐ์
์์ ๋ฐฐ์น๋ฅผ ์คํธ๋ฆฌ๋ฐํ๊ณ ๊ฐ ๋ฐฐ์น์ ํจ๋ฉ์ ์ถ๊ฐํ ์ ์์ผ๋ฏ๋ก ์ ์ฒด ๋ฐ์ดํฐ์
์ ํจ๋ฉ์ ์ถ๊ฐํ๋ ๊ฒ๋ณด๋ค ํจ๋ฉ ํ ํฐ์ ์๋ฅผ ํฌ๊ฒ ์ค์ผ ์ ์์ต๋๋ค.
```py
>>> tf_dataset = model.prepare_tf_dataset(dataset, batch_size=16, shuffle=True, tokenizer=tokenizer)
```
์์ ์ฝ๋ ์ํ์์๋ ๋ฐฐ์น๊ฐ ๋ก๋๋ ๋ ์ฌ๋ฐ๋ฅด๊ฒ ํจ๋ฉํ ์ ์๋๋ก `prepare_tf_dataset`์ ํ ํฌ๋์ด์ ๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
๋ฐ์ดํฐ์
์ ๋ชจ๋ ์ํ ๊ธธ์ด๊ฐ ๊ฐ๊ณ ํจ๋ฉ์ด ํ์ํ์ง ์์ ๊ฒฝ์ฐ ์ด ์ธ์๋ฅผ ๊ฑด๋๋ธ ์ ์์ต๋๋ค.
์ํ์ ์ฑ์ฐ๋ ๊ฒ๋ณด๋ค ๋ ๋ณต์กํ ์์
(์: ๋ง์คํน๋ ์ธ์ด์ ํ ํฐ ์์ ๋ชจ๋ธ๋ง)์ ์ํํ๊ธฐ ์ํด ํ ํฐ์ ์์์์ผ์ผ ํ๋ ๊ฒฝ์ฐ,
`collate_fn` ์ธ์๋ฅผ ์ฌ์ฉํ์ฌ ์ํ ๋ชฉ๋ก์ ๋ฐฐ์น๋ก ๋ณํํ๊ณ ์ํ๋ ์ ์ฒ๋ฆฌ๋ฅผ ์ ์ฉํ ํจ์๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค.
[์์](https://github.com/huggingface/transformers/tree/main/examples) ๋๋
[๋
ธํธ๋ถ](https://huggingface.co/docs/transformers/notebooks)์ ์ฐธ์กฐํ์ฌ ์ด ์ ๊ทผ ๋ฐฉ์์ด ์ค์ ๋ก ์๋ํ๋ ๋ชจ์ต์ ํ์ธํ์ธ์.
`tf.data.Dataset`์ ์์ฑํ ํ์๋ ์ด์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ชจ๋ธ์ ์ปดํ์ผํ๊ณ ํ๋ จ(fit)ํ ์ ์์ต๋๋ค:
```py
model.compile(optimizer=Adam(3e-5))
model.fit(tf_dataset)
```
</tf>
</frameworkcontent>
<a id='pytorch_native'></a>
## ๊ธฐ๋ณธ ํ์ดํ ์น๋ก ํ๋ จํ๊ธฐ[[train-in-native-pytorch]]
<frameworkcontent>
<pt>
<Youtube id="Dh9CL8fyG80"/>
[`Trainer`]๋ ํ๋ จ ๋ฃจํ๋ฅผ ์ฒ๋ฆฌํ๋ฉฐ ํ ์ค์ ์ฝ๋๋ก ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ ์ ์์ต๋๋ค. ์ง์ ํ๋ จ ๋ฃจํ๋ฅผ ์์ฑํ๋ ๊ฒ์ ์ ํธํ๋ ์ฌ์ฉ์์ ๊ฒฝ์ฐ, ๊ธฐ๋ณธ PyTorch์์ ๐ค Transformers ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ ์๋ ์์ต๋๋ค.
์ด ์์ ์์ ๋
ธํธ๋ถ์ ๋ค์ ์์ํ๊ฑฐ๋ ๋ค์ ์ฝ๋๋ฅผ ์คํํด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋ณดํด์ผ ํ ์ ์์ต๋๋ค:
```py
del model
del trainer
torch.cuda.empty_cache()
```
๋ค์์ผ๋ก, 'ํ ํฐํ๋ ๋ฐ์ดํฐ์
'์ ์๋์ผ๋ก ํ์ฒ๋ฆฌํ์ฌ ํ๋ จ๋ จ์ ์ฌ์ฉํ ์ ์๋๋ก ์ค๋นํฉ๋๋ค.
1. ๋ชจ๋ธ์ด ์์ ํ
์คํธ๋ฅผ ์
๋ ฅ์ผ๋ก ํ์ฉํ์ง ์์ผ๋ฏ๋ก `text` ์ด์ ์ ๊ฑฐํฉ๋๋ค:
```py
>>> tokenized_datasets = tokenized_datasets.remove_columns(["text"])
```
2. ๋ชจ๋ธ์์ ์ธ์์ ์ด๋ฆ์ด `labels`๋ก ์ง์ ๋ ๊ฒ์ผ๋ก ์์ํ๋ฏ๋ก `label` ์ด์ ์ด๋ฆ์ `labels`๋ก ๋ณ๊ฒฝํฉ๋๋ค:
```py
>>> tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
```
3. ๋ฐ์ดํฐ์
์ ํ์์ List ๋์ PyTorch ํ
์๋ฅผ ๋ฐํํ๋๋ก ์ค์ ํฉ๋๋ค:
```py
>>> tokenized_datasets.set_format("torch")
```
๊ทธ๋ฆฌ๊ณ ์์ ํ์๋ ๋๋ก ๋ฐ์ดํฐ์
์ ๋ ์์ ํ์ ์งํฉ์ ์์ฑํ์ฌ ๋ฏธ์ธ ์กฐ์ ์๋๋ฅผ ๋์
๋๋ค:
```py
>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000))
>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000))
```
### DataLoader[[dataloader]]
ํ๋ จ ๋ฐ ํ
์คํธ ๋ฐ์ดํฐ์
์ ๋ํ 'DataLoader'๋ฅผ ์์ฑํ์ฌ ๋ฐ์ดํฐ ๋ฐฐ์น๋ฅผ ๋ฐ๋ณตํ ์ ์์ต๋๋ค:
```py
>>> from torch.utils.data import DataLoader
>>> train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8)
>>> eval_dataloader = DataLoader(small_eval_dataset, batch_size=8)
```
์์ธก์ ์ํ ๋ ์ด๋ธ ๊ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค:
```py
>>> from transformers import AutoModelForSequenceClassification
>>> model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
```
### ์ตํฐ๋ง์ด์ ๋ฐ ํ์ต ์๋ ์ค์ผ์ค๋ฌ[[optimizer-and-learning-rate-scheduler]]
์ตํฐ๋ง์ด์ ์ ํ์ต ์๋ ์ค์ผ์ค๋ฌ๋ฅผ ์์ฑํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํฉ๋๋ค. ํ์ดํ ์น์์ ์ ๊ณตํ๋ [`AdamW`](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) ์ตํฐ๋ง์ด์ ๋ฅผ ์ฌ์ฉํด ๋ณด๊ฒ ์ต๋๋ค:
```py
>>> from torch.optim import AdamW
>>> optimizer = AdamW(model.parameters(), lr=5e-5)
```
[`Trainer`]์์ ๊ธฐ๋ณธ ํ์ต ์๋ ์ค์ผ์ค๋ฌ๋ฅผ ์์ฑํฉ๋๋ค:
```py
>>> from transformers import get_scheduler
>>> num_epochs = 3
>>> num_training_steps = num_epochs * len(train_dataloader)
>>> lr_scheduler = get_scheduler(
... name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps
... )
```
๋ง์ง๋ง์ผ๋ก, GPU์ ์ก์ธ์คํ ์ ์๋ ๊ฒฝ์ฐ 'device'๋ฅผ ์ง์ ํ์ฌ GPU๋ฅผ ์ฌ์ฉํ๋๋ก ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด CPU์์ ํ๋ จํ๋ฉฐ ๋ช ๋ถ์ด ์๋ ๋ช ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
```py
>>> import torch
>>> device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
>>> model.to(device)
```
<Tip>
[Colaboratory](https://colab.research.google.com/) ๋๋ [SageMaker StudioLab](https://studiolab.sagemaker.aws/)๊ณผ ๊ฐ์ ํธ์คํ
๋
ธํธ๋ถ์ด ์๋ ๊ฒฝ์ฐ ํด๋ผ์ฐ๋ GPU์ ๋ฌด๋ฃ๋ก ์ก์ธ์คํ ์ ์์ต๋๋ค.
</Tip>
์ด์ ํ๋ จํ ์ค๋น๊ฐ ๋์์ต๋๋ค! ๐ฅณ
### ํ๋ จ ๋ฃจํ[[training-loop]]
ํ๋ จ ์งํ ์ํฉ์ ์ถ์ ํ๋ ค๋ฉด [tqdm](https://tqdm.github.io/) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ํธ๋ ์ด๋ ๋จ๊ณ ์์ ์งํ๋ฅ ํ์์ค์ ์ถ๊ฐํ์ธ์:
```py
>>> from tqdm.auto import tqdm
>>> progress_bar = tqdm(range(num_training_steps))
>>> model.train()
>>> for epoch in range(num_epochs):
... for batch in train_dataloader:
... batch = {k: v.to(device) for k, v in batch.items()}
... outputs = model(**batch)
... loss = outputs.loss
... loss.backward()
... optimizer.step()
... lr_scheduler.step()
... optimizer.zero_grad()
... progress_bar.update(1)
```
### ํ๊ฐ ํ๊ธฐ[[evaluate]]
[`Trainer`]์ ํ๊ฐ ํจ์๋ฅผ ์ถ๊ฐํ ๋ฐฉ๋ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก, ํ๋ จ ๋ฃจํ๋ฅผ ์ง์ ์์ฑํ ๋๋ ๋์ผํ ์์
์ ์ํํด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ด๋ฒ์๋ ๊ฐ ์ํฌํฌ๊ฐ ๋๋ ๋๋ง๋ค ํ๊ฐ์งํ๋ฅผ ๊ณ์ฐํ์ฌ ๋ณด๊ณ ํ๋ ๋์ , [`~evaluate.add_batch`]๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๋ฐฐ์น๋ฅผ ๋์ ํ๊ณ ๋งจ ๋ง์ง๋ง์ ํ๊ฐ์งํ๋ฅผ ๊ณ์ฐํฉ๋๋ค.
```py
>>> import evaluate
>>> metric = evaluate.load("accuracy")
>>> model.eval()
>>> for batch in eval_dataloader:
... batch = {k: v.to(device) for k, v in batch.items()}
... with torch.no_grad():
... outputs = model(**batch)
... logits = outputs.logits
... predictions = torch.argmax(logits, dim=-1)
... metric.add_batch(predictions=predictions, references=batch["labels"])
>>> metric.compute()
```
</pt>
</frameworkcontent>
<a id='additional-resources'></a>
## ์ถ๊ฐ ์๋ฃ[[additional-resources]]
๋ ๋ง์ ๋ฏธ์ธ ํ๋ ์์ ๋ ๋ค์์ ์ฐธ์กฐํ์ธ์:
- [๐ค Trnasformers ์์ ](https://github.com/huggingface/transformers/tree/main/examples)์๋ PyTorch ๋ฐ ํ
์ํ๋ก์ฐ์์ ์ผ๋ฐ์ ์ธ NLP ์์
์ ํ๋ จํ ์ ์๋ ์คํฌ๋ฆฝํธ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
- [๐ค Transformers ๋
ธํธ๋ถ](notebooks)์๋ PyTorch ๋ฐ ํ
์ํ๋ก์ฐ์์ ํน์ ์์
์ ์ํด ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ค์ํ ๋
ธํธ๋ถ์ด ํฌํจ๋์ด ์์ต๋๋ค.
|