감성 분석을 위한 Hugging Face 파이프라인

Python으로 배우는 Natural Language Processing (NLP)

Fouad Trad

Machine Learning Engineer

요약: NLP 워크플로

전체 워크플로 다이어그램으로, 1장은 전처리, 2장은 특징 추출을 다룸을 보여줍니다.

Python으로 배우는 Natural Language Processing (NLP)

Hugging Face 파이프라인

전체 워크플로 다이어그램으로, 3~4장은 모든 단계를 묶는 hugging face 파이프라인(전처리, 특징 추출, 모델링)을 다룸을 보여줍니다.

  • 모든 단계를 한 번에 처리하는 준비된 워크플로
  • 파이프라인 정의에 필요한 요소:
    • NLP 작업
    • 작업을 수행할 모델
Python으로 배우는 Natural Language Processing (NLP)

감성 분석 파이프라인

 

 

  • 텍스트 분류 작업
  • 텍스트가 긍정/부정 감정을 나타내는지 예측

긍정은 웃는 얼굴과 엄지손가락 위, 부정은 우는 얼굴과 엄지손가락 아래 아이콘을 보여줍니다.

Python으로 배우는 Natural Language Processing (NLP)

텍스트 분류용 모델

웹사이트에서 작업을 선택해 적합한 모델을 찾는 과정을 스크롤로 보여주는 GIF입니다.

1 https://huggingface.co/models
Python으로 배우는 Natural Language Processing (NLP)

코드에서의 파이프라인

from transformers import pipeline

classification_pipeline = pipeline(
task="sentiment-analysis", # or text-classification
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english" )
result = classification_pipeline("I really liked the movie!!")
print(result)
[{'label': 'POSITIVE', 'score': 0.9998093247413635}]
Python으로 배우는 Natural Language Processing (NLP)

텍스트 배치 감성 분석

texts = ["I really liked the movie!!",
         "Great job ruining my day.",
         "This product exceeded my expectations.",
         "Wow, just what I needed... another problem.", 
         "Absolutely fantastic experience!"]

results = classification_pipeline(texts)
print(results)
[{'label': 'POSITIVE', 'score': 0.9998093247413635}, 
 {'label': 'NEGATIVE', 'score': 0.8666700124740601}, 
 {'label': 'POSITIVE', 'score': 0.998874843120575}, 
 {'label': 'POSITIVE', 'score': 0.98626708984375}, 
 {'label': 'POSITIVE', 'score': 0.9998812675476074}]
Python으로 배우는 Natural Language Processing (NLP)

감성 분석 모델 평가

texts = ["I really liked the movie!!",
         "Great job ruining my day.",
         "This product exceeded my expectations.",
         "Wow, just what I needed... another problem.", 
         "Absolutely fantastic experience!"]

true_labels = ["POSITIVE", "NEGATIVE", "POSITIVE", "NEGATIVE", "POSITIVE"]
results = classification_pipeline(texts)
predicted_labels = [result['label'] for result in results]
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(true_labels, predicted_labels)

print(f"Accuracy: {accuracy}")
Accuracy: 0.80
Python으로 배우는 Natural Language Processing (NLP)

Ayo berlatih!

Python으로 배우는 Natural Language Processing (NLP)

Preparing Video For Download...