易学智能

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1278|回复: 1

资源 | 最强预训练模型BERT的Pytorch实现(非官方)

[复制链接]

38

主题

84

帖子

243

积分

中级会员

Rank: 3Rank: 3

积分
243
发表于 2018-10-19 17:20:37 | 显示全部楼层 |阅读模式
本文主要阐述 BERT 的实现。它的代码非常简单、易懂。一些代码基于《Attention is All You Need》一文中的 annotated Transformer。

该项目目前还在进展阶段。代码尚未得到验证。

语言模型预训练

在这篇论文中,作者展示了语言模型训练的新方法,即「遮蔽语言模型」(masked language model,MLM)和「预测下一句」。

Masked LM

见原论文:3.3.1 Task #1: Masked LM

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

基于以下子规则,随机 15% 的输入 token 将被改变:

  • 80% 的 token 是 [MASK] token。
  • 10% 的 token 是 [RANDOM] token(另一个单词)。
  • 10% 的 token 将维持不变,但是需要预测。


预测下一句

见原论文:3.3.2 Task #2: Next Sentence Prediction

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
「这个句子可以继续和下一个句子联系起来吗?」

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

规则:

  • 下一句有 50% 的可能是连续的句子。
  • 下一句有 50% 的可能是无关的句子。


使用

注意:你的语料库一行应该有两个句子,中间用 (\t) 分隔符隔开。

Welcome to the \t the jungle \n
I can stay \t here all night \n
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


回复

使用道具 举报

665

主题

1234

帖子

6576

积分

xdtech

Rank: 5Rank: 5

积分
6576
发表于 2018-11-5 09:41:25 | 显示全部楼层
还是不错的

不过
google官方的,现在也放出来啦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|易学智能

GMT+8, 2024-5-9 05:04 , Processed in 0.012655 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表