用于情感分析的 Hugging Face 管道

Python 中的自然语言处理(NLP)

Fouad Trad

Machine Learning Engineer

回顾:NLP 工作流

完整工作流图,标注第 1 章覆盖预处理,第 2 章覆盖特征提取。

Python 中的自然语言处理(NLP)

Hugging Face 管道

完整工作流图,说明第 3、4 章将介绍 Hugging Face 管道,其封装了预处理、特征提取和建模。

  • 现成流程:一次函数调用完成全部步骤
  • 定义管道需指定:
    • NLP 任务
    • 执行该任务的模型
Python 中的自然语言处理(NLP)

用于情感分析的管道

 

 

  • 文本分类任务
  • 预测文本表达积极或消极情绪

图片显示:积极情绪为笑脸和点赞,消极情绪为哭脸和倒拇指。

Python 中的自然语言处理(NLP)

文本分类的模型

一个动图,演示如何在网站中滚动并选择合适的任务以筛选模型。

1 https://huggingface.co/models
Python 中的自然语言处理(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 中的自然语言处理(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 中的自然语言处理(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 中的自然语言处理(NLP)

Passons à la pratique !

Python 中的自然语言处理(NLP)

Preparing Video For Download...