Transformer——多头注意力机制(Pytorch)
创始人
2025-01-11 08:34:26
0

1. 原理图

2. 代码

import torch import torch.nn as nn   class Multi_Head_Self_Attention(nn.Module):     def __init__(self, embed_size, heads):         super(Multi_Head_Self_Attention, self).__init__()         self.embed_size = embed_size         self.heads = heads         self.head_dim = embed_size // heads          self.queries = nn.Linear(self.embed_size, self.embed_size, bias=False)         self.keys = nn.Linear(self.embed_size, self.embed_size, bias=False)         self.values = nn.Linear(self.embed_size, self.embed_size, bias=False)         self.fc_out = nn.Linear(self.embed_size, self.embed_size, bias=False)      def forward(self,queries, keys, values, mask):         N = queries.shape[0]  # batch_size         query_len = queries.shape[1]  # sequence_length         key_len = keys.shape[1]  # sequence_length          value_len = values.shape[1]  # sequence_length          queries = self.queries(queries)         keys = self.keys(keys)         values = self.values(values)          # Split the embedding into self.heads pieces         # batch_size, sequence_length, embed_size(512) -->          # batch_size, sequence_length, heads(8), head_dim(64)         queries = queries.reshape(N, query_len, self.heads, self.head_dim)         keys = keys.reshape(N, key_len, self.heads, self.head_dim)         values = values.reshape(N, value_len, self.heads, self.head_dim)          # batch_size, sequence_length, heads(8), head_dim(64) -->          # batch_size, heads(8), sequence_length, head_dim(64)         queries = queries.transpose(1, 2)         keys = keys.transpose(1, 2)         values = values.transpose(1, 2)          # Scaled dot-product attention         score = torch.matmul(queries, keys.transpose(-2, -1)) / (self.head_dim ** (1/2))          if mask is not None:             score = score.masked_fill(mask == 0, float("-inf"))         # batch_size, heads(8), sequence_length, sequence_length         attention = torch.softmax(score, dim=-1)          out = torch.matmul(attention, values)         # batch_size, heads(8), sequence_length, head_dim(64) -->         # batch_size, sequence_length, heads(8), head_dim(64) -->         # batch_size, sequence_length, embed_size(512)         # 为了方便送入后面的网络         out = out.transpose(1, 2).contiguous().reshape(N, query_len, self.embed_size)         out = self.fc_out(out)          return out       batch_size = 64 sequence_length = 10 embed_size = 512 heads = 8 mask = None  Q = torch.randn(batch_size, sequence_length, embed_size)   K = torch.randn(batch_size, sequence_length, embed_size)   V = torch.randn(batch_size, sequence_length, embed_size)    model = Multi_Head_Self_Attention(embed_size, heads) output = model(Q, K, V, mask) print(output.shape)

 

相关内容

热门资讯

热点推荐!wepoke有科技的... 热点推荐!wepoke有科技的,云扑克原来是有挂猫腻,揭秘攻略(有挂神器);云扑克软件透明挂是一个全...
我来向大家传授(WePoKe)... 我来向大家传授(WePoKe)软件透明挂(辅助挂)太坑了原来是有挂猫腻(2020已更新)(哔哩哔哩)...
专业讨论(Wepoke机制)软... 专业讨论(Wepoke机制)软件透明挂(wepoke)透明挂辅助器机器人(2024已更新)(哔哩哔哩...
最新技巧天天真人麻将有挂(辅助... 最新技巧天天真人麻将有挂(辅助挂)太坑了原来是有挂猫腻(2021已更新)(哔哩哔哩);AI智能教程细...
程序员教你!微扑克一直输,po... 程序员教你!微扑克一直输,pokermaster原来真的是有挂,必胜教程(有挂猫腻)是一款可以让一直...
第1方攻略(Wepoke开挂)... 第1方攻略(Wepoke开挂)软件透明挂(WePoKe)透明挂辅助器挂真的假的(2023已更新)(哔...
指导大家《Wepoke小程序》... 相信很多朋友都在电脑上玩过吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家带来了手机版,这...
总算了解!wpk辅助器安装,德... 《总算了解!wpk辅助器安装,德州智星菠萝其实真的是有挂,必备教程(有挂规律)》 德州智星菠萝软件透...
终于清楚(Wepoke内置)软... 终于清楚(Wepoke内置)软件透明挂(WEPOKE)外挂透明挂AI(2022已更新)(哔哩哔哩)是...
重大科普!wpk德州专用辅助器... 重大科普!wpk德州专用辅助器,德州游戏其实是有挂猫腻,解密教程(有挂辅助);1、超多福利:超高返利...