https://zhuanlan.zhihu.com/p/308301901 ##单头Attention的实现 ```python class ScaledDotProductAttention(nn.Module): ''' Scaled Dot-Product Attention ''' def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout) def forward(self, q, k, v, mask=None): # self.temperature是论文中的d_k ** 0.5,防止梯度过大 # QxK/sqrt(dk) attn = torch.matmul(q / self.temperature, k.transpose(2, 3)) if mask is not None: # 屏蔽不想要的输出 attn = attn.masked_fill(mask == 0, -1e9) # softmax+dropout attn = self.dropout(F.softmax(attn, dim=-1)) # 概率分布xV output = torch.matmul(attn, v) return output, attn ``` ###多头 Attention ```python class MultiHeadAttention(nn.Module): ''' Multi-Head Attention module ''' # n_head头的个数,默认是8 # d_model编码向量长度,例如本文说的512 # d_k, d_v的值一般会设置为 n_head * d_k=d_model, # 此时concat后正好和原始输入一样,当然不相同也可以,因为后面有fc层 # 相当于将可学习矩阵分成独立的n_head份 def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1): super().__init__() # 假设n_head=8,d_k=64 self.n_head = n_head self.d_k = d_k self.d_v = d_v # d_model输入向量,n_head * d_k输出向量 # 可学习W^Q,W^K,W^V矩阵参数初始化 self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False) self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False) self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False) # 最后的输出维度变换操作 self.fc = nn.Linear(n_head * d_v, d_model, bias=False) # 单头自注意力 self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5) self.dropout = nn.Dropout(dropout) # 层归一化 self.layer_norm = nn.LayerNorm(d_model, eps=1e-6) def forward(self, q, k, v, mask=None): # 假设qkv输入是(b,100,512),100是训练每个样本最大单词个数 # 一般qkv相等,即自注意力 residual = q # 将输入x和可学习矩阵相乘,得到(b,100,512)输出 # 其中512的含义其实是8x64,8个head,每个head的可学习矩阵为64维度 # q的输出是(b,100,8,64),kv也是一样 q = self.w_qs(q).view(sz_b, len_q, n_head, d_k) k = self.w_ks(k).view(sz_b, len_k, n_head, d_k) v = self.w_vs(v).view(sz_b, len_v, n_head, d_v) # 变成(b,8,100,64),方便后面计算,也就是8个头单独计算 q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2) if mask is not None: mask = mask.unsqueeze(1) # For head axis broadcasting. # 输出q是(b,8,100,64),维持不变,内部计算流程是: # q*k转置,除以d_k ** 0.5,输出维度是b,8,100,100即单词和单词直接的相似性 # 对最后一个维度进行softmax操作得到b,8,100,100 # 最后乘上V,得到b,8,100,64输出 q, attn = self.attention(q, k, v, mask=mask) # b,100,8,64-->b,100,512 q = q.transpose(1, 2).contiguous().view(sz_b, len_q, -1) q = self.dropout(self.fc(q)) # 残差计算 q += residual # 层归一化,在512维度计算均值和方差,进行层归一化 q = self.layer_norm(q) return q, attn ``` ```python class PositionwiseFeedForward(nn.Module): ''' A two-feed-forward-layer module ''' def __init__(self, d_in, d_hid, dropout=0.1): super().__init__() # 两个fc层,对最后的512维度进行变换 self.w_1 = nn.Linear(d_in, d_hid) # position-wise self.w_2 = nn.Linear(d_hid, d_in) # position-wise self.layer_norm = nn.LayerNorm(d_in, eps=1e-6) self.dropout = nn.Dropout(dropout) def forward(self, x): residual = x x = self.w_2(F.relu(self.w_1(x))) x = self.dropout(x) x += residual x = self.layer_norm(x) return x ``` ### EncoderLayer ```python class EncoderLayer(nn.Module): def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1): super(EncoderLayer, self).__init__() self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout) self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout) def forward(self, enc_input, slf_attn_mask=None): # Q K V是同一个,自注意力 # enc_input来自源单词嵌入向量或者前一个编码器输出 enc_output, enc_slf_attn = self.slf_attn( enc_input, enc_input, enc_input, mask=slf_attn_mask) enc_output = self.pos_ffn(enc_output) return enc_output, enc_slf_attn ``` ###Encoder层 ```python class Encoder(nn.Module): def __init__( self, n_src_vocab, d_word_vec, n_layers, n_head, d_k, d_v, d_model, d_inner, pad_idx, dropout=0.1, n_position=200): # nlp领域的词嵌入向量生成过程(单词在词表里面的索引idx-->d_word_vec长度的向量) self.src_word_emb = nn.Embedding(n_src_vocab, d_word_vec, padding_idx=pad_idx) # 位置编码 self.position_enc = PositionalEncoding(d_word_vec, n_position=n_position) self.dropout = nn.Dropout(p=dropout) # n个编码器层 self.layer_stack = nn.ModuleList([ EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout) for _ in range(n_layers)]) # 层归一化 self.layer_norm = nn.LayerNorm(d_model, eps=1e-6) def forward(self, src_seq, src_mask, return_attns=False): # 对输入序列进行词嵌入,加上位置编码 enc_output = self.dropout(self.position_enc(self.src_word_emb(src_seq))) enc_output = self.layer_norm(enc_output) # 作为编码器层输入 for enc_layer in self.layer_stack: enc_output, _ = enc_layer(enc_output, slf_attn_mask=src_mask) return enc_output ``` ###解码的mask ```python class ScaledDotProductAttention(nn.Module): ''' Scaled Dot-Product Attention ''' def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout) def forward(self, q, k, v, mask=None): # 假设q是b,8,10,64(b是batch,8是head个数,10是样本最大单词长度, # 64是每个单词的编码向量) # attn输出维度是b,8,10,10 attn = torch.matmul(q / self.temperature, k.transpose(2, 3)) # 故mask维度也是b,8,10,10 # 忽略b,8,只关注10x10的矩阵,其是下三角矩阵,下三角位置全1,其余位置全0 if mask is not None: # 提前算出mask,将为0的地方变成极小值-1e9,把这些位置的值设置为忽略 # 目的是避免解码过程中利用到未来信息 attn = attn.masked_fill(mask == 0, -1e9) # softmax+dropout attn = self.dropout(F.softmax(attn, dim=-1)) output = torch.matmul(attn, v) return output, attn ``` ### decoder layer ```python class DecoderLayer(nn.Module): ''' Compose with three layers ''' def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1): super(DecoderLayer, self).__init__() self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout) self.enc_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout) self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout) def forward( self, dec_input, enc_output, slf_attn_mask=None, dec_enc_attn_mask=None): # 标准的自注意力,QKV=dec_input来自目标单词嵌入或者前一个解码器输出 dec_output, dec_slf_attn = self.slf_attn( dec_input, dec_input, dec_input, mask=slf_attn_mask) # KV来自最后一个编码层输出enc_output,Q来自带有mask的self.slf_attn输出 dec_output, dec_enc_attn = self.enc_attn( dec_output, enc_output, enc_output, mask=dec_enc_attn_mask) dec_output = self.pos_ffn(dec_output) return dec_output, dec_slf_attn, dec_enc_attn ``` ### decoder 流程 ```python class Decoder(nn.Module): def __init__( self, n_trg_vocab, d_word_vec, n_layers, n_head, d_k, d_v, d_model, d_inner, pad_idx, n_position=200, dropout=0.1): # 目标单词嵌入 self.trg_word_emb = nn.Embedding(n_trg_vocab, d_word_vec, padding_idx=pad_idx) # 位置嵌入向量 self.position_enc = PositionalEncoding(d_word_vec, n_position=n_position) self.dropout = nn.Dropout(p=dropout) # n个解码器 self.layer_stack = nn.ModuleList([ DecoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout) for _ in range(n_layers)]) # 层归一化 self.layer_norm = nn.LayerNorm(d_model, eps=1e-6) def forward(self, trg_seq, trg_mask, enc_output, src_mask, return_attns=False): # 目标单词嵌入+位置编码 dec_output = self.dropout(self.position_enc(self.trg_word_emb(trg_seq))) dec_output = self.layer_norm(dec_output) # 遍历每个解码器 for dec_layer in self.layer_stack: # 需要输入3个信息:目标单词嵌入+位置编码、最后一个编码器输出enc_output # 和dec_enc_attn_mask,解码时候不能看到未来单词信息 dec_output, dec_slf_attn, dec_enc_attn = dec_layer( dec_output, enc_output, slf_attn_mask=trg_mask, dec_enc_attn_mask=src_mask) return dec_output ``` # 主要区别 > decoder 模块比encoder 模块 中多了一个 decodermask decoder会mask掉后面的信息 >