【NLP】用BERT進(jìn)行機(jī)器閱讀理解
編譯 | VK
來源 | Towards Data Science

這里可以找到帶有代碼的Github存儲(chǔ)庫(kù):https://github.com/edwardcqian/bert_QA。本文將討論如何設(shè)置此項(xiàng)功能.
機(jī)器(閱讀)理解是NLP的領(lǐng)域,我們使用非結(jié)構(gòu)化文本教機(jī)器理解和回答問題。
https://www.coursera.org/specializations/deep-learning?ranMID=40328&ranEAID=J2RDoRlzkk&ranSiteID=J2RDo.Rlzkk-XtffRH2JEnDifWa3VrZJ1A&siteID=J2RDo.Rlzkk-XtffRH2JEnDifWa3VrZJ1A&utm_content=2&utm_medium=partners&utm_source=linkshare&utm_campaign=J2RDoRlzkk
2016年,斯坦福德NLP將由超過100000對(duì)問答對(duì)組成的SQuAD (斯坦福問答數(shù)據(jù)集)集合在一起,這些數(shù)據(jù)集由維基百科文章編制。挑戰(zhàn)是訓(xùn)練一個(gè)機(jī)器學(xué)習(xí)模型,以基于上下文文檔回答問題。
當(dāng)提供上下文(自由文本)和問題時(shí),模型將返回最有可能回答問題的文本子集。

世界上頂尖的人工智能從業(yè)人員解決了這個(gè)問題,但兩年后,沒有一個(gè)模型能超越人類的標(biāo)準(zhǔn)。然而,2018年底,谷歌大腦引入了通用語言理解模型BERT(Transforms的雙向編碼器表示)。經(jīng)過一些微調(diào),與小組測(cè)試集相比,該模型能夠超越人類的基準(zhǔn)。
來自論文:https://arxiv.org/pdf/1606.05250.pdf,這些是用于評(píng)估的指標(biāo):
完全匹配。這個(gè)度量標(biāo)準(zhǔn)衡量的是精確匹配任何一個(gè)真實(shí)答案的百分比。
(宏平均)F1得分。這個(gè)度量了預(yù)測(cè)和真實(shí)答案之間的平均重疊。我們把預(yù)測(cè)和真實(shí)標(biāo)簽當(dāng)作token列表,并計(jì)算它們的F1。我們對(duì)給定問題的所有真實(shí)答案取最大F1,然后對(duì)所有問題取平均值。
基于初始的團(tuán)隊(duì)數(shù)據(jù)集:
人類注釋者的精確匹配得分為82.304%,F(xiàn)1得分91.221%
原BERT模型獲得85.083%的精確匹配分?jǐn)?shù),F(xiàn)1得分91.835%
今天,我將向你演示如何使用BERT建立自己的閱讀理解系統(tǒng)。這里可以找到帶有代碼的Github存儲(chǔ)庫(kù)。
https://github.com/edwardcqian/bert_QA
先設(shè)置Docker。??
設(shè)置Docker
Docker對(duì)于容器化應(yīng)用程序很有用。我們將使用Docker使這項(xiàng)工作更為有用,結(jié)果更可重復(fù)。按照以下說明在系統(tǒng)上安裝Docker。
你還需要docker compose,它是在macos和windows上與docker一起提供的。如果你使用Linux,可以在此處安裝它:https://runnable.com/docker/introduction-to-docker-compose
從Github倉(cāng)庫(kù)保存代碼
除了訓(xùn)練過的數(shù)據(jù)和預(yù)訓(xùn)練的權(quán)重之外,所有代碼和必要的依賴項(xiàng)都在repo中。注意:只有當(dāng)你想自己訓(xùn)練模型時(shí),才需要數(shù)據(jù)。如果沒有,你可以使用我預(yù)訓(xùn)練過的權(quán)重。另一個(gè)注意:除非你有一個(gè)強(qiáng)大的GPU或大量的時(shí)間,否則我不建議訓(xùn)練這個(gè)模型。
如果你想親自訓(xùn)練模型…
在這里下載Team2.0數(shù)據(jù)集:https://rajpurkar.github.io/SQuAD-explorer/。將“Training Set v2.0”和“Dev Set v2.0”保存到bert_uqa/data。
如果你想使用預(yù)訓(xùn)練的權(quán)重…
我已經(jīng)針對(duì)SQuAD 2.0訓(xùn)練了模型。你可以在這里下載。解壓縮文件并保存內(nèi)容asbert_uqa/weights。
創(chuàng)建Docker容器
Docker容器是使用Docker映像中提供的指令構(gòu)建的工作環(huán)境。我們需要一個(gè)docker-compose.yaml配置文件來定義容器的外觀。
version: '3'
services:
pytorch:
image: 'edwardcqian/huggingface_transformers_pytorch:2.5.1'
tty: true
restart: always
volumes:
- ./:/workspace
我為huggingface的Pytorch Transformer制作了一個(gè)定制的docker鏡像,你可以在dockerhub上找到。為了本教程的目的,你不需要像配置文件那樣拉取任何鏡像。配置文件還將本地bert_QA文件夾作為容器中的/workspace。
通過在terminal/shell根目錄中運(yùn)行docker compose up-d來啟動(dòng)容器。第一次將需要幾分鐘。
檢查我們的包含是否已啟動(dòng),并使用Docker ps運(yùn)行。

