自然語言處理(NLP): 14 BERT上下文表示和其他預訓練語言模型

預訓練語言模型,稱爲BERT,是Bidirectional Encoder Representations from Transformers 的縮寫。和ELMo或者OpenAI-GPT不同,BERT的預訓練是同時考慮左邊和右邊上下文的雙向表示。將預訓練好的BERT表示應用到各個任務時只需要微調最後一層就能達到最佳效果。
BERT的原理非常簡單但是應用效果非常好。在十一個NLP任務上達到新的最佳效果。

關注學習NLP小夥伴一起交流學習,文章最後提供NLP技術交流羣 ,歡迎加入我們。
—————————————————————————————————————————————

歷史和背景

Pre-training in NLP

  • Word embeddings are the basis of deep learning for NLP

圖片

針對每個word使用一個向量來表示,就是我們說的word embedding

  • Word embeddings (word2vec, GloVe) are often pre-trained on text corpus from** co-occurrence statistics**

圖片

word周邊的共現詞的次數做爲當前word的vector

Contextual Representations

  • Problem: Word embeddings are applied in a context free manner

圖片

未考慮上下文的影響上下文無關

  • Solution: Train contextual representations on text corpus

圖片

word embedding 考慮上下文,這樣bank就可以很容易區分出來了

History of Contextual Representations

Semi-Supervised Sequence Learning, Google, 2015

論文鏈接:https://arxiv.org/abs/1511.01432

圖片

圖片

谷歌發表於2015年的一篇論文。該論文主要是提供了一種基於海量無標籤數據的預訓練NLP語言模型的思路以及如何利用無標籤數據進行預訓練。

  • 論文摘要

本文主要是提供了兩種利用無標籤數據來提高序列模型表現的方法。

第一種是利用傳統的神經語言模型(predict what comes next in a sequence)

第二種就是利用序列的自動編碼器(autoencoder)。

這兩種模型可以提前用海量無標註數據來訓練,得到的參數可以用於後續有監督學習模型的初始化參數。在論文實驗中,利用預訓練參數訓練的LSTM網絡表現的更好且更穩定,並且在許多NLP分類任務中取得了不錯的表現。

  • 預訓練模型的重要性

循環神經網絡雖然已經被證明是NLP領域的第一大殺器,但是循環神經網絡這一套參數很難調整,容易過擬合。但是循環神經網絡又確確實實在理論上會取得比其他網絡更卓越的效果,只要好好調參。因此,本文提出了一種全新的基於預訓練的參數初始化方法。

  • 核心思想

本文的核心不是模型架構本身,而是一種預訓練的思路。該思路就是,利用無標籤數據先訓練模型(非監督學習),由此得到的參數,作爲下一階段模型訓練的初始化參數(監督學習)。因此,本文取名半監督學習(semi-supervised)。

  • 核心本質

提前讓循環神經網絡學會句子的表達,再之後根據標籤去學習分類的能力。,這樣訓練的模型具有更高的穩定性,應該說是一個很好的調參思路。

ELMo: Deep Contextual Word Embeddings, AI2 & University of Washington, 2017

論文地址: https://arxiv.org/pdf/1802.05365.pdf

圖片

https://allennlp.org/elmo

首先,我們 瞭解詞向量訓練模型(例如:word2vec、GloVe)存在的問題,忽視了同一個的詞語在不同語境下可能具有不同的含義問題。

**那麼,ELMo(Embeddings from Language Models)**就是解決這樣一個問題的模型。ELMo訓練的不再只是一個詞向量,而是一個包含多層BiLstm的模型,然後對於每一個句子,都需要傳入該模型,分別拿到每個時間步在每個層的輸出,最後在具體的NLP任務中,再單獨訓練每一層的權重向量,對每一層的向量進行線性加權作爲每個詞彙的最終向量表示。

ELMo具有以下兩個優勢.

(1)ELMo能夠學習到詞彙用法的複雜性,比如語法、語義。

(2)ELMo能夠學習不同上下文情況下的詞彙多義性。

Improving Language Understanding by Generative Pre-Training, OpenAI, 2018

論文地址:https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf

項目地址:https://github.com/openai/finetune-transformer-lm

leaderboard 榜單:https://gluebenchmark.com/leaderboard

圖片

圖片

在這篇論文中,探索出了一種對自然語言理解任務的半監督方法,融合了無監督的預訓練(pre-training)和有監督的微調(fine-tuning)過程。本文提出了一種通用表示,能夠在範圍廣泛的任務中稍加修改、適應就能快速進行transfer.整個過程分成兩個階段。

  • 在無標籤的海量數據中訓練語言模型,學習神經網絡模型的參數。
  • 應用階段一訓練完成模型參數用相關標籤數據訓練target task。

圖片

Model Architecture

Transformer encoder

http://jalammar.github.io/illustrated-transformer/

圖片

● Multi-headed self attention

○ Models context

● Feed-forward layers

○ Computes non-linear hierarchical features

● Layer norm and residuals

