Spaces:
Paused
Paused
File size: 17,848 Bytes
ee6e328 |
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 |
<!--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.
-->
# ํ
์คํธ ๋ถ๋ฅ[[text-classification]]
[[open-in-colab]]
<Youtube id="leNG9fN9FQU"/>
ํ
์คํธ ๋ถ๋ฅ๋ ์์ฐ์ด ์ฒ๋ฆฌ์ ์ผ์ข
์ผ๋ก, ํ
์คํธ์ ๋ ์ด๋ธ ๋๋ ํด๋์ค๋ฅผ ์ง์ ํ๋ ์์
์
๋๋ค. ๋ง์ ๋๊ธฐ์
์ด ๋ค์ํ ์ค์ฉ์ ์ธ ์์ฉ ๋ถ์ผ์์ ํ
์คํธ ๋ถ๋ฅ๋ฅผ ์ด์ํ๊ณ ์์ต๋๋ค. ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ํ
์คํธ ๋ถ๋ฅ ํํ ์ค ํ๋๋ ๊ฐ์ฑ ๋ถ์์ผ๋ก, ํ
์คํธ ์ํ์ค์ ๐ ๊ธ์ , ๐ ๋ถ์ ๋๋ ๐ ์ค๋ฆฝ๊ณผ ๊ฐ์ ๋ ์ด๋ธ์ ์ง์ ํฉ๋๋ค.
์ด ๊ฐ์ด๋์์ ํ์ตํ ๋ด์ฉ์:
1. [IMDb](https://huggingface.co/datasets/imdb) ๋ฐ์ดํฐ์
์์ [DistilBERT](https://huggingface.co/distilbert-base-uncased)๋ฅผ ํ์ธ ํ๋ํ์ฌ ์ํ ๋ฆฌ๋ทฐ๊ฐ ๊ธ์ ์ ์ธ์ง ๋ถ์ ์ ์ธ์ง ํ๋จํฉ๋๋ค.
2. ์ถ๋ก ์ ์ํด ํ์ธ ํ๋ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
<Tip>
์ด ํํ ๋ฆฌ์ผ์์ ์ค๋ช
ํ๋ ์์
์ ๋ค์ ๋ชจ๋ธ ์ํคํ
์ฒ์ ์ํด ์ง์๋ฉ๋๋ค:
<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->
[ALBERT](../model_doc/albert), [BART](../model_doc/bart), [BERT](../model_doc/bert), [BigBird](../model_doc/big_bird), [BigBird-Pegasus](../model_doc/bigbird_pegasus), [BLOOM](../model_doc/bloom), [CamemBERT](../model_doc/camembert), [CANINE](../model_doc/canine), [ConvBERT](../model_doc/convbert), [CTRL](../model_doc/ctrl), [Data2VecText](../model_doc/data2vec-text), [DeBERTa](../model_doc/deberta), [DeBERTa-v2](../model_doc/deberta-v2), [DistilBERT](../model_doc/distilbert), [ELECTRA](../model_doc/electra), [ERNIE](../model_doc/ernie), [ErnieM](../model_doc/ernie_m), [ESM](../model_doc/esm), [FlauBERT](../model_doc/flaubert), [FNet](../model_doc/fnet), [Funnel Transformer](../model_doc/funnel), [GPT-Sw3](../model_doc/gpt-sw3), [OpenAI GPT-2](../model_doc/gpt2), [GPT Neo](../model_doc/gpt_neo), [GPT-J](../model_doc/gptj), [I-BERT](../model_doc/ibert), [LayoutLM](../model_doc/layoutlm), [LayoutLMv2](../model_doc/layoutlmv2), [LayoutLMv3](../model_doc/layoutlmv3), [LED](../model_doc/led), [LiLT](../model_doc/lilt), [LLaMA](../model_doc/llama), [Longformer](../model_doc/longformer), [LUKE](../model_doc/luke), [MarkupLM](../model_doc/markuplm), [mBART](../model_doc/mbart), [MEGA](../model_doc/mega), [Megatron-BERT](../model_doc/megatron-bert), [MobileBERT](../model_doc/mobilebert), [MPNet](../model_doc/mpnet), [MVP](../model_doc/mvp), [Nezha](../model_doc/nezha), [Nystrรถmformer](../model_doc/nystromformer), [OpenAI GPT](../model_doc/openai-gpt), [OPT](../model_doc/opt), [Perceiver](../model_doc/perceiver), [PLBart](../model_doc/plbart), [QDQBert](../model_doc/qdqbert), [Reformer](../model_doc/reformer), [RemBERT](../model_doc/rembert), [RoBERTa](../model_doc/roberta), [RoBERTa-PreLayerNorm](../model_doc/roberta-prelayernorm), [RoCBert](../model_doc/roc_bert), [RoFormer](../model_doc/roformer), [SqueezeBERT](../model_doc/squeezebert), [TAPAS](../model_doc/tapas), [Transformer-XL](../model_doc/transfo-xl), [XLM](../model_doc/xlm), [XLM-RoBERTa](../model_doc/xlm-roberta), [XLM-RoBERTa-XL](../model_doc/xlm-roberta-xl), [XLNet](../model_doc/xlnet), [X-MOD](../model_doc/xmod), [YOSO](../model_doc/yoso)
<!--End of the generated tip-->
</Tip>
์์ํ๊ธฐ ์ ์, ํ์ํ ๋ชจ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
```bash
pip install transformers datasets evaluate
```
Hugging Face ๊ณ์ ์ ๋ก๊ทธ์ธํ์ฌ ๋ชจ๋ธ์ ์
๋ก๋ํ๊ณ ์ปค๋ฎค๋ํฐ์ ๊ณต์ ํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ๋ฉ์์ง๊ฐ ํ์๋๋ฉด, ํ ํฐ์ ์
๋ ฅํ์ฌ ๋ก๊ทธ์ธํ์ธ์:
```py
>>> from huggingface_hub import notebook_login
>>> notebook_login()
```
## IMDb ๋ฐ์ดํฐ์
๊ฐ์ ธ์ค๊ธฐ[[load-imdb-dataset]]
๋จผ์ ๐ค Datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ IMDb ๋ฐ์ดํฐ์
์ ๊ฐ์ ธ์ต๋๋ค:
```py
>>> from datasets import load_dataset
>>> imdb = load_dataset("imdb")
```
๊ทธ๋ฐ ๋ค์ ์์๋ฅผ ์ดํด๋ด
์๋ค:
```py
>>> imdb["test"][0]
{
"label": 0,
"text": "I love sci-fi and am willing to put up with a lot. Sci-fi movies/TV are usually underfunded, under-appreciated and misunderstood. I tried to like this, I really did, but it is to good TV sci-fi as Babylon 5 is to Star Trek (the original). Silly prosthetics, cheap cardboard sets, stilted dialogues, CG that doesn't match the background, and painfully one-dimensional characters cannot be overcome with a 'sci-fi' setting. (I'm sure there are those of you out there who think Babylon 5 is good sci-fi TV. It's not. It's clichรฉd and uninspiring.) While US viewers might like emotion and character development, sci-fi is a genre that does not take itself seriously (cf. Star Trek). It may treat important issues, yet not as a serious philosophy. It's really difficult to care about the characters here as they are not simply foolish, just missing a spark of life. Their actions and reactions are wooden and predictable, often painful to watch. The makers of Earth KNOW it's rubbish as they have to always say \"Gene Roddenberry's Earth...\" otherwise people would not continue watching. Roddenberry's ashes must be turning in their orbit as this dull, cheap, poorly edited (watching it without advert breaks really brings this home) trudging Trabant of a show lumbers into space. Spoiler. So, kill off a main character. And then bring him back as another actor. Jeeez! Dallas all over again.",
}
```
์ด ๋ฐ์ดํฐ์
์๋ ๋ ๊ฐ์ง ํ๋๊ฐ ์์ต๋๋ค:
- `text`: ์ํ ๋ฆฌ๋ทฐ ํ
์คํธ
- `label`: `0`์ ๋ถ์ ์ ์ธ ๋ฆฌ๋ทฐ, `1`์ ๊ธ์ ์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋ํ๋
๋๋ค.
## ์ ์ฒ๋ฆฌ[[preprocess]]
๋ค์ ๋จ๊ณ๋ DistilBERT ํ ํฌ๋์ด์ ๋ฅผ ๊ฐ์ ธ์์ `text` ํ๋๋ฅผ ์ ์ฒ๋ฆฌํ๋ ๊ฒ์
๋๋ค:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
```
`text`๋ฅผ ํ ํฐํํ๊ณ ์ํ์ค๊ฐ DistilBERT์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ณด๋ค ๊ธธ์ง ์๋๋ก ์๋ฅด๊ธฐ ์ํ ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์์ฑํ์ธ์:
```py
>>> def preprocess_function(examples):
... return tokenizer(examples["text"], truncation=True)
```
์ ์ฒด ๋ฐ์ดํฐ์
์ ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ ์ฉํ๋ ค๋ฉด, ๐ค Datasets [`~datasets.Dataset.map`] ํจ์๋ฅผ ์ฌ์ฉํ์ธ์. ๋ฐ์ดํฐ์
์ ์ฌ๋ฌ ์์๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด `batched=True`๋ก ์ค์ ํจ์ผ๋ก์จ ๋ฐ์ดํฐ์
`map`๋ฅผ ๋ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค:
```py
tokenized_imdb = imdb.map(preprocess_function, batched=True)
```
์ด์ [`DataCollatorWithPadding`]๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ฐฐ์น๋ฅผ ๋ง๋ค์ด๋ด
์๋ค. ๋ฐ์ดํฐ์
์ ์ฒด๋ฅผ ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉํ๋ ๋์ , *๋์ ํจ๋ฉ*์ ์ฌ์ฉํ์ฌ ๋ฐฐ์น์์ ๊ฐ์ฅ ๊ธด ๊ธธ์ด์ ๋ง๊ฒ ๋ฌธ์ฅ์ ํจ๋ฉํ๋ ๊ฒ์ด ํจ์จ์ ์
๋๋ค.
<frameworkcontent>
<pt>
```py
>>> from transformers import DataCollatorWithPadding
>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
```
</pt>
<tf>
```py
>>> from transformers import DataCollatorWithPadding
>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors="tf")
```
</tf>
</frameworkcontent>
## ํ๊ฐํ๊ธฐ[[evaluate]]
ํ๋ จ ์ค ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํ๊ฐํ๊ธฐ ์ํด ๋ฉํธ๋ฆญ์ ํฌํจํ๋ ๊ฒ์ด ์ ์ฉํฉ๋๋ค. ๐ค [Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋น ๋ฅด๊ฒ ํ๊ฐ ๋ฐฉ๋ฒ์ ๋ก๋ํ ์ ์์ต๋๋ค. ์ด ์์
์์๋ [accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ๋ฉํธ๋ฆญ์ ๊ฐ์ ธ์ต๋๋ค. (๋ฉํธ๋ฆญ์ ๊ฐ์ ธ์ค๊ณ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ๋ํด์๋ ๐ค Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ์ธ์):
```py
>>> import evaluate
>>> accuracy = evaluate.load("accuracy")
```
๊ทธ๋ฐ ๋ค์ `compute_metrics` ํจ์๋ฅผ ๋ง๋ค์ด์ ์์ธก๊ณผ ๋ ์ด๋ธ์ ๊ณ์ฐํ์ฌ ์ ํ๋๋ฅผ ๊ณ์ฐํ๋๋ก [`~evaluate.EvaluationModule.compute`]๋ฅผ ํธ์ถํฉ๋๋ค:
```py
>>> import numpy as np
>>> def compute_metrics(eval_pred):
... predictions, labels = eval_pred
... predictions = np.argmax(predictions, axis=1)
... return accuracy.compute(predictions=predictions, references=labels)
```
์ด์ `compute_metrics` ํจ์๋ ์ค๋น๋์๊ณ , ํ๋ จ ๊ณผ์ ์ ์ค์ ํ ๋ ๋ค์ ์ดํด๋ณผ ์์ ์
๋๋ค.
## ํ๋ จ[[train]]
๋ชจ๋ธ์ ํ๋ จํ๊ธฐ ์ ์, `id2label`์ `label2id`๋ฅผ ์ฌ์ฉํ์ฌ ์์๋๋ id์ ๋ ์ด๋ธ์ ๋งต์ ์์ฑํ์ธ์:
```py
>>> id2label = {0: "NEGATIVE", 1: "POSITIVE"}
>>> label2id = {"NEGATIVE": 0, "POSITIVE": 1}
```
<frameworkcontent>
<pt>
<Tip>
[`Trainer`]๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ์ต์ํ์ง ์์ ๊ฒฝ์ฐ, [์ฌ๊ธฐ](../training#train-with-pytorch-trainer)์ ๊ธฐ๋ณธ ํํ ๋ฆฌ์ผ์ ํ์ธํ์ธ์!
</Tip>
์ด์ ๋ชจ๋ธ์ ํ๋ จ์ํฌ ์ค๋น๊ฐ ๋์์ต๋๋ค! [`AutoModelForSequenceClassification`]๋ก DistilBERT๋ฅผ ๊ฐ์ณ์ค๊ณ ์์๋๋ ๋ ์ด๋ธ ์์ ๋ ์ด๋ธ ๋งคํ์ ์ง์ ํ์ธ์:
```py
>>> from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
>>> model = AutoModelForSequenceClassification.from_pretrained(
... "distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id
... )
```
์ด์ ์ธ ๋จ๊ณ๋ง ๊ฑฐ์น๋ฉด ๋์
๋๋ค:
1. [`TrainingArguments`]์์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ์ธ์. `output_dir`๋ ๋ชจ๋ธ์ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํ๋ ์ ์ผํ ํ๋ผ๋ฏธํฐ์
๋๋ค. ์ด ๋ชจ๋ธ์ Hub์ ์
๋ก๋ํ๊ธฐ ์ํด `push_to_hub=True`๋ฅผ ์ค์ ํฉ๋๋ค. (๋ชจ๋ธ์ ์
๋ก๋ํ๊ธฐ ์ํด Hugging Face์ ๋ก๊ทธ์ธํด์ผํฉ๋๋ค.) ๊ฐ ์ํญ์ด ๋๋ ๋๋ง๋ค, [`Trainer`]๋ ์ ํ๋๋ฅผ ํ๊ฐํ๊ณ ํ๋ จ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ ์ฅํฉ๋๋ค.
2. [`Trainer`]์ ํ๋ จ ์ธ์์ ๋ชจ๋ธ, ๋ฐ์ดํฐ์
, ํ ํฌ๋์ด์ , ๋ฐ์ดํฐ ์์ง๊ธฐ ๋ฐ `compute_metrics` ํจ์๋ฅผ ์ ๋ฌํ์ธ์.
3. [`~Trainer.train`]๋ฅผ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ์ธ์.
```py
>>> training_args = TrainingArguments(
... output_dir="my_awesome_model",
... learning_rate=2e-5,
... per_device_train_batch_size=16,
... per_device_eval_batch_size=16,
... num_train_epochs=2,
... weight_decay=0.01,
... evaluation_strategy="epoch",
... save_strategy="epoch",
... load_best_model_at_end=True,
... push_to_hub=True,
... )
>>> trainer = Trainer(
... model=model,
... args=training_args,
... train_dataset=tokenized_imdb["train"],
... eval_dataset=tokenized_imdb["test"],
... tokenizer=tokenizer,
... data_collator=data_collator,
... compute_metrics=compute_metrics,
... )
>>> trainer.train()
```
<Tip>
[`Trainer`]๋ `tokenizer`๋ฅผ ์ ๋ฌํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ๋์ ๋งคํ์ ์ ์ฉํฉ๋๋ค. ์ด ๊ฒฝ์ฐ, ๋ช
์์ ์ผ๋ก ๋ฐ์ดํฐ ์์ง๊ธฐ๋ฅผ ์ง์ ํ ํ์๊ฐ ์์ต๋๋ค.
</Tip>
ํ๋ จ์ด ์๋ฃ๋๋ฉด, [`~transformers.Trainer.push_to_hub`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ Hub์ ๊ณต์ ํ ์ ์์ต๋๋ค.
```py
>>> trainer.push_to_hub()
```
</pt>
<tf>
<Tip>
Keras๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ์ต์ํ์ง ์์ ๊ฒฝ์ฐ, [์ฌ๊ธฐ](../training#train-a-tensorflow-model-with-keras)์ ๊ธฐ๋ณธ ํํ ๋ฆฌ์ผ์ ํ์ธํ์ธ์!
</Tip>
TensorFlow์์ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ๋ ค๋ฉด, ๋จผ์ ์ตํฐ๋ง์ด์ ํจ์์ ํ์ต๋ฅ ์ค์ผ์ฅด, ๊ทธ๋ฆฌ๊ณ ์ผ๋ถ ํ๋ จ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค:
```py
>>> from transformers import create_optimizer
>>> import tensorflow as tf
>>> batch_size = 16
>>> num_epochs = 5
>>> batches_per_epoch = len(tokenized_imdb["train"]) // batch_size
>>> total_train_steps = int(batches_per_epoch * num_epochs)
>>> optimizer, schedule = create_optimizer(init_lr=2e-5, num_warmup_steps=0, num_train_steps=total_train_steps)
```
๊ทธ๋ฐ ๋ค์ [`TFAutoModelForSequenceClassification`]์ ์ฌ์ฉํ์ฌ DistilBERT๋ฅผ ๋ก๋ํ๊ณ , ์์๋๋ ๋ ์ด๋ธ ์์ ๋ ์ด๋ธ ๋งคํ์ ๋ก๋ํ ์ ์์ต๋๋ค:
```py
>>> from transformers import TFAutoModelForSequenceClassification
>>> model = TFAutoModelForSequenceClassification.from_pretrained(
... "distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id
... )
```
[`~transformers.TFPreTrainedModel.prepare_tf_dataset`]์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ์
์ `tf.data.Dataset` ํ์์ผ๋ก ๋ณํํฉ๋๋ค:
```py
>>> tf_train_set = model.prepare_tf_dataset(
... tokenized_imdb["train"],
... shuffle=True,
... batch_size=16,
... collate_fn=data_collator,
... )
>>> tf_validation_set = model.prepare_tf_dataset(
... tokenized_imdb["test"],
... shuffle=False,
... batch_size=16,
... collate_fn=data_collator,
... )
```
[`compile`](https://keras.io/api/models/model_training_apis/#compile-method)๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ จํ ๋ชจ๋ธ์ ๊ตฌ์ฑํฉ๋๋ค:
```py
>>> import tensorflow as tf
>>> model.compile(optimizer=optimizer)
```
ํ๋ จ์ ์์ํ๊ธฐ ์ ์ ์ค์ ํด์ผํ ๋ง์ง๋ง ๋ ๊ฐ์ง๋ ์์ธก์์ ์ ํ๋๋ฅผ ๊ณ์ฐํ๊ณ , ๋ชจ๋ธ์ Hub์ ์
๋ก๋ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ ๊ฒ์
๋๋ค. ๋ชจ๋ [Keras callbacks](../main_classes/keras_callbacks)๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ฉ๋๋ค.
[`~transformers.KerasMetricCallback`]์ `compute_metrics`๋ฅผ ์ ๋ฌํ์ฌ ์ ํ๋๋ฅผ ๋์
๋๋ค.
```py
>>> from transformers.keras_callbacks import KerasMetricCallback
>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set)
```
[`~transformers.PushToHubCallback`]์์ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ์
๋ก๋ํ ์์น๋ฅผ ์ง์ ํฉ๋๋ค:
```py
>>> from transformers.keras_callbacks import PushToHubCallback
>>> push_to_hub_callback = PushToHubCallback(
... output_dir="my_awesome_model",
... tokenizer=tokenizer,
... )
```
๊ทธ๋ฐ ๋ค์ ์ฝ๋ฐฑ์ ํจ๊ป ๋ฌถ์ต๋๋ค:
```py
>>> callbacks = [metric_callback, push_to_hub_callback]
```
๋๋์ด, ๋ชจ๋ธ ํ๋ จ์ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค! [`fit`](https://keras.io/api/models/model_training_apis/#fit-method)์ ํ๋ จ ๋ฐ์ดํฐ์
, ๊ฒ์ฆ ๋ฐ์ดํฐ์
, ์ํญ์ ์ ๋ฐ ์ฝ๋ฐฑ์ ์ ๋ฌํ์ฌ ํ์ธ ํ๋ํฉ๋๋ค:
```py
>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=callbacks)
```
ํ๋ จ์ด ์๋ฃ๋๋ฉด, ๋ชจ๋ธ์ด ์๋์ผ๋ก Hub์ ์
๋ก๋๋์ด ๋ชจ๋ ์ฌ๋์ด ์ฌ์ฉํ ์ ์์ต๋๋ค!
</tf>
</frameworkcontent>
<Tip>
ํ
์คํธ ๋ถ๋ฅ๋ฅผ ์ํ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ๋ ์์ธํ ์์ ๋ ๋ค์ [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb) ๋๋ [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)๋ฅผ ์ฐธ์กฐํ์ธ์.
</Tip>
## ์ถ๋ก [[inference]]
์ข์์, ์ด์ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ์ผ๋ ์ถ๋ก ์ ์ฌ์ฉํ ์ ์์ต๋๋ค!
์ถ๋ก ์ ์ํํ๊ณ ์ ํ๋ ํ
์คํธ๋ฅผ ๊ฐ์ ธ์๋ด
์๋ค:
```py
>>> text = "This was a masterpiece. Not completely faithful to the books, but enthralling from beginning to end. Might be my favorite of the three."
```
ํ์ธ ํ๋๋ ๋ชจ๋ธ๋ก ์ถ๋ก ์ ์๋ํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ [`pipeline`]๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ๋ชจ๋ธ๋ก ๊ฐ์ ๋ถ์์ ์ํ `pipeline`์ ์ธ์คํด์คํํ๊ณ , ํ
์คํธ๋ฅผ ์ ๋ฌํด๋ณด์ธ์:
```py
>>> from transformers import pipeline
>>> classifier = pipeline("sentiment-analysis", model="stevhliu/my_awesome_model")
>>> classifier(text)
[{'label': 'POSITIVE', 'score': 0.9994940757751465}]
```
์ํ๋ค๋ฉด, `pipeline`์ ๊ฒฐ๊ณผ๋ฅผ ์๋์ผ๋ก ๋ณต์ ํ ์๋ ์์ต๋๋ค.
<frameworkcontent>
<pt>
ํ
์คํธ๋ฅผ ํ ํฐํํ๊ณ PyTorch ํ
์๋ฅผ ๋ฐํํฉ๋๋ค.
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model")
>>> inputs = tokenizer(text, return_tensors="pt")
```
์
๋ ฅ์ ๋ชจ๋ธ์ ์ ๋ฌํ๊ณ `logits`์ ๋ฐํํฉ๋๋ค:
```py
>>> from transformers import AutoModelForSequenceClassification
>>> model = AutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model")
>>> with torch.no_grad():
... logits = model(**inputs).logits
```
๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ๋ชจ๋ธ์ `id2label` ๋งคํ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ๋ ์ด๋ธ๋ก ๋ณํํฉ๋๋ค:
```py
>>> predicted_class_id = logits.argmax().item()
>>> model.config.id2label[predicted_class_id]
'POSITIVE'
```
</pt>
<tf>
ํ
์คํธ๋ฅผ ํ ํฐํํ๊ณ TensorFlow ํ
์๋ฅผ ๋ฐํํฉ๋๋ค:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model")
>>> inputs = tokenizer(text, return_tensors="tf")
```
์
๋ ฅ๊ฐ์ ๋ชจ๋ธ์ ์ ๋ฌํ๊ณ `logits`์ ๋ฐํํฉ๋๋ค:
```py
>>> from transformers import TFAutoModelForSequenceClassification
>>> model = TFAutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model")
>>> logits = model(**inputs).logits
```
๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ๋ชจ๋ธ์ `id2label` ๋งคํ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ๋ ์ด๋ธ๋ก ๋ณํํฉ๋๋ค:
```py
>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0])
>>> model.config.id2label[predicted_class_id]
'POSITIVE'
```
</tf>
</frameworkcontent>
|