预训练语言模型,称为BERT,是Bidirectional Encoder Representations from Transformers 的缩写。和ELMo或者OpenAI-GPT不同,BERT的预训练是同时考虑左边和右边上下文的双向表示。将预训练好的BERT表示应用到各个任务时只需要微调最后一层就能达到最佳效果。
BERT的原理非常简单但是应用效果非常好。在十一个NLP任务上达到新的最佳效果。
关注学习NLP小伙伴一起交流学习,文章最后提供NLP技术交流群 ,欢迎加入我们。
—————————————————————————————————————————————
针对每个word使用一个向量来表示,就是我们说的word embedding
以word周边的共现词的次数做为当前word的vector
未考虑上下文的影响上下文无关
word embedding 考虑上下文,这样bank就可以很容易区分出来了
论文链接:https://arxiv.org/abs/1511.01432
谷歌发表于2015年的一篇论文。该论文主要是提供了一种基于海量无标签数据的预训练NLP语言模型的思路以及如何利用无标签数据进行预训练。
本文主要是提供了两种利用无标签数据来提高序列模型表现的方法。
第一种是利用传统的神经语言模型(predict what comes next in a sequence)
第二种就是利用序列的自动编码器(autoencoder)。
这两种模型可以提前用海量无标注数据来训练,得到的参数可以用于后续有监督学习模型的初始化参数。在论文实验中,利用预训练参数训练的LSTM网络表现的更好且更稳定,并且在许多NLP分类任务中取得了不错的表现。
循环神经网络虽然已经被证明是NLP领域的第一大杀器,但是循环神经网络这一套参数很难调整,容易过拟合。但是循环神经网络又确确实实在理论上会取得比其他网络更卓越的效果,只要好好调参。因此,本文提出了一种全新的基于预训练的参数初始化方法。
本文的核心不是模型架构本身,而是一种预训练的思路。该思路就是,利用无标签数据先训练模型(非监督学习),由此得到的参数,作为下一阶段模型训练的初始化参数(监督学习)。因此,本文取名半监督学习(semi-supervised)。
提前让循环神经网络学会句子的表达,再之后根据标签去学习分类的能力。,这样训练的模型具有更高的稳定性,应该说是一个很好的调参思路。
论文地址: https://arxiv.org/pdf/1802.05365.pdf
首先,我们 了解词向量训练模型(例如:word2vec、GloVe)存在的问题,忽视了同一个的词语在不同语境下可能具有不同的含义问题。
**那么,ELMo(Embeddings from Language Models)**就是解决这样一个问题的模型。ELMo训练的不再只是一个词向量,而是一个包含多层BiLstm的模型,然后对于每一个句子,都需要传入该模型,分别拿到每个时间步在每个层的输出,最后在具体的NLP任务中,再单独训练每一层的权重向量,对每一层的向量进行线性加权作为每个词汇的最终向量表示。
ELMo具有以下两个优势.
(1)ELMo能够学习到词汇用法的复杂性,比如语法、语义。
(2)ELMo能够学习不同上下文情况下的词汇多义性。
项目地址:https://github.com/openai/finetune-transformer-lm
leaderboard 榜单:https://gluebenchmark.com/leaderboard
在这篇论文中,探索出了一种对自然语言理解任务的半监督方法,融合了无监督的预训练(pre-training)和有监督的微调(fine-tuning)过程。本文提出了一种通用表示,能够在范围广泛的任务中稍加修改、适应就能快速进行transfer.整个过程分成两个阶段。
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
● 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.
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
● 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,把需要预测的词给挡住。
如下图:
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)
BERT的论文为我们介绍了几种BERT可以处理的NLP任务:
● 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的唯一方法,就像ELMo一样,你可以使用预选训练好的BERT来创建语境化词嵌入。然后你可以将这些嵌入提供给现有的模型。
哪个向量最适合作为上下文嵌入? 我认为这取决于任务。 本文考察了六种选择(与微调模型相比,得分为96.4):
NLP可以分为自然语言理解(NLU)和自然语言生成(NLG)。在NLU方面,GLUE(General Language Understanding Evaluation)排行榜举例,数据集介绍。
●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
● Masked LM takes slightly longer to converge because we only predict 15% instead of 100%
● But absolute results are much better almost immediately
模型大小对微调准确率的影响,如下表:
Big models help a lot
● Going from 110M ->** 340M** params helps even on datasets with 3,600 labeled examples ● Improvements have not asymptoted
The Illustrated BERT, ELMo, and co. 详细阅读源码分析章节学习。
RoBERTa: A Robustly Optimized BERT Pretraining Approach (Liu et al, University of Washington and Facebook, 2019)
论文地址:https://arxiv.org/pdf/1907.11692.pdf
论文地址:https://arxiv.org/pdf/1909.11942.pdf
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。
the teacher - or an ensemble of models.
the student - is trained to reproduce the behaviour of a larger model
最近几年,预训练模型在NLP得到来快速的发展,而模型参数变得越来越多,导致线上环境的推断性能降低,这里使用DistilBERT解决来该问题。
Patient Distillation
https://arxiv.org/abs/1908.09355
https://www.aclweb.org/anthology/D19-1441.pdf
The inference/serving problem is mostly “solved” through distillation
如果感觉有用,留个赞吧~~
QQ:1121025745
更多学习内容可以关注 博客 https://blog.csdn.net/shenfuli ,并加入我们NLP技术交流群学习。
[1]BERT-全文翻译
https://zhuanlan.zhihu.com/p/59775981
[2] cs224N
[3]Contextual Word Representations: BERT (guest lecture by Jacob Devlin)
[4]Modeling contexts of use: Contextual Representations and Pretraining. ELMo and BERT.
[5]Contextual Word Representations: A Contextual Introduction