○ Makes training deep networks healthy

● Positional embeddings

○ Allows model to learn relative positioning

BERT 介紹

Problem with Previous Methods

● Problem: Language models only use left context or right context, but language understanding is bidirectional.

● Why are LMs unidirectional?

● Reason 1: Directionality is needed to generate a

well-formed probability distribution.

○ We don’t care about this.

● Reason 2: Words can “see themselves” in a bidirectional encoder.

Unidirectional vs. Bidirectional Models

圖片

BERT:Masked Language Modeling預訓練模型

論文地址:https://arxiv.org/pdf/1810.04805.pdf

圖片

he two steps of how BERT is developed. You can download the model pre-trained in step 1 (trained on un-annotated data), and only worry about fine-tuning it for step

Masked Language Model

● Problem: Mask token never seen at fine-tuning

● Solution: 15% of the words to predict, but don’t replace with [MASK] 100% of the time. Instead:

● 80% of the time, replace with [MASK] went to the store → went to the [MASK]

● 10% of the time, replace random word went to the store → went to the running

● 10% of the time, keep same went to the store → went to the store

BERT說:“我要用 transformer 的 encoders”

Ernie不屑道:“呵呵,你不能像Bi-Lstm一樣考慮文章”

BERT自信回答道:“我們會用masks”

解釋一下Mask:

語言模型會根據前面單詞來預測下一個單詞,但是self-attention的注意力只會放在自己身上,那麼這樣100%預測到自己,毫無意義,所以用Mask,把需要預測的詞給擋住。

如下圖:

圖片

Two-sentence Tasks

make BERT better at handling relationships between multiple sentences, the pre-training process includes an additional task: Given two sentences (A and B), is B likely to be the sentence that follows A, or not?

在某些任務上我們需要2個句子作爲輸入,並做一些更爲智能的判斷,比如是否相似,比如 給出一個維基百科的內容作爲輸入,同時在放入一條針對該條目的問題,那麼我們的算法模型能夠處理這個問題嗎?爲了使BERT更好的處理2個句子之間的關係,預訓練的過程還有一個額外的任務:給定2個句子(A和B),A與B是否相似?(0或者1)

圖片

輸入表示 Input Representation

圖片

  • 用 WordPiece 嵌入,字典大小爲30000,詞之間用 ## 分隔。
  • 用最長512的序列學習位置嵌入
  • 每個序列的開始是一個用於分類任務的嵌入,用[CLS]表示,對應了序列最終的隱藏狀態。而對於非分類任務,這個向量可以忽視。
  • 句子對,打包成了一個序列,用[SEP] 分割開。其次,分別加上了一個句子嵌入E_A表示句子A,E_B表示句子嵌入B。
  • 對於單句輸入我們只用E_A

特殊NLP任務 Task specific-Models

圖片

BERT的論文爲我們介紹了幾種BERT可以處理的NLP任務:

  1. 短文本相似
  2. 文本分類
  3. QA機器人
  4. 語義標註

圖片

Model Details

● Data: Wikipedia (2.5B words) + BookCorpus (800M words)

● Batch Size: 131,072 words (1024 sequences * 128 length or 256 sequences * 512 length) ● Training Time: 1M steps (~40 epochs)

● Optimizer: AdamW, 1e-4 learning rate, linear decay

● BERT-Base: 12-layer, 768-hidden, 12-head

● BERT-Large: 24-layer, 1024-hidden, 16-head

● Trained on 4x4 or 8x8 TPU slice for 4 days

BERT for feature extraction

微調方法並不是使用BERT的唯一方法,就像ELMo一樣,你可以使用預選訓練好的BERT來創建語境化詞嵌入。然後你可以將這些嵌入提供給現有的模型。

圖片

哪個向量最適合作爲上下文嵌入? 我認爲這取決於任務。 本文考察了六種選擇(與微調模型相比,得分爲96.4):

圖片

  • Feature Extraction:特徵提取
  • Finetune:微調

GLUE Results

在這裏插入圖片描述

