背景
这里主要比较了bert-base和albert-tiny。前者窄而深,后者宽而浅。需要注意的一点是,albert是对bert瘦了身,但只省了空间,没有省时间。因为虽然不同层共享了参数,但模型还是要经过这么多层算一遍。
假设词表大小为V,句子长度为S,词向量维度为H,层数为L,自注意力头数为A,对于ALBert,其嵌入分解维度为E。模型的下游任务为多分类,标签数为K。
参数设置
不同模型参数 V S H L E bert-base 30000 512 768 12 − albert-tiny 30000 512 1024 4 128 \begin{array}{c|c|c|c|c|c|} \hline \text { 不同模型参数 } & \mathbf{V} & \mathbf{S} & \mathbf{H} & \mathbf{L} & \mathbf{E} \\ \hline \text { bert-base } & 30000 & 512 & 768 & 12 & - \\ \hline \text { albert-tiny } & 30000 & 512 & 1024 & 4 & 128 \\ \hline \end{array} 不同模型参数 bert-base albert-tiny V3000030000S512512H7681024L124E−128
参数量比较
不同模型参数量 嵌入初始化 自注意力机制 前向网络传播 嵌入正则 下游任务 bert-base V × H + S × H + 2 H 3 L × H 2 9 L × H 2 ( 4 L + 2 ) × H H × K albert-tiny V × E + E × H + S × H + 2 H 3 H 2 9 H 2 6 H H × K \begin{array}{c|c|c|c|c|c|c} \hline \text { 不同模型参数量 } & \text { 嵌入初始化 } & \text { 自注意力机制 } & \text { 前向网络传播 } & \text { 嵌入正则 } & \text { 下游任务 } \\ \hline \text { bert-base } & \mathrm{V}\times \mathrm{H}+\mathrm{S}\times \mathrm{H}+2 \mathrm{H} & 3 \mathrm{~L}\times \mathrm{H}^2 & 9 \mathrm{~L}\times \mathrm{H}^2 & (4 \mathrm{~L}+2)\times \mathrm{H} & \mathrm{H}\times \mathrm{~K} \\ \hline \text { albert-tiny } & \mathrm{V}\times \mathrm{E}+\mathrm{E}\times \mathrm{H}+\mathrm{S}\times \mathrm{H}+2 \mathrm{H} & 3 \mathrm{H}^2 & 9 \mathrm{H}^2 & 6 \mathrm{H} & \mathrm{H}\times \mathrm{~K} \\ \hline \end{array} 不同模型参数量 bert-base albert-tiny 嵌入初始化 V×H+S×H+2HV×E+E×H+S×H+2H 自注意力机制 3 L×H23H2 前向网络传播 9 L×H29H2 嵌入正则 (4 L+2)×H6H 下游任务 H× KH× K
运行时间比较
不同模型时间复杂度 嵌入初始化 自注意力机制 前向网络传播 嵌入正则 下游任务 bert-base O ( ( V + S ) H ) O ( L H 2 ) O ( L H 2 ) O ( L H ) O ( H K ) albert-tiny O ( V E H ) + O ( V E ) + O ( S H ) O ( L H 2 ) O ( L H 2 ) O ( L H ) O ( H K ) \begin{array}{c|c|c|c|c|c} \hline \text { 不同模型时间复杂度 } & \text { 嵌入初始化 } & \text { 自注意力机制 } & \text { 前向网络传播 } & \text { 嵌入正则 } & \text { 下游任务 } \\ \hline \text { bert-base } & \mathrm{O}((\mathrm{V}+\mathrm{S}) \mathrm{H}) & \mathrm{O}\left(\mathrm{LH}^2\right) & \mathrm{O}\left(\mathrm{LH}^2\right) & \mathrm{O}(\mathrm{LH}) & \mathrm{O}(\mathrm{HK}) \\ \hline \text { albert-tiny } & \mathrm{O}(\mathrm{VEH})+\mathrm{O}(\mathrm{VE})+\mathrm{O}(\mathrm{SH}) & \mathrm{O}\left(\mathrm{LH}^2\right) & \mathrm{O}\left(\mathrm{LH}^2\right) & \mathrm{O}(\mathrm{LH}) & \mathrm{O}(\mathrm{HK}) \\ \hline \end{array} 不同模型时间复杂度 bert-base albert-tiny 嵌入初始化 O((V+S)H)O(VEH)+O(VE)+O(SH) 自注意力机制 O(LH2)O(LH2) 前向网络传播 O(LH2)O(LH2) 嵌入正则 O(LH)O(LH) 下游任务 O(HK)O(HK)
分析
- 词嵌入所需的参数为Token embedding,Position embedding,Segment embedding【对应数据集级别、句子级别、样例级别】: V × H + S × H + 2 H \mathrm{V}\times \mathrm{H}+\mathrm{S}\times \mathrm{H}+2 \mathrm{H} V×H+S×H+2H
- 自注意力机制所需的参数为【3对应QKV】: L × 3 × A × ( H × H A ) = 3 L × H 2 L \times 3 \times A \times (H \times \frac{H}{A})=3L\times H^2 L×3×A×(H×AH)=3L×H2
- Forward网络所需参数为: L × ( H × H + H × 4 H + 4 H × H ) = 9 L × H 2 L\times(H \times H+H\times4H+4H\times H)= 9L\times H^2 L×(H×H+H×4H+4H×H)=9L×H2
- LN层所需参数为(在embedding和block都有用到,有weight和bias两个参数,每个块包含两个LN): 2 H + L × 2 × 2 H 2H+L \times 2\times 2H 2H+L×2×2H