Dual Self-Attention Network (DSANet)

本文深入解析DSANet网络结构,包括全局与局部自注意力模块、线性自回归模块,阐述各参数含义及模型工作原理。

在这里插入图片描述
本文来对 DSANet 从源码的角度做自定向下的分析:

总体结构

3 个支路:全局自注意力模块、局部自注意力模块、线性自回归模块。

参数含义

参数 含义
window (int) the length of the input window size
n_multiv (int) num of univariate time series
n_kernels (int) the num of channels
w_kernel (int) the default is 1,初始通道数
local (int) 1维卷积核宽度
d_k (int) d_model / n_head
d_v (int) d_model / n_head
d_model (int) outputs of dimension
d_inner (int) the inner-layer dimension of Position-wise Feed-Forward Networks
n_layers (int) num of layers in Encoder
n_head (int) num of Multi-head
drop_prob (float) the probability of dropout
class DSANet(LightningModule):

    def __init__(self, hparams):
        """
        Pass in parsed HyperOptArgumentParser to the model
        """
        super(DSANet, self).__init__()
        ... # init variables

        # build model
        self.__build_model()


    def __build_model(self):
        """
        Layout model
        """
        self.sgsf = Single_Global_SelfAttn_Module(
            window=self.window, n_multiv=self.n_multiv, n_kernels=self.n_kernels,
            w_kernel=self.w_kernel, d_k=self.d_k, d_v=self.d_v, d_model=self.d_model,
            d_inner=self.d_inner, n_layers=self.n_layers, n_head=self.n_head, drop_prob=self.drop_prob)

        self.slsf = Single_Local_SelfAttn_Module(
            window=self.window, local=self.local, n_multiv=self.n_multiv, n_kernels=self.n_kernels,
            w_kernel=self.w_kernel, d_k=self.d_k, d_v=self.d_v, d_model=self.d_model,
            d_inner=self.d_inner, n_layers=self.n_layers, n_head=self.n_head, drop_prob=self.drop_prob)

        self.ar = AR(window=self.window)
        self.W_output1 = nn.Linear(2 * self.n_kernels, 1)
        self.dropout = nn.Dropout(p=self.drop_prob)
        self.active_func = nn.Tanh()


	def forward(self, x):
        """
        
        :param x: [batch, window, n_multiv]
        :return: output: [batch,  1, n_multiv]
        """        
        sgsf_output, *_ = self.sgsf(x)  # sgsf_output: [batch,  n_multiv, n_kernel]
        slsf_output, *_ = self.slsf(x)  # slsf_output: [batch,  n_multiv, n_kernel]
        sf_output = torch.cat((sgsf_output, slsf_output), 2)  # sf_output: [batch,  n_multiv, 2*n_kernel]
        sf_output = self.dropout(sf_output)
        
        sf_output = self.W_output1(sf_output)  # sf_output: [batch,  n_multiv, 1]
        sf_output = torch.transpose(sf_output, 1, 2)   # sf_output: [batch,  1, n_multiv]
        
        ar_output = self.ar(x)  # ar_output: [batch,  1, n_multiv]

        output = sf_output + ar_output

        return output 

全局自注意力模块

class Single_Global_SelfAttn_Module(nn.Module):

    def __init__(
            self,
            window, n_multiv, n_kernels
### Dual Attention Network 架构原理 Dual Attention Network (DAN) 是一种用于场景分割的神经网络模型,其核心在于引入了空间注意力机制和通道注意力机制来提升特征表示的能力。具体来说: - **空间注意力机制**:通过对输入特征图的空间维度施加权重,使得模型能够聚焦于重要的区域,忽略不相关的背景信息[^3]。 - **通道注意力机制**:通过分析各个通道之间的关系,强化那些有助于分类的通道,抑制无关紧要的信息。这一步骤增强了特征图对于特定语义的理解能力[^5]。 这两种机制共同作用,在保持全局感受野的同时提高了局部细节的关注度,从而显著提升了分割效果。 #### 应用领域 DAN 已经被成功应用于多个计算机视觉任务中,包括但不限于: - 场景分割:如城市街景解析、医学影像处理等领域; - 基于会话的推荐系统:利用双重稀疏注意力机制捕捉用户的短期兴趣偏好变化趋势[^2]; - 行人重识别(Person Re-ID):采用上下文感知特征序列建模以及跨视图匹配策略,实现了更精准的身份验证[^4]; #### 实现方式 以下是构建一个简单的 Dual Attention Module 的 Python 代码片段,适用于 PyTorch 框架下: ```python import torch.nn as nn import torch class SpatialAttention(nn.Module): def __init__(self, kernel_size=7): super(SpatialAttention, self).__init__() assert kernel_size in (3, 7), 'kernel size must be 3 or 7' padding = 3 if kernel_size == 7 else 1 self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): avg_out = torch.mean(x, dim=1, keepdim=True) max_out, _ = torch.max(x, dim=1, keepdim=True) scale = torch.cat([avg_out, max_out], dim=1) scale = self.conv1(scale) return x * self.sigmoid(scale) class ChannelAttention(nn.Module): def __init__(self, in_planes, ratio=16): super(ChannelAttention, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False) self.relu1 = nn.ReLU() self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x)))) max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x)))) out = avg_out + max_out return x * self.sigmoid(out) class DANModule(nn.Module): def __init__(self, channel_in): super(DANModule, self).__init__() self.channel_attention = ChannelAttention(channel_in) self.spatial_attention = SpatialAttention() def forward(self, input_tensor): ca_output = self.channel_attention(input_tensor) sa_output = self.spatial_attention(ca_output) return sa_output ``` 此模块可以在任何卷积层之后添加,以增强模型的学习能力和泛化性能。
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颹蕭蕭

白嫖?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值