NLP可以分爲自然語言理解(NLU)和自然語言生成(NLG)。在NLU方面,GLUE(General Language Understanding Evaluation)排行榜舉例,數據集介紹。

  • CoLA(The Corpus of Linguistic Acceptability):紐約大學發佈的有關語法的數據集,該任務主要是對一個給定句子,判定其是否語法正確,因此CoLA屬於單個句子的文本二分類任務;
  • SST(The Stanford Sentiment Treebank),是斯坦福大學發佈的一個情感分析數據集,主要針對電影評論來做情感分類,因此SST屬於單個句子的文本分類任務(其中SST-2是二分類,SST-5是五分類,SST-5的情感極性區分的更細緻);
  • MRPC(Microsoft Research Paraphrase Corpus),由微軟發佈,判斷兩個給定句子,是否具有相同的語義,屬於句子對的文本二分類任務;
  • STS-B(Semantic Textual Similarity Benchmark),主要是來自於歷年SemEval中的一個任務(同時該數據集也包含在了SentEval),具體來說是用1到5的分數來表徵兩個句子的語義相似性,本質上是一個迴歸問題,但依然可以用分類的方法做,因此可以歸類爲句子對的文本五分類任務;
  • QQP(Quora Question Pairs),是由Quora發佈的兩個句子是否語義一致的數據集,屬於句子對的文本二分類任務;
  • MNLI(Multi-Genre Natural Language Inference),同樣由紐約大學發佈,是一個文本蘊含的任務,在給定前提(Premise)下,需要判斷假設(Hypothesis)是否成立,其中因爲MNLI主打賣點是集合了許多不同領域風格的文本,因此又分爲matched和mismatched兩個版本的MNLI數據集,前者指訓練集和測試集的數據來源一致,而後者指來源不一致。該任務屬於句子對的文本三分類問題。
  • QNLI(Question Natural Language Inference),其前身是SQuAD 1.0數據集,給定一個問句,需要判斷給定文本中是否包含該問句的正確答案。屬於句子對的文本二分類任務;
  • RTE(Recognizing Textual Entailment),和MNLI類似,也是一個文本蘊含任務,不同的是MNLI是三分類,RTE只需要判斷兩個句子是否能夠推斷或對齊,屬於句子對的文本二分類任務;
  • WNLI(Winograd Natural Language Inference),也是一個文本蘊含任務,不過似乎GLUE上這個數據集還有些問題

SQuAD 2.0

圖片

Effect of Pre-training Task

圖片

●Masked LM (compared to left-to-right LM) is very important on some tasks, Next Sentence Prediction is important on other tasks.

● Left-to-right model does very poorly on word-level task (SQuAD), although this is mitigated by BiLSTM

訓練步長的影響 Effect of Directionality and Training Time

圖片

● Masked LM takes slightly longer to converge because we only predict 15% instead of 100%

● But absolute results are much better almost immediately

模型大小的影響 Effect of Model Size

模型大小對微調準確率的影響,如下表:

圖片

Big models help a lot

● Going from 110M ->** 340M** params helps even on datasets with 3,600 labeled examples ● Improvements have not asymptoted

如何使用BERT

BERT源碼分析

The Illustrated BERT, ELMo, and co. 詳細閱讀源碼分析章節學習。

BERT升級版

RoBERTa:更強大的BERT

RoBERTa: A Robustly Optimized BERT Pretraining Approach (Liu et al, University of Washington and Facebook, 2019)

論文地址:https://arxiv.org/pdf/1907.11692.pdf

  • 加大訓練數據 16GB -> 160GB,更大的batch size,訓練時間加長
  • 不需要NSP Loss
  • 使用更長的訓練 Sequence
  • Static vs. Dynamic Masking
  • 模型訓練成本在6萬美金以上(估算)
  • Improved masking and pre-training data slightly

圖片

ALBERT:參數更少的BERT

論文地址:https://arxiv.org/pdf/1909.11942.pdf

  • 一個輕量級的BERT模型
  • 核心思想:
    • 共享層與層之間的參數 (減少模型參數)
    • 增加單層向量維度

圖片

圖片

DistilBERT:輕量版BERT

https://arxiv.org/pdf/1910.01108.pdf

Knowledge distillation [Bucila et al., 2006, Hinton et al., 2015] is a compression technique in which a compact model。

  • teacher (MLM) = distribution

the teacher - or an ensemble of models.

  • student: 學習distribution: -\sum_{i=1}^k p_teacher_i log (q_student_i)

the student - is trained to reproduce the behaviour of a larger model

最近幾年,預訓練模型在NLP得到來快速的發展,而模型參數變得越來越多,導致線上環境的推斷性能降低,這裏使用DistilBERT解決來該問題。

圖片

Patient Distillation

https://arxiv.org/abs/1908.09355

圖片

圖片

預訓練模型總結

  • 基於 Transformer 的模型統治了該領域。
  • 更大規模的預訓練數據搭配更大的模型和更強大的算力。
  • 一些局部的小技巧層出不窮(數據預處理、masking、訓練目標等等)
  • 模型壓縮、加速

https://www.aclweb.org/anthology/D19-1441.pdf

  • 針對bert在線服務推理問題通過蒸餾手段完成

The inference/serving problem is mostly “solved” through distillation

聯繫方式

如果感覺有用,留個贊吧~~

QQ:1121025745

更多學習內容可以關注 博客 https://blog.csdn.net/shenfuli ,並加入我們NLP技術交流羣學習。

20200117151004647.jpeg

參考資料

[1]BERT-全文翻譯
https://zhuanlan.zhihu.com/p/59775981

[2] cs224N

[3]Contextual Word Representations: BERT (guest lecture by Jacob Devlin)

[slides] [video]

[4]Modeling contexts of use: Contextual Representations and Pretraining. ELMo and BERT.

[slides] [video]

[5]Contextual Word Representations: A Contextual Introduction

https://arxiv.org/pdf/1902.06006.pdf

[6]The Illustrated BERT, ELMo, and co.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章