File size: 11,308 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 | <!--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/distilbert-base-uncased)๋ฅผ ํ์ธ ํ๋ํ์ฌ ์ํ ๋ฆฌ๋ทฐ๊ฐ ๊ธ์ ์ ์ธ์ง ๋ถ์ ์ ์ธ์ง ํ๋จํฉ๋๋ค.
2. ์ถ๋ก ์ ์ํด ํ์ธ ํ๋ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
<Tip>
์ด ์์
๊ณผ ํธํ๋๋ ๋ชจ๋ ์ํคํ
์ฒ์ ์ฒดํฌํฌ์ธํธ๋ฅผ ๋ณด๋ ค๋ฉด [์์
ํ์ด์ง](https://huggingface.co/tasks/text-classification)๋ฅผ ํ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
</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/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`]๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ฐฐ์น๋ฅผ ๋ง๋ค์ด๋ด
์๋ค. ๋ฐ์ดํฐ์
์ ์ฒด๋ฅผ ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉํ๋ ๋์ , *๋์ ํจ๋ฉ*์ ์ฌ์ฉํ์ฌ ๋ฐฐ์น์์ ๊ฐ์ฅ ๊ธด ๊ธธ์ด์ ๋ง๊ฒ ๋ฌธ์ฅ์ ํจ๋ฉํ๋ ๊ฒ์ด ํจ์จ์ ์
๋๋ค.
```py
>>> from transformers import DataCollatorWithPadding
>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
```
## ํ๊ฐํ๊ธฐ[[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}
```
<Tip>
[`Trainer`]๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ์ธ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ์ต์ํ์ง ์์ ๊ฒฝ์ฐ, [์ฌ๊ธฐ](../training#train-with-pytorch-trainer)์ ๊ธฐ๋ณธ ํํ ๋ฆฌ์ผ์ ํ์ธํ์ธ์!
</Tip>
์ด์ ๋ชจ๋ธ์ ํ๋ จ์ํฌ ์ค๋น๊ฐ ๋์์ต๋๋ค! [`AutoModelForSequenceClassification`]๋ก DistilBERT๋ฅผ ๊ฐ์ณ์ค๊ณ ์์๋๋ ๋ ์ด๋ธ ์์ ๋ ์ด๋ธ ๋งคํ์ ์ง์ ํ์ธ์:
```py
>>> from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
>>> model = AutoModelForSequenceClassification.from_pretrained(
... "distilbert/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,
... eval_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"],
... processing_class=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()
```
<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`์ ๊ฒฐ๊ณผ๋ฅผ ์๋์ผ๋ก ๋ณต์ ํ ์๋ ์์ต๋๋ค.
ํ
์คํธ๋ฅผ ํ ํฐํํ๊ณ 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'
```
|