【NLP】32. Transformers (HuggingFace Pipelines 实战)

🤖 Transformers (HuggingFace Pipelines 实战)

本教程基于 Hugging Face 的 transformers 库,展示如何使用预训练模型完成以下任务:

  • 情感分析(Sentiment Analysis)
  • 文本生成(Text Generation)
  • 翻译(Translation)
  • 掩码填空(Masked Language Modeling)
  • 零样本分类(Zero-shot Classification)
  • 特定任务模型推理(Text2Text Generation, e.g., T5)
  • 文本摘要(Summarization)
  • 自定义分类模型载入与推理(如 BERT)

📦 安装依赖

python 复制代码
!pip install datasets evaluate transformers sentencepiece -q

✅ 情感分析

python 复制代码
from transformers import pipeline

classifier = pipeline("sentiment-analysis")

# 单个句子
classifier("I really enjoy learning new things every day.")

# 多个句子
classifier([
    "I really enjoy learning new things every day.",
    "I dislike rainy weather on weekends."
])

✍️ 文本生成(默认模型)

python 复制代码
from transformers import pipeline

generator = pipeline("text-generation")
generator("Today we are going to explore something exciting")

🎯 文本生成(指定参数)

python 复制代码
# 两句话,每句最多15词
generator("Today we are going to explore something exciting", num_return_sequences=2, max_length=15)

⚙️ 使用 distilgpt2 模型生成文本

python 复制代码
from transformers import pipeline

generator = pipeline("text-generation", model="distilgpt2")
generator(
    "Today we are going to explore something exciting",
    max_length=30,
    num_return_sequences=2,
)

🌍 翻译(英语→德语)

python 复制代码
from transformers import pipeline

translator = pipeline("translation_en_to_de")
translator("This is a great day to learn something new!")

🧩 掩码填空任务(填词)

python 复制代码
from transformers import pipeline

unmasker = pipeline("fill-mask")
unmasker("Life is like a <mask> of chocolates.")

🧠 零样本分类(Zero-shot Classification)

python 复制代码
from transformers import pipeline

classifier = pipeline("zero-shot-classification")
classifier(
    "This tutorial is about machine learning and natural language processing.",
    candidate_labels=["education", "sports", "politics"]
)

🔁 T5 模型(Text2Text)

python 复制代码
from transformers import pipeline

text2text = pipeline("text2text-generation")
text2text("Translate English to French: How are you today?")

✂️ 文本摘要(Summarization)

python 复制代码
from transformers import pipeline

summarizer = pipeline("summarization")
summarizer(
    "Machine learning is a field of artificial intelligence that focuses on enabling machines to learn from data..."
)

🧪 使用自己的模型(以 BERT 为例)

python 复制代码
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline

model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForSequenceClassification.from_pretrained(model_name)

pipe = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
pipe("I had an amazing experience with this product!")
相关推荐
SelectDB9 小时前
网易云音乐基于 Apache Doris 的 AI 数据统一查询与治理实践
人工智能
火山引擎开发者社区10 小时前
一个向量库,两种检索:VikingDB 关键词检索实践
人工智能
火山引擎开发者社区10 小时前
火山引擎携手南开大学:两个 AI 智能体,让美学研究走进日常对话
人工智能
WT同学11 小时前
RAG 系统工程化实践(深化篇):从"能用"到"好用"的进阶之路
人工智能
货拉拉技术11 小时前
货拉拉大数据智能运维AI Agent探索实践
大数据·人工智能
xsd2024111811 小时前
Eye-to-Hand 手眼标定实操:固定相机怎么标定出机械臂基座到相机的外参
人工智能
zc.z12 小时前
提示词工程(十一):深入理解Planning Prompt底层原理,从任务拆解到执行计划
人工智能·prompt·提示词工程·prompt planning
上弦外贸新观察13 小时前
WordPress外贸网站建设与Google SEO、海外GEO优化技术指南:网站速度、内容架构和AI搜索可见度布局
人工智能·架构
硅谷秋水13 小时前
逆矩阵的Physis-v1.0
人工智能·机器学习·计算机视觉·语言模型·机器人
feasibility.13 小时前
智元发布 LIFE 框架:让机器人从“完成任务“到“学会生活“,24 小时真机视频验证具身智能
人工智能·机器人·生活·视频·具身智能·智元机器人·智元