韩国小哥哥用Pytorch实现谷歌最强NLP预训练模型BERT|代码

新鲜代码,还热乎着呢。

前几天,谷歌发布了一篇论文,介绍了一个超强的NLP预训练模型BERT。

不仅在SQuAD中摧枯拉朽,全面超越人类表现,还在多种不同NLP测试中创出最佳成绩,包括包括将GLUE基准提升7.6%,将MultiNLI的准确率提提升5.6%。

更重要的是,论文中称,这个预训练语言模型可用于任何NLP任务,整个过程不需要对架构进行实质性的修改。

有人说这是自然语言理解领域几个月来最重大的事件,也有一些人认为这将改变NLP的研究模式。

当然,也有不少人心里长满了“草”,都想上手试试这个模型怎么样。

现在,方法来了。

一位名叫Junseong Kim韩国小哥哥,在GitHub上分享了自己用Pytorch实现BERT的过程与代码。

韩国小哥哥用Pytorch实现谷歌最强NLP预训练模型BERT|代码

Junseong Kim表示,代码很简单,而且也易于理解,其中一些代码基于The Annotated Transformer,但尚未得到验证

The Annotated Transformer来自“Attention is All You Need”,是哈佛大学的一个研究团队对后者的解读与实现,链接在文末。

语言模型预训练

在谷歌的论文中,作者给出了两种针对语言模型进行预训练的任务,分别是Masked Language Model(论文中简称Masked LM)和预测下一句。

Masked LM

Input Sequence : The man went to [MASK] store with [MASK] dog
Target Sequence : the his

规则:

根据下面的子规则,随机改变15%的输入token:

1、80%的 token 将成为 [MASK] token。

2、10% 的 token 将成为 [RANDOM] token(另一个单词)。

3、10% 的 token 将维持不变,但是需要预测。

预测下一句

Input : [CLS] the man went to the store [SEP] he bought a gallon of milk [SEP]
Label : Is Next
Input = [CLS] the man heading to the store [SEP] penguin [MASK] are flight ##less birds [SEP]
Label = NotNext

“当前的这个句子能够和下一句联系起来吗?”

理解两个文本句子之间的关系,这无法通过语言建模直接获取。

规则:

1、下一句有 50% 的概率是连续的句子。

2、下一句有 50% 的概率是无关的句子。

使用

注意:你的语料库中,一行中要准备两个句子,中间用 ( ) 分隔符隔开。

Welcome to the 	 the jungle 

I can stay 	 here all night 

1、根据自己的语料库构建vocab

python build_vocab.py -c data/corpus.small -o data/corpus.small.vocab
usage: build_vocab.py [-h] -c CORPUS_PATH -o OUTPUT_PATH [-s VOCAB_SIZE]
 [-e ENCODING] [-m MIN_FREQ]
optional arguments:
 -h, --help show this help message and exit
 -c CORPUS_PATH, --corpus_path CORPUS_PATH
 -o OUTPUT_PATH, --output_path OUTPUT_PATH
 -s VOCAB_SIZE, --vocab_size VOCAB_SIZE
 -e ENCODING, --encoding ENCODING
 -m MIN_FREQ, --min_freq MIN_FREQ

2、用自己的语料库构建BERT训练数据集

python build_dataset.py -d data/corpus.small -v data/corpus.small.vocab -o data/dataset.small
usage: build_dataset.py [-h] -v VOCAB_PATH -c CORPUS_PATH [-e ENCODING] -o
 OUTPUT_PATH
optional arguments:
 -h, --help show this help message and exit
 -v VOCAB_PATH, --vocab_path VOCAB_PATH
 -c CORPUS_PATH, --corpus_path CORPUS_PATH
 -e ENCODING, --encoding ENCODING
 -o OUTPUT_PATH, --output_path OUTPUT_PATH

3训练你自己的BERT模型

python train.py -d data/dataset.small -v data/corpus.small.vocab -o output/
usage: train.py [-h] -d TRAIN_DATASET [-t TEST_DATASET] -v VOCAB_PATH -o
 OUTPUT_DIR [-hs HIDDEN] [-n LAYERS] [-a ATTN_HEADS]
 [-s SEQ_LEN] [-b BATCH_SIZE] [-e EPOCHS]
optional arguments:
 -h, --help show this help message and exit
 -d TRAIN_DATASET, --train_dataset TRAIN_DATASET
 -t TEST_DATASET, --test_dataset TEST_DATASET
 -v VOCAB_PATH, --vocab_path VOCAB_PATH
 -o OUTPUT_DIR, --output_dir OUTPUT_DIR
 -hs HIDDEN, --hidden HIDDEN
 -n LAYERS, --layers LAYERS
 -a ATTN_HEADS, --attn_heads ATTN_HEADS
 -s SEQ_LEN, --seq_len SEQ_LEN
 -b BATCH_SIZE, --batch_size BATCH_SIZE
 -e EPOCHS, --epochs EPOCHS

GitHub传送门:

https://github.com/codertimo/BERT-pytorch

The Annotated Transformer传送门:

http://nlp.seas.harvard.edu/2018/04/03/attention.html

版权所有,未经授权不得以任何形式转载及使用,违者必究。