一 BERT_Base?110M參數(shù)拆解
BERT_base模型的110M的參數(shù)具體是如何組成的呢,我們一起來計算一下:
剛好也能更深入地了解一下Transformer Encoder模型的架構(gòu)細節(jié)。
借助transformers模塊查看一下模型的架構(gòu):
import torch
from transformers import BertTokenizer, BertModel
bertModel = BertModel.from_pretrained('bert-base-uncased', output_hidden_states=True, output_attentions=True)
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
for name,param in bertModel.named_parameters():
print(name, param.shape)
得到的模型參數(shù)為:
embeddings.word_embeddings.weight torch.Size([30522, 768])
embeddings.position_embeddings.weight torch.Size([512, 768])
embeddings.token_type_embeddings.weight torch.Size([2, 768])
embeddings.LayerNorm.weight torch.Size([768])
embeddings.LayerNorm.bias torch.Size([768])
encoder.layer.0.attention.self.query.weight torch.Size([768, 768])
encoder.layer.0.attention.self.query.bias torch.Size([768])
encoder.layer.0.attention.self.key.weight torch.Size([768, 768])
encoder.layer.0.attention.self.key.bias torch.Size([768])
encoder.layer.0.attention.self.value.weight torch.Size([768, 768])
encoder.layer.0.attention.self.value.bias torch.Size([768])
encoder.layer.0.attention.output.dense.weight torch.Size([768, 768])
encoder.layer.0.attention.output.dense.bias torch.Size([768])
encoder.layer.0.attention.output.LayerNorm.weight torch.Size([768])
encoder.layer.0.attention.output.LayerNorm.bias torch.Size([768])
encoder.layer.0.intermediate.dense.weight torch.Size([3072, 768])
encoder.layer.0.intermediate.dense.bias torch.Size([3072])
encoder.layer.0.output.dense.weight torch.Size([768, 3072])
encoder.layer.0.output.dense.bias torch.Size([768])
encoder.layer.0.output.LayerNorm.weight torch.Size([768])
encoder.layer.0.output.LayerNorm.bias torch.Size([768])
encoder.layer.11.attention.self.query.weight torch.Size([768, 768])
encoder.layer.11.attention.self.query.bias torch.Size([768])
encoder.layer.11.attention.self.key.weight torch.Size([768, 768])
encoder.layer.11.attention.self.key.bias torch.Size([768])
encoder.layer.11.attention.self.value.weight torch.Size([768, 768])
encoder.layer.11.attention.self.value.bias torch.Size([768])
encoder.layer.11.attention.output.dense.weight torch.Size([768, 768])
encoder.layer.11.attention.output.dense.bias torch.Size([768])
encoder.layer.11.attention.output.LayerNorm.weight torch.Size([768])
encoder.layer.11.attention.output.LayerNorm.bias torch.Size([768])
encoder.layer.11.intermediate.dense.weight torch.Size([3072, 768])
encoder.layer.11.intermediate.dense.bias torch.Size([3072])
encoder.layer.11.output.dense.weight torch.Size([768, 3072])
encoder.layer.11.output.dense.bias torch.Size([768])
encoder.layer.11.output.LayerNorm.weight torch.Size([768])
encoder.layer.11.output.LayerNorm.bias torch.Size([768])
pooler.dense.weight torch.Size([768, 768])
pooler.dense.bias torch.Size([768])
其中,BERT模型的參數(shù)主要由三部分組成:
Embedding層參數(shù)
Transformer Encoder層參數(shù)
LayerNorm層參數(shù)
二 Embedding層參數(shù)
由于詞向量是由Token embedding,Position embedding,Segment embedding三部分構(gòu)成的,因此embedding層的參數(shù)也包括以上三部分的參數(shù)。
BERT_base英文詞表大小為:30522, 隱藏層hidden_size=768,文本最大長度seq_len = 512
Token embedding參數(shù)量為:30522 * 768;
Position embedding參數(shù)量為:512 * 768;
Segment embedding參數(shù)量為:2 * 768。
因此總的參數(shù)量為:(30522 + 512 +2)* 768 = 23,835,648
?
LN層在Embedding層
norm使用的是layer normalization,每個維度有兩個參數(shù)
768 * 2 =?1536
三 Transformer Encoder層參數(shù)
可以將該部分拆解成兩部分:Self-attention層參數(shù)、Feed-Forward Network層參數(shù)。
1.Self-attention層參數(shù)
改層主要是由Q、K、V三個矩陣運算組成,BERT模型中是Multi-head多頭的Self-attention(記為SA)機制。先通過Q和K矩陣運算并通過softmax變換得到對應(yīng)的權(quán)重矩陣,然后將權(quán)重矩陣與 V矩陣相乘,最后將12個頭得到的結(jié)果進行concat,得到最終的SA層輸出。
1. multi-head因為分成12份, 單個head的參數(shù)是 768 * (768/12) * 3,? 緊接著將多個head進行concat再進行變換,此時W的大小是768 * 768
? ? 12個head就是? 768 * (768/12) * 3 *?12? +?768 * 768 =?1,769,472 + 589,824 =?2359296
3. LN層在Self-attention層
norm使用的是layer normalization,每個維度有兩個參數(shù)
768 * 2 =?1536
2.Feed-Forward Network層參數(shù)
由FFN(x)=max(0, xW1+b1)W2+b2可知,前饋網(wǎng)絡(luò)FFN主要由兩個全連接層組成,且W1和W2的形狀分別是(768,3072),(3072,768),因此該層的參數(shù)量為:
feed forward的參數(shù)主要由兩個全連接層組成,intermediate_size為3072(原文中4H長度) ,那么參數(shù)為12*(768*3072+3072*768)= 56623104
LN層在FFN層
norm使用的是layer normalization,每個維度有兩個參數(shù)
768 * 2 =?1536
layer normalization
layer normalization有兩個參數(shù),分別是gamma和beta。有三個地方用到了layer normalization,分別是embedding層后、multi-head attention后、feed forward后,這三部分的參數(shù)為768*2+12*(768*2+768*2)=38400
四 總結(jié)
綜上,BERT模型的參數(shù)總量為:
23835648 + 12*2359296(28311552)? ?+ 56623104?+? 38400? =?108808704??≈103.7M
Embedding層約占參數(shù)總量的20%,Transformer層約占參數(shù)總量的80%。文章來源:http://www.zghlxwxcb.cn/news/detail-668713.html
注:本文介紹的參數(shù)僅是BERT模型的Transformer Encoder部分的參數(shù),涉及的bias由于參數(shù)很少,本文也未計入。文章來源地址http://www.zghlxwxcb.cn/news/detail-668713.html
到了這里,關(guān)于[NLP] BERT模型參數(shù)量的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!