这里以BertForMaskedLM为例,记录下BertModel的网络结构和一些思考,cls那部分网络结构不涉及,即BertOnlyMLMHead那部分。
1 | BertForMaskedLM( |
1. position_embedding和transformer里面的位置编码不一样
transformer里面的positional embedding,是正余弦绝对位置编码,对于sequence分配不同的position id。而bert是学习出来的,分配一个(512,768)embedding。
随后word_embedding,positional embedding, token_type_embedding进行相加,然后经过LN、dropout获得最终embedding。
2. BertIntermediate是用来干嘛的
在transformer结构里,是没有这个中间层
的,细看下,是将768扩大4倍,随后在BertOutput那里又降维到768。那bert加这一个linear的作用是干嘛的呢?
搜了下网上的看法,很少关注或者没有相关思考。但是这这篇论文:Undivided Attention: Are Intermediate Layers Necessary for BERT? 给出了自己的解释。作者观点如下:
1 | In recent times, BERT-based models have been extremely successful in solving a variety of natural language processing (NLP) tasks such as reading comprehension, natural language inference, sentiment analysis, etc. All BERT-based architectures have a self-attention block followed by a block of intermediate layers as the basic building component. However, a strong justification for the inclusion of these intermediate layers remains missing in the literature. In this work we investigate the importance of intermediate layers on the overall network performance of downstream tasks. We show that reducing the number of intermediate layers and modifying the architecture for BERT-Base results in minimal loss in fine-tuning accuracy for downstream tasks while decreasing the number of parameters and training time of the model. Additionally, we use the central kernel alignment (CKA) similarity metric and probing classifiers to demonstrate that removing intermediate layers has little impact on the learned self-attention representations. |
结论就是实验后发现降低网络复杂度后,同时也能保持微调任务的准确性。