【人工智能】Transformers之Pipeline(十五):总结(summarization)

本文主要是介绍【人工智能】Transformers之Pipeline(十五):总结(summarization),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

​​​​​​​

目录

一、引言 

二、总结(summarization)

2.1 概述

2.2 BERT与GPT的结合—BART

2.3 应用场景​​​​​​​

2.4 pipeline参数

2.4.1 pipeline对象实例化参数

2.4.2 pipeline对象使用参数 ​​​​​​​

2.4.3 pipeline返回参数 ​​​​​​​​​​​​​​

2.5 pipeline实战

2.6 模型排名

三、总结


一、引言 

 pipeline(管道)是huggingface transformers库中一种极简方式使用大模型推理的抽象,将所有大模型分为音频(Audio)、计算机视觉(Computer vision)、自然语言处理(NLP)、多模态(Multimodal)等4大类,28小类任务(tasks)。共计覆盖32万个模型

今天介绍NLP自然语言处理的第三篇:总结(summarization),在huggingface库内有2000个总结(summarization)模型。

二、总结(summarization)

2.1 概述

摘要是在保留重要信息的同时生成文档的较短版本的任务。模型可以从原始输入中提取文本,同时可以生成全新的文本!

2.2 BERT与GPT的结合—BART

BART 是一个由facebook研发的Transformer 编码器-编码器 (seq2seq) 模型,具有双向 (类似 BERT) 编码器和自回归 (类似 GPT) 解码器。BART 通过 (1) 使用任意噪声函数破坏文本,以及 (2) 学习模型来重建原始文本进行预训练。
BART 在针对文本生成(例如摘要、翻译)进行微调时特别有效,但它也适用于理解任务(例如文本分类、问答)。这个特定的检查点已在 CNN Daily Mail(一个庞大的文本摘要对集合)上进行了微调。

2.3 应用场景​​​​​​​

  • 自动文摘:使用自然语言处理(NLP)技术,从长篇文章中提取出最重要的段落或句子。
  • 文本分类:根据文本内容对其进行分类,如新闻、博客、产品描述等。
  • 信息检索:通过总结来帮助用户快速找到相关信息。
  • 智能问答:使用总结技术来生成问题的答案。
  • 文本分析:从大量文本数据中提取出有价值的信息和知识。

2.4 pipeline参数

2.4.1 pipeline对象实例化参数

  • model(PreTrainedModel或TFPreTrainedModel)— 管道将使用其进行预测的模型。 对于 PyTorch,这需要从PreTrainedModel继承;对于 TensorFlow,这需要从TFPreTrainedModel继承。
  • tokenizer ( PreTrainedTokenizer ) — 管道将使用 tokenizer 来为模型编码数据。此对象继承自 PreTrainedTokenizer。
  • modelcardstrModelCard可选)— 属于此管道模型的模型卡。
  • frameworkstr可选)— 要使用的框架,"pt"适用于 PyTorch 或"tf"TensorFlow。必须安装指定的框架。
  • taskstr,默认为"")— 管道的任务标识符。
  • num_workersint可选,默认为 8)— 当管道将使用DataLoader(传递数据集时,在 Pytorch 模型的 GPU 上)时,要使用的工作者数量。
  • batch_sizeint可选,默认为 1)— 当管道将使用DataLoader(传递数据集时,在 Pytorch 模型的 GPU 上)时,要使用的批次的大小,对于推理来说,这并不总是有益的,请阅读使用管道进行批处理。
  • args_parser(ArgumentHandler,可选) - 引用负责解析提供的管道参数的对象。
  • deviceint可选,默认为 -1)— CPU/GPU 支持的设备序号。将其设置为 -1 将利用 CPU,设置为正数将在关联的 CUDA 设备 ID 上运行模型。您可以传递本机torch.devicestr
  • torch_dtypestrtorch.dtype可选) - 直接发送model_kwargs(只是一种更简单的快捷方式)以使用此模型的可用精度(torch.float16,,torch.bfloat16...或"auto"
  • binary_outputbool可选,默认为False)——标志指示管道的输出是否应以序列化格式(即 pickle)或原始输出数据(例如文本)进行。

2.4.2 pipeline对象使用参数 ​​​​​​​

  • documents ( strList[str]) — 要总结的一篇或多篇文章(或一个文章列表)。
  • return_textbool可选,默认为True)— 是否在输出中包含解码后的文本
  • return_tensorsbool可选,默认为False)— 是否在输出中包含预测张量(作为标记索引)。
  • clean_up_tokenization_spacesbool可选,默认为False)——是否清理文本输出中可能出现的额外空格。generate_kwargs——传递给模型的生成方法的附加关键字参数(请参阅此处与您的框架相对应的生成方法)。

2.4.3 pipeline返回参数 ​​​​​​​​​​​​​​

  • summary_text(str,出现时间return_text=True)——相应输入的摘要。
  • summary_token_ids(torch.Tensor或tf.Tensor,存在时return_tensors=True)—摘要的令牌 ID。

2.5 pipeline实战

