Windows11更新后Chrome无法打开解决方案

时间:2025-04-22 08:12:10
算法手撕面经系列(1)--手撕多头注意力机制

Al_Pacino_: [code=python] def forward(self,X,causal=True): B, L, C = Q = (X) K = (X) V = (X) # 将 QKV转成 (B,n_head,L,)的形状 Q = (B,L,self.n_head,-1).permute(0,2,1,3) K = (B,L,self.n_head,-1).permute(0,2,1,3) V = (B,L,self.n_head,-1).permute(0,2,1,3) atten = ((2,3)) / **(1/2) if causal: mask = ((L,L),diagonal=1) atten = torch.masked_fill(atten,(),-) atten = (atten, dim=-1) atten = (atten) out = (atten, V) # 转成(B,L,n_head,)格式 out = (0,2,1,3).reshape(B,L,-1) out = self.W_concat(out) return out [/code]