Attention Free Transformer (AFT)-2020论文笔记


名称:

Attention Free Transformer (AFT)

来源:

2105.14103 An Attention Free Transformer

相关工作:

#Approximatingthedotproduct #Sparselocalattention #Contextcompression #Eliminatingdotproductattention #MLPsforvision

创新点:

贡献:

  • 提出了一种全新的注意力机制替代方案,完全摒弃了点积注意力。

  • AFT的计算复杂度与输入长度和特征维度呈线性关系,适用于大规模数据。

  • AFT-local和AFT-conv变体通过引入局部性和空间权重共享,进一步提高了模型的效率和性能。

代码:

python 复制代码
# ---------------------------------------  
# 论文:An Attention Free Transformer (arxiv2021)  
# ---------------------------------------  
import torch  
from torch import nn  
from torch.nn import init  
  
  
class AFT_FULL(nn.Module):  
  
    def __init__(self, d_model, n=49, simple=False):  
  
        super(AFT_FULL, self).__init__()  
        self.fc_q = nn.Linear(d_model, d_model)  
        self.fc_k = nn.Linear(d_model, d_model)  
        self.fc_v = nn.Linear(d_model, d_model)  
        if (simple):  
            self.position_biases = torch.zeros((n, n))  
        else:  
            self.position_biases = nn.Parameter(torch.ones((n, n)))  
        self.d_model = d_model  
        self.n = n  
        self.sigmoid = nn.Sigmoid()  
  
        self.init_weights()  
  
    def init_weights(self):  
        for m in self.modules():  
            if isinstance(m, nn.Conv2d):  
                init.kaiming_normal_(m.weight, mode='fan_out')  
                if m.bias is not None:  
                    init.constant_(m.bias, 0)  
            elif isinstance(m, nn.BatchNorm2d):  
                init.constant_(m.weight, 1)  
                init.constant_(m.bias, 0)  
            elif isinstance(m, nn.Linear):  
                init.normal_(m.weight, std=0.001)  
                if m.bias is not None:  
                    init.constant_(m.bias, 0)  
  
    def forward(self, input):  
  
        bs, n, dim = input.shape  
  
        q = self.fc_q(input)  # bs,n,dim  
        k = self.fc_k(input).view(1, bs, n, dim)  # 1,bs,n,dim  
        v = self.fc_v(input).view(1, bs, n, dim)  # 1,bs,n,dim  
  
        numerator = torch.sum(torch.exp(k + self.position_biases.view(n, 1, -1, 1)) * v, dim=2)  # n,bs,dim  
        denominator = torch.sum(torch.exp(k + self.position_biases.view(n, 1, -1, 1)), dim=2)  # n,bs,dim  
  
        out = (numerator / denominator)  # n,bs,dim  
        out = self.sigmoid(q) * (out.permute(1, 0, 2))  # bs,n,dim  
  
        return out  
  
  
# 输入 B C N,  输出 B C Nif __name__ == '__main__':  
    block = AFT_FULL(d_model=512, n=64).cuda()  
    input = torch.rand(64, 64, 512).cuda()  
    output = block( input)  
    print(input.size(), output.size())
    
相关推荐
EW Frontier22 分钟前
拆解|多尺度复数卷积,把调制识别准确率又往上推了 3 个点【文末附代码链接】
transformer·调制识别·无人机信号识别·辐射源识别
ZhouDevin33 分钟前
算法论文/数据集3——CLD(TMLR2025)压缩训练集,仅保留对验证集有益的样本
人工智能·深度学习·算法·计算机视觉
船厂电气自动化ai大模型2 小时前
AI大模型与数学/第63课:矩阵定义、矩阵加法、标量乘法(逐级精讲)
数据结构·人工智能·深度学习·线性代数·算法
Jialu.3 小时前
从 102M 到 34M:BERT 到 BiLSTM 的完整知识蒸馏实现
深度学习·nlp·bert
YOLO数据集集合3 小时前
排球动作识别数据集 | 排球动作 体育分析 动作识别9041期
深度学习·数据集·动作识别·排球动作识别·运动动作识别·体育分析·排球动作
Mid_search4 小时前
高估问题、Target Network、Double DQN
人工智能·深度学习·强化学习·double dqn·bootstrapping·target network
YOLO数据集集合4 小时前
隧道病害检测数据集 | 隧道检测 裂缝识别 渗水监测 剥落检测9037期
人工智能·深度学习·yolo·隧道·裂缝检测·裂缝识别·隧道病害
张继雁5 小时前
不同类型磨削液使用安全注意事项
人工智能·深度学习·安全·业界资讯·远程工作·空间计算
tangjiawen101796 小时前
海外竞争新格局下,中国汽车的真正分水岭
论文阅读
小王2047 小时前
Day 32:DeepLab系列与语义分割进阶
深度学习·神经网络·计算机视觉