快速使用transformers的pipeline实现各种深度学习任务

目录

引言

安装

情感分析

文本生成

文本摘要

图片分类

实例分割

目标检测

音频分类

自动语音识别

视觉问答

文档问题回答

图文描述


引言

在这篇中文博客中,我们将深入探讨使用transformers库中的pipeline()函数,它为预训练模型提供了一个简单且快速的推理方法。pipeline()函数支持多种任务,包括文本分类、文本生成、摘要生成、图像分类、图像分割、对象检测、音频分类、自动语音识别、视觉问题回答、文档问题回答和图像字幕生成等。有了transformers的pipeline,我们可以快速的实现很多高质量的任务,我顿时感觉算法工程师的工作会方便很多,也会少很多职位了。

安装

直接用这个命令去安装这些库

pip install transformers datasets

情感分析

from transformers import pipeline

# 创建情感分析的pipeline
classifier = pipeline("sentiment-analysis")

# 应用模型进行推理
result = classifier("I love using Transformers. It's so easy and powerful.")
print(result)
# 输出结果:[{'label': 'POSITIVE', 'score': 0.9998376369476318}]
  1. 导入pipeline函数:首先,从transformers库中导入pipeline函数,这是使用transformers库进行各种自然语言处理任务的第一步。

  2. 创建情感分析的pipeline:接下来,调用pipeline函数并传入参数"sentiment-analysis",创建一个用于情感分析的pipeline。这个步骤会自动选择和下载一个适合于情感分析任务的预训练模型。

  3. 应用模型进行推理:定义一段文本"I love using Transformers. It's so easy and powerful."作为输入,使用上一步创建的情感分析pipeline对其进行分析。这个过程涉及将文本传递给预训练模型,模型会根据其学习到的特征来预测文本的情绪。

  4. 打印推理结果:情感分析的结果是一个列表,其中每个元素是一个包含预测标签和置信度分数的字典。通过打印结果,我们可以看到模型对于给定文本情感倾向的判断和相应的置信度。

文本生成

from transformers import pipeline

# 创建情感分析的pipeline
text_generator = pipeline("text-generation")

# 提供一个提示文本进行文本生成
prompt = "In a distant future, humanity has discovered the secret to interstellar travel,"
generated_text = text_generator(prompt, max_length=50)

print(generated_text)
# [{'generated_text': 'In a distant future, humanity has discovered the secret to interstellar travel, a technological civilization that has advanced far beyond their previous ability to transport resources. These starships are still capable of transporting materials and materials from their distant galaxies, but only able to provide'}]
  1. 导入pipeline函数:首先,从transformers库中导入pipeline函数。

  2. 创建文本生成的pipeline:通过调用pipeline函数并传入任务名称"text-generation"来创建一个文本生成pipeline。这将自动下载和加载一个适合文本生成的默认预训练模型。

  3. 提供一个提示文本进行文本生成:定义一个变量prompt作为模型的输入,这个例子中的提示文本是"In a distant future, humanity has discovered the secret to interstellar travel,"。然后,调用text_generator对象,并将prompt传递给它。此外,我们还可以通过max_length参数指定生成文本的最大长度,以控制生成内容的长度。

  4. 打印生成的文本text_generator函数返回一个列表,其中每个元素都是一个字典,包含键'generated_text'对应生成的文本。在这个例子中,我们打印第一个(也是唯一一个)生成文本的结果。

文本摘要

from transformers import pipeline

# 创建文本摘要生成的pipeline
summarizer = pipeline("summarization")

# 定义需要生成摘要的文本
text = """
The history of natural language processing (NLP) generally started in the 1950s, although work can be found from earlier periods.
In 1950, Alan Turing published an article titled "Computing Machinery and Intelligence" which proposed what is now called the Turing Test as a criterion of intelligence.
"""

# 使用summarizer生成摘要
summary = summarizer(text, max_length=45, min_length=25, do_sample=False)

# 打印生成的摘要
print(summary[0]['summary_text'])
# The history of natural language processing (NLP) generally started in the 1950s . In 1950, Alan Turing published an article titled "Computing Machinery and Intelligence" which proposed what is now called the Turing
  1. 导入pipeline函数:这一步从transformers库中导入pipeline<

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

E寻数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值