生成摘要(Summarization)的新方法

1 引言

生成摘要(Summarization)是指將一份文件或一篇文章歸納為一個較短的文本。這是自然語言處理非常重要的一個研究方向。在初始階段,我們使用的算法主要是基于文本簡單的詞頻統計《文本摘要生成的確定過程和隨機過程》, 后來使用的算法《PyTextRank---文本關鍵字(keywords)的自動取出》《聯合6種Transformers預訓練模型》開始朝著大規模的預訓練模型發展。本文繼續探索Transformers在生成摘要中的應用。


2 虛擬環境

起初比較懶得動手,把所有的庫都安裝在默認的根目錄下,但隨著安裝庫的不斷增加,各種庫之間的模塊開始發生沖突。因此還的按照規矩來,建立虛擬環境。建立虛擬環境有兩種方法:一種是手工建立,這種方法比較隨意,可以建立在硬盤中的任何位置,缺點是每次進入環境時都必須手動輸入命令;另一種是在Anaconda中建立,可以直接跳入到虛擬環境中,缺點是虛擬環境的位置已經被固定(C:\Users\Administrator\anaconda3\envs\tfs4),而且每次必須首先打開Anaconda,不過這個操作過程應該可以用一個cmd命令行來簡化,目前我還懶得動手寫。 

生成摘要(Summarization)的新方法的圖1

Transformers的虛擬環境名為tfs4。主要模塊安裝了截至2021/7/24的最新版本,關鍵庫如下:

Transformers V4.9.0 

Torch V1.9.0 

Spacy V3.1.1 

pytextrank 3.2.0

Spyder V5.0.5

Python 3.8.10


3 管道(Pipeline)方法

Transformers提供了許多管道(Pipeline)用來簡化操作。在《Transformers之問題對答(Question Answering)》中,使用了"question-answering"管道進行回答問題。現在我們使用另一個管道"summarization"來生成摘要。

from transformers import pipelinesummarizer = pipeline("summarization")

原始數據集采用的是CNN/每日郵報數據集,它是為產生摘要而準備的,由CNN的長篇新聞文章組成,然后在此基礎上進行了微調,產生出巴特模型(Bart model). 文章節選自一篇論文:

ARTICLE="Chuquicamata, one of the largest open pit copper mines and the second deepest open-pit mine in the world, is located 1,650km north of Santiago, Chile. The mine, popularly known as Chuqui, has been operating since 1910. The century-old copper mine is owned and operated by Codelco and forms part of the company’s Codelco Norte division, which includes the Radomiro Tomic (RT) mine found on the same mineralised system. A new underground mine is being developed to access the ore body situated beneath the present open pit mine. The conceptual engineering for the underground Chuquicamata mine began in 2007 and was finalised in March 2009. The project obtained the environmental authorisation in September 2010. The new underground mine, scheduled begin operations in 2019, will comprise of four production levels, a 7.5km main access tunnel, five clean air injection ramps, and two air-extraction shafts." 【丘基卡馬塔是最大的露天銅礦之一,也是世界上第二深的露天礦,位于智利圣地亞哥北部1650公里處。該礦俗稱丘基,自1910年以來一直在運營。這個有百年歷史的銅礦由Codelco公司擁有和經營,是該公司Codelco Norte部門的一部分,該部門包括在同一礦化系統上發現的Radomiro Tomic(RT)礦。目前正在開發一個新的地下礦,以進入位于目前露天礦下面的礦體。Chuquicamata地下礦的概念工程于2007年開始,并在2009年3月最終完成。該項目于2010年9月獲得環境授權。新的地下礦場計劃于2019年開始運營,將包括四個生產層,一條7.5公里的主通道,五個清潔空氣注入坡道,以及兩個空氣提取井。】


因為summarization管道依賴于PreTrainedModel.generate()方法,我們可以在管道中直接重載PreTrainedModel.generate()中max_length和min_length的默認參數,生成的總結如下所示。可以看出后半句話打亂了原文的語序,抽取出中心內容。 

Chuquicamata, one of the largest open pit copper mines in the world, is located 1,650km north of Santiago, Chile . The century-old mine is owned and operated by Codelco and forms part of the company’s Radomiro Tomic (RT) division.


4 模型和標記方法

盡管使用管道方法簡單,但正如在《閱讀理解回答問題(Question Answering)---一個更強的BERT預訓練模型》中指出的一樣,默認的管道模型精度太低,為了提高精度,下面使用模型和標記的方法來進行段落總結任務。采用Google的encoder-decoder模型T5(t5-base),它與上面使用的Bart模型類似,是一個多任務混合數據集,包括了Bart模型中的數據。

from transformers import AutoModelForSeq2SeqLM, AutoTokenizermodel = AutoModelForSeq2SeqLM.from_pretrained("t5-base")tokenizer = AutoTokenizer.from_pretrained("t5-base")

文本內容仍然使用上面的ARTICLE,得出的總結如下所示。顯然,這個結果比上面得出的結果要好。

Chuquicamata is one of the largest open pit copper mines and the second deepest open-pit mine in the world. the century-old copper mine is owned and operated by Codelco and forms part of the company's Codelco Norte division. new underground mine is being developed to access the ore body situated beneath the present open pit mine.

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP