Hugging Face pipelines สำหรับการวิเคราะห์ความรู้สึก

การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

Fouad Trad

Machine Learning Engineer

ทบทวน: NLP workflow

แผนภาพ workflow ฉบับสมบูรณ์ที่ระบุว่า Chapter 1 ครอบคลุม preprocessing และ Chapter 2 ครอบคลุม feature extraction

การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

Hugging Face pipelines

แผนภาพ workflow ฉบับสมบูรณ์ที่ระบุว่า Chapters 3 และ 4 จะครอบคลุม Hugging Face pipelines ซึ่งรวมทุกขั้นตอน ได้แก่ preprocessing, feature extraction และ modeling

  • workflow สำเร็จรูปที่จัดการทุกขั้นตอนในการเรียกฟังก์ชันครั้งเดียว
  • การสร้าง pipeline ต้องกำหนด:
    • งาน NLP
    • โมเดลที่ใช้ดำเนินงาน
การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

Pipeline สำหรับการวิเคราะห์ความรู้สึก

 

 

  • งานจำแนกประเภทข้อความ
  • ทำนายว่าข้อความแสดงอารมณ์เชิงบวกหรือเชิงลบ

ภาพแสดงหน้ายิ้มพร้อมนิ้วโป้งขึ้นสำหรับความรู้สึกเชิงบวก และหน้าเศร้าพร้อมนิ้วโป้งลงสำหรับความรู้สึกเชิงลบ

การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

โมเดลสำหรับการจำแนกประเภทข้อความ

GIF แสดงการเลื่อนดูเว็บไซต์และเลือกงานที่ต้องการเพื่อค้นหาโมเดลที่เหมาะสม

1 https://huggingface.co/models
การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

Pipeline ในโค้ด

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}]
การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

การวิเคราะห์ความรู้สึกกับข้อความหลายรายการพร้อมกัน

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}]
การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

การประเมินโมเดลวิเคราะห์ความรู้สึก

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
การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

มาฝึกกันเถอะ!

การประมวลผลภาษาธรรมชาติ (NLP) ด้วย Python

Preparing Video For Download...