將bash shell附加到正在運(yùn)行的容器:
docker exec -it <your_container_name> bash
訓(xùn)練模型
如果你使用我的預(yù)訓(xùn)練的權(quán)重,請(qǐng)?zhí)^此步驟。我們將使用huggingface提供的默認(rèn)訓(xùn)練腳本來訓(xùn)練模型。
在bash shell中運(yùn)行此命令:
python run_squad.py \
--model_type bert \
--model_name_or_path bert-base-uncased \
--do_train \
--do_eval \
--do_lower_case \
--train_file data/train-v2.0.json \
--predict_file data/dev-v2.0.json \
--per_gpu_train_batch_size 12 \
--learning_rate 3e-5 \
--num_train_epochs 2.0 \
--max_seq_length 384 \
--doc_stride 128 \
--output_dir weights/
注意:如果沒有GPU,則per_gpu_train_batch_size將不會(huì)做任何操作
這將訓(xùn)練模型權(quán)重并將其保存到權(quán)重目錄。
用模型進(jìn)行推斷
現(xiàn)在讓我們用這個(gè)模型來做一些酷的事情。
在shell中啟動(dòng)ipython會(huì)話,并導(dǎo)入ModelInference模塊以從weights/加載權(quán)重。將上下文文檔作為參數(shù)傳遞給mi.add_target_text()。
攝入語料庫(kù)后,使用mi.evaluate()提問。只有當(dāng)模型確信答案存在于文本中時(shí),模塊才會(huì)返回答案。否則,模型將輸出“找不到有效答案”。
from model_inference import ModelInference
mi = ModelInference('weights/')
mi.add_target_text((
'The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) '
'were the people who in the 10th and 11th centuries gave their name to '
'Normandy, a region in France. They were descended from Norse '
'(\"Norman\" comes from \"Norseman\") raiders and pirates from Denmark, '
'Iceland and Norway who, under their leader Rollo, '
'agreed to swear fealty to King Charles III of West Francia. '
'Through generations of assimilation and mixing with the native '
'Frankish and Roman-Gaulish populations, their descendants would gradually '
'merge with the Carolingian-based cultures of West Francia. '
'The distinct cultural and ethnic identity of the Normans emerged initially '
'in the first half of the 10th century, '
'and it continued to evolve over the succeeding centuries.'
))
mi.evaluate('Where is Normandy')
# france
mi.evaluate('What are Normans called in latin?')
# normanni
mi.evaluate('When was normandy named?')
# in the 10th and 11th centuries
mi.evaluate('What kind of songs did the Normans sing?')
# No valid answers found.
mi.evaluate('What is you quest?')
# No valid answers found.
結(jié)論
往期精彩回顧 本站qq群851320808,加入微信群請(qǐng)掃碼:
