自然语言处理(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.