BERT-Pytorch 源码阅读
[TOC]
1. 整体描述
BERT-Pytorch 在分发包时,主要设置了两大功能:
bert-vocab :统计词频,token2idx, idx2token 等信息。对应 bert_pytorch.dataset.vocab 中的 build 函数。
bert:对应 bert_pytorch.__main__ 下的 train 函数。
为了能够调试,我重新建立了两个文件来分别对这两大功能进行调试。
1. bert-vocab
python3 -m ipdb test_bert_vocab.py # 调试 bert-vocab
其实 bert-vocab 内部并没有什么重要信息,无非就是一些自然语言处理中常见的预处理手段, 自己花个十分钟调试一下就明白了, 我加了少部分注释, 很容易就能明白。
内部继承关系为:
TorchVocab --> Vocab --> WordVocab
2. 模型架构
调试命令:
python3 -m ipdb test_bert.py -c data/corpus.small -v data/vocab.small -o output/bert.model
从模型整体上看, 分为两大部分: MaskedLanguageModel 与 NextSentencePrediction ,并且二者都以 BERT 为前置模型,在分别加上一个全连接层与 softmax 层来分别获得输出。
这段代码相对很简单,十分容易理解&