HuggingFace:pipeline为特定NLP任务直接调用

本文介绍了HuggingFace的pipeline在多种自然语言处理任务中的应用,包括情绪分析、零样本分类、文本生成、模型库中模型的使用、填空任务、命名实体识别、问答、摘要生成和翻译。每个任务都提供了简单的调用方法,并强调了pipeline的便捷性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. English Sentiment Analysis

默认情况下,pipeline选择一个特定的预训练模型,该模型已为英语情绪分析进行了微调。创建分类器对象时,将下载并缓存模型。如果重新运行该命令,则将使用缓存的模型,无需再次下载该模型。

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")
输出:[{'label': 'POSITIVE', 'score': 0.9598047137260437}]

2. zero-shot classification

from transformers import pipeline

classifier = pipeline("zero-shot-classification")
classifier(
    "This is a course about the Transformers library",
    candidate_labels=["education", "politics", "business", "course"],
)
输出:
{'sequence': 'This is a course about the Transformers library',
 'labels': ['course', 'education', 'business', 'politics'],
 'scores': [0.9461037516593933,
  0.04552055522799492,
  0.0060350666753947735,
  0.002340571256354451]}

3. text generation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值