Transformers documentation

텍스트 분류

You are viewing v4.29.1 version. A newer version v4.40.1 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

텍스트 분류

텍스트 분류는 자연어 처리의 일종으로, 텍스트에 레이블 또는 클래스를 지정하는 작업입니다. 많은 대기업이 다양한 실용적인 응용 분야에서 텍스트 분류를 운영하고 있습니다. 가장 인기 있는 텍스트 분류 형태 중 하나는 감성 분석으로, 텍스트 시퀀스에 🙂 긍정, 🙁 부정 또는 😐 중립과 같은 레이블을 지정합니다.

이 가이드에서 학습할 내용은:

  1. IMDb 데이터셋에서 DistilBERT를 파인 튜닝하여 영화 리뷰가 긍정적인지 부정적인지 판단합니다.
  2. 추론을 위해 파인 튜닝 모델을 사용합니다.

시작하기 전에, 필요한 모든 라이브러리가 설치되어 있는지 확인하세요:

pip install transformers datasets evaluate

Hugging Face 계정에 로그인하여 모델을 업로드하고 커뮤니티에 공유하는 것을 권장합니다. 메시지가 표시되면, 토큰을 입력하여 로그인하세요:

>>> from huggingface_hub import notebook_login

>>> notebook_login()

IMDb 데이터셋 가져오기

먼저 🤗 Datasets 라이브러리에서 IMDb 데이터셋을 가져옵니다:

>>> from datasets import load_dataset

>>> imdb = load_dataset("imdb")

그런 다음 예시를 살펴봅시다:

>>> 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은 긍정적인 리뷰를 나타냅니다.

전처리

다음 단계는 DistilBERT 토크나이저를 가져와서 text 필드를 전처리하는 것입니다:

>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

text를 토큰화하고 시퀀스가 DistilBERT의 최대 입력 길이보다 길지 않도록 자르기 위한 전처리 함수를 생성하세요:

>>> def preprocess_function(examples):
...     return tokenizer(examples["text"], truncation=True)

전체 데이터셋에 전처리 함수를 적용하려면, 🤗 Datasets map 함수를 사용하세요. 데이터셋의 여러 요소를 한 번에 처리하기 위해 batched=True로 설정함으로써 데이터셋 map를 더 빠르게 처리할 수 있습니다:

tokenized_imdb = imdb.map(preprocess_function, batched=True)

이제 DataCollatorWithPadding를 사용하여 예제 배치를 만들어봅시다. 데이터셋 전체를 최대 길이로 패딩하는 대신, 동적 패딩을 사용하여 배치에서 가장 긴 길이에 맞게 문장을 패딩하는 것이 효율적입니다.

Pytorch
Hide Pytorch content
>>> from transformers import DataCollatorWithPadding

>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
TensorFlow
Hide TensorFlow content
>>> from transformers import DataCollatorWithPadding

>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors="tf")

평가하기

훈련 중 모델의 성능을 평가하기 위해 메트릭을 포함하는 것이 유용합니다. 🤗 Evaluate 라이브러리를 사용하여 빠르게 평가 방법을 로드할 수 있습니다. 이 작업에서는 accuracy 메트릭을 가져옵니다. (메트릭을 가져오고 계산하는 방법에 대해서는 🤗 Evaluate quick tour를 참조하세요):

>>> import evaluate

>>> accuracy = evaluate.load("accuracy")

그런 다음 compute_metrics 함수를 만들어서 예측과 레이블을 계산하여 정확도를 계산하도록 compute를 호출합니다:

>>> 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 함수는 준비되었고, 훈련 과정을 설정할 때 다시 살펴볼 예정입니다.

훈련

모델을 훈련하기 전에, id2labellabel2id를 사용하여 예상되는 id와 레이블의 맵을 생성하세요:

>>> id2label = {0: "NEGATIVE", 1: "POSITIVE"}
>>> label2id = {"NEGATIVE": 0, "POSITIVE": 1}
Pytorch
Hide Pytorch content

Trainer를 사용하여 모델을 파인 튜닝하는 방법에 익숙하지 않은 경우, 여기의 기본 튜토리얼을 확인하세요!

이제 모델을 훈련시킬 준비가 되었습니다! AutoModelForSequenceClassification로 DistilBERT를 가쳐오고 예상되는 레이블 수와 레이블 매핑을 지정하세요:

>>> 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. train()를 호출하여 모델은 파인 튜닝하세요.
>>> 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()

