用Huggingface变形器和Python进行文本总结方法介绍

239 阅读8分钟

我们生活在一个总是匆匆忙忙的时代--由于我们的日常活动,我们正经历着时间的匮乏,我们被互联网和媒体上大量的可用信息所淹没。我们并不总是有时间去寻找并花时间阅读或观看我们真正感兴趣的东西--但有一个专门针对我们每天通过媒体收到的文本的解决方案。

我们可以阅读文本的摘要,而不是整个文本。这将帮助我们深入了解文本的内容,并通过不阅读整个文本来节省时间。总结已经被广泛使用--作为摘要,作为结论等等。如今,文本处理经常被用于工业,而文本总结被认为是一项具有挑战性的任务。

在总结的任务中,很难回答文本的总结是否好?我们要回答的一个最重要的问题是--文本的信息量是否足够大?使用Huggingface Transformers制作文本的摘要并不难。通过本教程,我们将向你展示如何使用一些Huggingface Transformers来制作文本的摘要。在这篇文章中,我们将介绍:

1.先决条件

为了学习本教程,你需要安装Python 3.6或更高版本。你可以把它作为Anaconda的一部分来安装,也可以独立安装。你可以选择你喜欢的工作环境 - PyCharm、Visual Studio Code、Jupyter Notebook。无论你喜欢什么。你需要用命令来安装库中的转化器。

pip install transformers

Data Visual

在你安装转化器之后,你需要用命令导入库。

import transformers

值得注意的是,我们将只使用预训练的模型,在本教程中我们不会进行微调。

2.选择模型和背后的理论

Huggingface 包含模型部分,你可以选择你想处理的任务--在我们的例子中,我们将选择任务 总结。 当涉及到复杂的语言任务(如总结)时,变压器是一个众所周知的解决方案。

归纳任务使用了一个标准的编码器-解码器变形器-带有注意力模型的神经网络。变换器引入了 "注意力",负责捕捉一个句子中出现的所有单词之间的关系。在本教程中,我们将在实验中使用一个文本例子和三个模型。

我们决定用以下模型进行实验:

  • Pegasus
  • BART
  • T5

2.1Pegasus

Pegasus是标准的Transformer编码器-解码器,但是在Pegasus的预训练任务中,我们采用了类似于抽取式摘要的方法--从输入文档中抽取出重要的句子,并将其与剩余的句子连接起来作为一个输出序列。

Data Visual

这实际上意味着编码器输出的是被掩盖的标记,而解码器生成的是空隙句子。关于Pegasus模型的论文介绍了生成间隙句的情况,并解释了选择这些句子的策略。关于Pegasus模型的更多信息可以在 PEGASUS的科学论文中找到 张景清、赵耀、Mohammad Saleh和Peter J. Liu撰写的《用提取的空隙句子进行抽象概括的预训练》。

该模型被预训练了150万步,而不是50万步,因为我们观察到预训练的复杂度收敛得比较慢。对SentencePiece标记器进行了更新以编码换行字符。PEGASUSlarge(混合、随机)模型在几乎所有的下游任务中都取得了最佳结果。

2.2BART

这个模型是一个序列到序列的模型,被训练成一个去噪自动编码器。这表明BART可以将一种语言的序列作为输入,并返回另一种语言的输出序列。除了文本总结之外,BART还在许多任务中得到了应用,如问题回答、机器翻译等。

Programming Visual

BART模型对英语进行了预训练,并在CNN每日邮报上进行了微调。关于该模型的更多信息可以在论文 BART中找到 。用于自然语言生成、翻译和理解的去噪序列对序列预训练。 该论文是由Lewis等人撰写的。

BART在所有的ROUGE指标上比以前最好的工作(利用BERT)高出大约6.0分--代表了这个问题上性能的重大进步。

2.3T5

XL-Sum代表了一个数据集,其中包含了来自BBC的100万对注释的文章摘要。该数据集涵盖了44种不同的语言,它是基于从单一来源收集的数据数量的最大数据集。

Data Science Visual

mT5是一个在XL-SUM数据集上预先训练好的多语言T5模型。更多的细节可以在 XL-Sum中找到 。针对44种语言的大规模多语言抽象化总结

对于许多语言,XL-Sum提供了第一个公开可用的抽象概括数据集和基准。我们还为研究人员提供了数据集的整理工具,这将有助于随着时间的推移扩大数据集。

塔赫米德-哈桑

3.实例与实施

在所有的例子中,我们将使用同一个文本例子。文本例子取自HuggingFace,作为google/pegasus-xsum模型的一个例子。

text_example = 'The tower is 324 meters (1,063 ft) tall, about the same height 
as an 81-storey building, and the tallest structure in Paris. Its base is square, 
measuring 125 meters (410 ft) on each side. During its construction, the Eiffel 
Tower surpassed the Washington Monument to become the tallest man-made structure 
in the world, a title it held for 41 years until the Chrysler Building in New York
City was finished in 1930. It was the first structure to reach a height of 300 meters. 
Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is 
now taller than the Chrysler Building by 5.2 meters (17 ft). Excluding transmitters, 
the Eiffel Tower is the second tallest free-standing structure in France
after the Millau Viaduct.'