采用pipeline,使用facebook的bart的微调版本bart-large-cnn进行摘要总结。

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"from transformers import pipelinesummarizer = pipeline("summarization", model="facebook/bart-large-cnn")ARTICLE = """New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison.  Her next court appearance is scheduled for May 18.
"""
print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))

执行后,自动下载模型文件并进行识别:

2.6 模型排名

在huggingface上,我们将总结(summarization)模型按下载量从高到低排序,总计2000个模型,排名第一是我们上述介绍的bart-large-cnn。

三、总结

本文对transformers之pipeline的总结(summarization)从概述、技术原理、pipeline参数、pipeline实战、模型排名等方面进行介绍,读者可以基于pipeline使用文中的2行代码极简的使用NLP中的总结(summarization)模型。

期待您的3连+关注,如何还有时间,欢迎阅读我的其他文章:

《Transformers-Pipeline概述》

【人工智能】Transformers之Pipeline(概述):30w+大模型极简应用

《Transformers-Pipeline 第一章:音频(Audio)篇》

【人工智能】Transformers之Pipeline(一):音频分类(audio-classification)

【人工智能】Transformers之Pipeline(二):自动语音识别(automatic-speech-recognition)

【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)

【人工智能】Transformers之Pipeline(四):零样本音频分类(zero-shot-audio-classification)

《Transformers-Pipeline 第二章:计算机视觉(CV)篇》

【人工智能】Transformers之Pipeline(五):深度估计(depth-estimation)

【人工智能】Transformers之Pipeline(六):图像分类(image-classification)

【人工智能】Transformers之Pipeline(七):图像分割(image-segmentation)

【人工智能】Transformers之Pipeline(八):图生图(image-to-image)

【人工智能】Transformers之Pipeline(九):物体检测(object-detection)

【人工智能】Transformers之Pipeline(十):视频分类(video-classification)

【人工智能】Transformers之Pipeline(十一):零样本图片分类(zero-shot-image-classification)

【人工智能】Transformers之Pipeline(十二):零样本物体检测(zero-shot-object-detection)​​​​​​​

《Transformers-Pipeline 第三章:自然语言处理(NLP)篇》

【人工智能】Transformers之Pipeline(十三):填充蒙版(fill-mask)​​​​​​​

【人工智能】Transformers之Pipeline(十四):问答(question-answering)

【人工智能】Transformers之Pipeline(十五):总结(summarization)

【人工智能】Transformers之Pipeline(十六):表格问答(table-question-answering)

【人工智能】Transformers之Pipeline(十七):文本分类(text-classification)

【人工智能】Transformers之Pipeline(十八):文本生成(text-generation)

【人工智能】Transformers之Pipeline(十九):文生文(text2text-generation)

【人工智能】Transformers之Pipeline(二十):令牌分类(token-classification)

【人工智能】Transformers之Pipeline(二十一):翻译(translation)

【人工智能】Transformers之Pipeline(二十二):零样本文本分类(zero-shot-classification)

《Transformers-Pipeline 第四章:多模态(Multimodal)篇》

【人工智能】Transformers之Pipeline(二十三):文档问答(document-question-answering)

【人工智能】Transformers之Pipeline(二十四):特征抽取(feature-extraction)

【人工智能】Transformers之Pipeline(二十五):图片特征抽取(image-feature-extraction)

【人工智能】Transformers之Pipeline(二十六):图片转文本(image-to-text)

【人工智能】Transformers之Pipeline(二十七):掩码生成(mask-generation)

【人工智能】Transformers之Pipeline(二十八):视觉问答(visual-question-answering)

这篇关于【人工智能】Transformers之Pipeline(十五):总结(summarization)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137225

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

二分最大匹配总结

HDU 2444  黑白染色 ,二分图判定 const int maxn = 208 ;vector<int> g[maxn] ;int n ;bool vis[maxn] ;int match[maxn] ;;int color[maxn] ;int setcolor(int u , int c){color[u] = c ;for(vector<int>::iter

整数Hash散列总结

方法:    step1  :线性探测  step2 散列   当 h(k)位置已经存储有元素的时候,依次探查(h(k)+i) mod S, i=1,2,3…,直到找到空的存储单元为止。其中,S为 数组长度。 HDU 1496   a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 。 x在 [-100,100] 解的个数  const int MaxN = 3000

状态dp总结

zoj 3631  N 个数中选若干数和(只能选一次)<=M 的最大值 const int Max_N = 38 ;int a[1<<16] , b[1<<16] , x[Max_N] , e[Max_N] ;void GetNum(int g[] , int n , int s[] , int &m){ int i , j , t ;m = 0 ;for(i = 0 ;

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

9.8javaweb项目总结

1.主界面用户信息显示 登录成功后,将用户信息存储在记录在 localStorage中,然后进入界面之前通过js来渲染主界面 存储用户信息 将用户信息渲染在主界面上,并且头像设置跳转,到个人资料界面 这里数据库中还没有设置相关信息 2.模糊查找 检测输入框是否有变更,有的话调用方法,进行查找 发送检测请求,然后接收的时候设置最多显示四个类似的搜索结果

java面试常见问题之Hibernate总结

1  Hibernate的检索方式 Ø  导航对象图检索(根据已经加载的对象,导航到其他对象。) Ø  OID检索(按照对象的OID来检索对象。) Ø  HQL检索(使用面向对象的HQL查询语言。) Ø  QBC检索(使用QBC(Qurey By Criteria)API来检索对象。 QBC/QBE离线/在线) Ø  本地SQL检索(使用本地数据库的SQL查询语句。) 包括Hibern