Trainertokenizer를 전달하면 기본적으로 동적 매핑을 적용합니다. 이 경우, 명시적으로 데이터 수집기를 지정할 필요가 없습니다.

훈련이 완료되면, push_to_hub() 메소드를 사용하여 모델을 Hub에 공유할 수 있습니다.

>>> trainer.push_to_hub()
TensorFlow
Hide TensorFlow content

Keras를 사용하여 모델을 파인 튜닝하는 방법에 익숙하지 않은 경우, 여기의 기본 튜토리얼을 확인하세요!

TensorFlow에서 모델을 파인 튜닝하려면, 먼저 옵티마이저 함수와 학습률 스케쥴, 그리고 일부 훈련 하이퍼파라미터를 설정해야 합니다:
>>> 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를 로드하고, 예상되는 레이블 수와 레이블 매핑을 로드할 수 있습니다:

>>> from transformers import TFAutoModelForSequenceClassification

>>> model = TFAutoModelForSequenceClassification.from_pretrained(
...     "distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id
... )

prepare_tf_dataset()을 사용하여 데이터셋을 tf.data.Dataset 형식으로 변환합니다:

>>> 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를 사용하여 훈련할 모델을 구성합니다:

>>> import tensorflow as tf

>>> model.compile(optimizer=optimizer)

훈련을 시작하기 전에 설정해야할 마지막 두 가지는 예측에서 정확도를 계산하고, 모델을 Hub에 업로드할 방법을 제공하는 것입니다. 모두 Keras callbacks를 사용하여 수행됩니다.

KerasMetricCallbackcompute_metrics를 전달하여 정확도를 높입니다.

>>> from transformers.keras_callbacks import KerasMetricCallback

>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set)

PushToHubCallback에서 모델과 토크나이저를 업로드할 위치를 지정합니다:

>>> from transformers.keras_callbacks import PushToHubCallback

>>> push_to_hub_callback = PushToHubCallback(
...     output_dir="my_awesome_model",
...     tokenizer=tokenizer,
... )

그런 다음 콜백을 함께 묶습니다:

>>> callbacks = [metric_callback, push_to_hub_callback]

드디어, 모델 훈련을 시작할 준비가 되었습니다! fit에 훈련 데이터셋, 검증 데이터셋, 에폭의 수 및 콜백을 전달하여 파인 튜닝합니다:

>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=callbacks)

훈련이 완료되면, 모델이 자동으로 Hub에 업로드되어 모든 사람이 사용할 수 있습니다!

텍스트 분류를 위한 모델을 파인 튜닝하는 자세한 예제는 다음 PyTorch notebook 또는 TensorFlow notebook를 참조하세요.

추론

좋아요, 이제 모델을 파인 튜닝했으니 추론에 사용할 수 있습니다!

추론을 수행하고자 하는 텍스트를 가져와봅시다:

>>> 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을 인스턴스화하고, 텍스트를 전달해보세요:

>>> from transformers import pipeline

>>> classifier = pipeline("sentiment-analysis", model="stevhliu/my_awesome_model")
>>> classifier(text)
[{'label': 'POSITIVE', 'score': 0.9994940757751465}]

원한다면, pipeline의 결과를 수동으로 복제할 수도 있습니다.

Pytorch
Hide Pytorch content

텍스트를 토큰화하고 PyTorch 텐서를 반환합니다.

>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model")
>>> inputs = tokenizer(text, return_tensors="pt")

입력을 모델에 전달하고 logits을 반환합니다:

>>> from transformers import AutoModelForSequenceClassification

>>> model = AutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model")
>>> with torch.no_grad():
...     logits = model(**inputs).logits

가장 높은 확률을 가진 클래스를 모델의 id2label 매핑을 사용하여 텍스트 레이블로 변환합니다:

>>> predicted_class_id = logits.argmax().item()
>>> model.config.id2label[predicted_class_id]
'POSITIVE'
TensorFlow
Hide TensorFlow content

텍스트를 토큰화하고 TensorFlow 텐서를 반환합니다:

>>> from transformers import AutoTokenizer

>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model")
>>> inputs = tokenizer(text, return_tensors="tf")

입력값을 모델에 전달하고 logits을 반환합니다:

>>> from transformers import TFAutoModelForSequenceClassification

>>> model = TFAutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model")
>>> logits = model(**inputs).logits

가장 높은 확률을 가진 클래스를 모델의 id2label 매핑을 사용하여 텍스트 레이블로 변환합니다:

>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0])
>>> model.config.id2label[predicted_class_id]
'POSITIVE'