当前位置: 首页 > news >正文

【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)

📦 安装依赖

!pip install datasets evaluate transformers sentencepiece -q

✅ 情感分析

from transformers import pipelineclassifier = 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."
])

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

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

🎯 文本生成(指定参数)

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

⚙️ 使用 distilgpt2 模型生成文本

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

🌍 翻译(英语→德语)

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

🧩 掩码填空任务(填词)

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

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

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

🔁 T5 模型(Text2Text)

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

✂️ 文本摘要(Summarization)

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

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

from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipelinemodel_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!")

相关文章:

  • 形式化数学——Lean求值表达式
  • Winform(11.案例讲解1)
  • 探寻适用工具:AI+3D 平台与工具的关键能力及选型考量 (AI+3D 产品经理笔记 S2E03)
  • 动态指令参数:根据组件状态调整指令行为
  • MVC、MVP、MVVM三大架构区别
  • 全球化电商平台AWS云架构设计
  • APK 图标提取软件!一键获取应用宝藏图标
  • TS 类类型
  • 关于 dex2oat 以及 vdex、cdex、dex 格式转换
  • Sui 上线两周年,掀起增长「海啸」
  • 一、Hadoop历史发展与优劣势
  • 项目成本管理_挣得进度ES
  • osquery在网络安全入侵场景中的应用实战(二)
  • 【AND-OR-~OR锁存器设计】2022-8-31
  • 深度学习中学习率调整:提升食物图像分类模型性能的关键实践
  • 山东大学项目实训-创新实训-法律文书专家系统-项目报告(三)
  • Linux常用命令31——groupmod更改群组属性
  • 分析 Docker 磁盘占用
  • 浙大:基于内在偏好的LLM个性化对齐
  • 基于EFISH-SCB-RK3576/SAIL-RK3576的自助服务终端技术方案‌(国产化替代J1900的全场景技术解析)
  • 央行宣布优化两项支持资本市场的货币政策工具
  • 退休11年后,71岁四川厅官杨家卷被查
  • 《黎明的一切》:与正常世界脱轨后,我选择不再回去
  • 超导电路新设计有望提升量子处理器速度
  • 体坛联播|米兰逆转热那亚豪取3连胜,阿诺德官宣离开利物浦
  • 国铁集团:铁路五一假期旅客发送量累计已超1亿人次