3.1 使用管道的例子

Huggingface变压器有一个选项,可以用所谓的管道下载模型,这是最简单的方法,可以尝试看看模型的工作原理。

管道在后台有来自transformers库的复杂代码,它代表了多种任务的API,如总结、情感分析、命名实体识别等等。对于PEGASUS模型,我们可以使用以下代码。

from transformers import pipeline
summarizer = pipeline("summarization", model = "google/pegasus-xsum")
summarizer(text_example)
[{'summary_text': 'The Eiffel Tower is a landmark in Paris, France.'}]

Programming Visual

BART模型也可以以类似的方式使用。

from transformers import pipeline
summarizer = pipeline("summarization", model = "facebook/bart-large-cnn")
summarizer(text_example)
[{'summary_text': 'The tower is 324 meters (1,063 ft) tall, about the same height 
as an 81-storey building. Its base is square, measuring 125 meters (410 ft) on each
side. During its construction, the Eiffel Tower surpassed the Washington Monument
to become the tallest man-made structure in the world.'}]

T5的情况也是如此。

from transformers import pipeline
summarizer = pipeline("summarization", model= "csebuetnlp/mT5_multilingual_XLSum")
summarizer(text_example)
[{'summary_text': 'The Eiffel Tower has become the tallest free-standing building 
in the world.'}]

3.2 使用AutoTokenizer和AutoModel的例子

通常情况下,我们想要自动检索相关的模型,给定预训练的配置的名称。这要感谢Huggignface的AutoClasses。AutoClasses 分为 AutoConfig、AutoModel 和 AutoTokenizer。根据模型名称或路径实例化其中一个,将为提供名称的模型创建相关架构。

对于PEGASUS来说,使用这个代码。

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

model = AutoModelForSeq2SeqLM.from_pretrained('google/pegasus-xsum')
tokenizer = AutoTokenizer.from_pretrained('google/pegasus-xsum')

tokens_input = tokenizer.encode("summarize: "+ text_example, return_tensors='pt', max_length=512, truncation=True)
ids = model.generate(tokens_input, min_length=80, max_length=120)
summary = tokenizer.decode(ids[0], skip_special_tokens=True)

print(summary)
"The Eiffel Tower in Paris, France, is the world's tallest free-standing 
structure and one of the most famous buildings in the world, having opened to the public 
on 1 September 1889, the same year it was officially opened to the public by the French
President, Charles de Gaulle, in a ceremony at the Arc de Triomphe on the Champs-Elysees
in Paris, France."

AI Visual

BART也可以用类似的方式来使用。

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")

tokens_input = tokenizer.encode("summarize: "+text_example, return_tensors='pt', max_length=512, truncation=True)
ids = model.generate(tokens_input, min_length=80, max_length=120)
summary = tokenizer.decode(ids[0], skip_special_tokens=True)

print(summary)
"The tower is 324 meters(1,063 ft) tall, about the same height as an 81-storey building. 
Its base is square, measuring 125 meters (410 ft) on each side. 
During its construction, the Eiffel Tower surpassed the Washington Monument to become 
the tallest man-made structure in the world. It held the title for 41 years until
the Chrysler Building in New York City was finished in 1930."

最后,这里是T5的代码。

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")

model = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")

tokens_input = tokenizer.encode("summarize: "+text_example, return_tensors='pt', max_length=512, truncation=True)
summary_ids = model.generate(tokens_input, min_length=80, max_length=120)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print(summary)
"The Eiffel Tower has become the world's tallest building, marking its 50th anniversary. 
But what exactly is it and what does it mean for those who want to be able to afford it?
The BBC has been looking at some of the key facts about the structure and why it is being built 
in the Mediterranean."

Programming Visual

使用管道作为输出的实验,与我们使用自动模型和自动标记器的实验相比,其总结更短。从所有的管道实验中,我们在这个实验中得到的输出,我更愿意看到来自第三个模型csebuetnlp/mT5_multilingual_XLSum的摘要。

在所有情况下,使用自动模型和自动标记器给了我们更详细的摘要,但我们定义了每个摘要的最短长度。在我看来,Google/pegasus-xsum是最好的,但csebuetnlp/mT5_multilingual_XLSum也很有参考价值。在facebook/bart-large-cnn模型的输出中,我不喜欢第一句话--我无法断定这句话是哪座塔,我希望在摘要的开头就知道这一点。

4.结论

最后,根据你想达到的目的--你可以在HuggingFace选择各种模型。你可以找到不同语言的模型,或者是多语言模型。在本教程中,我们介绍了你如何使用不同的模型与转化器管道或使用自动模型和自动标记器。

谢谢您的阅读!