在① import torch
import torch.nn as nn
import torch.nn.functional as F

# 为结合vit中的attention优势,设计了新的交叉注意力
class Cross_Attention(nn.Module):
    def __init__(self, input_channels, output_channels):
        super(Cross_Attention, self).__init__()
        self.query = nn.Linear(input_channels, output_channels)
        self.key = nn.Linear(input_channels, output_channels)
        self.value = nn.Linear(input_channels, output_channels)
        self.scale = output_channels ** -0.5  # 缩放因子，防止softmax的梯度消失/爆炸
    
    def forward(self, assistant, present):
        # 生成查询、键和值
        q = self.query(assistant)
        k = self.key(present)
        v = self.value(present)
        
        # 计算注意力分数
        attention_scores = torch.bmm(q, k.transpose(1, 2)) * self.scale
        attention = F.softmax(attention_scores, dim=-1)
        
        # 应用注意力机制
        attended_features = torch.bmm(attention, v)
        return attended_features

class Multi_Context(nn.Module):
    def __init__(self, input_channels, output_channels):  # 修正了def init为def init
        super(Multi_Context, self).__init__()
        self.linear1 = nn.Linear(input_channels, output_channels)
        self.linear2 = nn.Linear(input_channels, output_channels)
        self.linear3 = nn.Linear(input_channels, output_channels)
        self.linear_final = nn.Linear(output_channels * 3, output_channels)

    def forward(self, x):
        x1 = F.relu(self.linear1(x))
        x2 = F.relu(self.linear2(x))
        x3 = F.relu(self.linear3(x))
        x = torch.cat([x1, x2, x3], dim=-1)  # Concatenate along the feature dimension
        x = self.linear_final(x)
        return x

class Adaptive_Weight(nn.Module):
    def __init__(self, input_channels):  # 修正了def init为def init
        super(Adaptive_Weight, self).__init__()
        self.fc1 = nn.Linear(input_channels, input_channels // 4)
        self.fc2 = nn.Linear(input_channels // 4, input_channels)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        x_avg = torch.mean(x, dim=1)  # Global Average Pooling along the ‘sequence length’ dimension
        weight = F.relu(self.fc1(x_avg))
        weight = self.fc2(weight)
        weight = self.sigmoid(weight).unsqueeze(1)
        out = x * weight
        return out

class Counter_Guide_Enhanced(nn.Module):
    def __init__(self, input_channels, output_channels):
        super(Counter_Guide_Enhanced, self).__init__()
        self.mc = Multi_Context(input_channels, output_channels)
        self.cross_attention = Cross_Attention(output_channels, output_channels)
        # 结合自适应
        self.adaptive_weight = Adaptive_Weight(output_channels)
    
    def forward(self, x, event_x):
        # 首先使用Multi-Context增强事件特征
        enhanced_event_x = self.mc(event_x)

        # 使用Cross-Attention进行特征交互,(x和event_x互相增强)
        enhanced_x = self.cross_attention(x, enhanced_event_x)
        enhanced_event_x_via_x = self.cross_attention(event_x, enhanced_x)
        
        # 进一步通过adaptive weight调整增强的特征权重
        adaptive_enhanced_x = self.adaptive_weight(enhanced_x)
        adaptive_enhanced_event_x = self.adaptive_weight(enhanced_event_x_via_x)
        
        return adaptive_enhanced_x, adaptive_enhanced_event_x中，修改counter_guide_enhanced，原本的增强事件特征（enhanced_event_x = self.mc(event_x)）改为 增强rgb特征，然后后续的逻辑对应修改一下