BERT--学习

一、Transformer

Transformer,是由编码块和解码块两部分组成,其中编码块由多个编码器组成,解码块同样也是由多个解码块组成。

编码器:自注意力 + 全连接

  • **多头自注意力:**Q、K、V
  • 公式:

解码块:自注意力 + 编码 - 解码自注意力 +全连接

  • 多头自注意力
  • **编码---解码自注意力:**Q上个解码器的输出

K、V最后一个编码器输出

二、BERT

  • **bert,**是由Transformer的多个编码器组成。
  • **Base :**12层编码器,每个编码器有12个多头,隐藏维度为768。
  • Large: 24层编码器,每个编码器16个头,隐层维度为1024
  • bert结构 :
python 复制代码
import torch
class MultiHeadAttention(nn.Module):
    def__init__(self,hidden_size,head_num):
        super().__init__()
        self.head_size = hidden_size / head_num
        self.query = nn.Linear(hidden_size, hidden_size)
        self.key = nn.Linear(hidden_size, hidden_size)
        self.value = nn.Linear(hidden_size, hidden_size)
    def transpose_dim(self,x):
        x_new_shape = x.size()[:-1]+(self.head_num, head_size)
        x = x.view(*x_new_shape)
        return x.permute(0,2,1,3)

    def forward(self,x,attention_mask):
        Quary_layer = self.query(x)
        Key_layer = self.key(x)
        Value_layer = self.value(x)

        '''
        B = Quary_layer.shape[0]
        N = Quary_layer.shape[1]
        multi_quary = Quary_layer.view(B,N,self.head_num,self.head_size).transpose(1,2)
        '''
        
        multi_quary =self.transpose_dim(Quary_layer)
        multi_key =self.transpose_dim(Key_layer)
        multi_value =self.transpose_dim(Value_layer)

        attention_scores = torch.matmul(multi_quary, multi_key.transpose(-1,-2))
        attention_scores = attention_scores / math.sqrt(self.head_size)

        attention_probs = nn.Softmax(dim=-1)(attention_scores) 
        context_layer = torch.matmul(attention_probs,values_layer)
        context_layer = context_layer.permute(0,2,1,3).contiguous()
        context_layer_shape =  context_layer.size()[:-2]+(self.hidden_size)
        context_layer = cotext_layer.view(*context_layer_shape 

        return context_layer
        
        
相关推荐
孤独野指针*P20 分钟前
深度学习中的目标检测:从 PR 曲线到 AP
python·深度学习·yolo
大有数据可视化1 小时前
人工智能如何革新数据可视化领域?探索未来趋势
人工智能·信息可视化
AI technophile2 小时前
OpenCV计算机视觉实战(4)——计算机视觉核心技术全解析
人工智能·opencv·计算机视觉
云和数据.ChenGuang2 小时前
人工智能 机器学习期末考试题
开发语言·人工智能·python·机器学习·毕业设计
珊珊而川3 小时前
3.1监督微调
人工智能
我是小伍同学3 小时前
基于卷积神经网络和Pyqt5的猫狗识别小程序
人工智能·python·神经网络·qt·小程序·cnn
界面开发小八哥5 小时前
界面控件DevExpress WinForms v25.1新功能预览 - 功能区组件全新升级
人工智能·.net·界面控件·winform·devexpress
zhz52146 小时前
开源数字人框架 AWESOME-DIGITAL-HUMAN 技术解析与应用指南
人工智能·ai·机器人·开源·ai编程·ai数字人·智能体
1296004526 小时前
pytorch基础的学习
人工智能·pytorch·学习
沉默媛6 小时前
RuntimeError: expected scalar type ComplexDouble but found Float
人工智能·pytorch·深度学习