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

【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!")
http://www.xdnf.cn/news/3884.html

相关文章:

  • 全球化电商平台Azure云架构设计
  • 【计网】交换机和集线器对比
  • java学习之数据结构:四、树(代码补充)
  • 【Spring Boot】Spring Boot + Thymeleaf搭建mvc项目
  • flink rocksdb状态说明
  • 阿里云物联网平台--云产品流传
  • 7、Activiti-任务类型
  • 如何快速获取字符串的UTF-8或UTF-16编码二进制数据?数值转换成字符串itoa不是C标准?其它类型转换成字符串?其它类型转换成数值类型?
  • 虚幻引擎作者采访
  • 2.在Openharmony写hello world
  • 蓝桥杯 18. 积木
  • 记9(Torch
  • Leetcode刷题记录32——搜索二维矩阵 II
  • Dubbo(97)如何在物联网系统中应用Dubbo?
  • C语言 ——— 函数
  • Java设计模式: 工厂模式与策略模式
  • COlT_CMDB_linux_tomcat_20250505.sh
  • 【AI大模型】SpringBoot整合Spring AI 核心组件使用详解
  • 基于大模型的子宫腺肌病全流程预测与诊疗方案研究报告
  • 定位理论第一法则在医疗AI编程中的应用
  • Linux /dev/null文件用法介绍
  • 【KWDB 创作者计划】KWDB 2.2.0多模融合架构与分布式时序引擎
  • 如何选择合适的光源?
  • 【Linux网络#17】TCP全连接队列与tcpdump抓包
  • Linux55yum源配置、本机yum源备份,本机yum源配置,网络Yum源配置,自建yum源仓库
  • 人工智能数学基础(十)—— 图论
  • 告别散乱的 @ExceptionHandler:实现统一、可维护的 Spring Boot 错误处理
  • graphviz和dot绘制流程图
  • 金仓数据库 KingbaseES 在电商平台数据库迁移与运维中深入复现剖析
  • MongoDB 整合SpringBoot