1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| import torch import torch.nn as nn import torch.nn.functional as F
class BidirectionalDelta(nn.Module): """ 双向Delta模块 论文核心思想: - 一阶差分 Δx = x(t+1) - x(t) - 正分量 Δx+ = max(Δx, 0) → 神经激活 - 负分量 Δx- = min(Δx, 0) → 神经抑制 - 激活和抑制是不对称的,需独立建模 """ def __init__(self, in_channels: int, out_channels: int): super().__init__() self.pos_transform = nn.Conv1d( in_channels, out_channels, 1, bias=False ) self.neg_transform = nn.Conv1d( in_channels, out_channels, 1, bias=False ) self.fusion = nn.Sequential( nn.Conv1d(out_channels * 2, out_channels, 1), nn.BatchNorm1d(out_channels), nn.GELU() ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: [B, C, T] EEG信号 Returns: delta_feat: [B, C_out, T] 双向Delta特征 """ delta = x[:, :, 1:] - x[:, :, :-1] pos_delta = torch.clamp(delta, min=0) neg_delta = torch.clamp(delta, max=0) pos_feat = self.pos_transform(pos_delta) neg_feat = self.neg_transform(neg_delta) concat = torch.cat([pos_feat, neg_feat], dim=1) delta_feat = self.fusion(concat) delta_feat = F.pad(delta_feat, (1, 0), mode='replicate') return delta_feat
class GatedTemporalConvolution(nn.Module): """ 门控时序卷积模块 论文Section III-C: - 逐通道深度时序卷积(保持通道特异性) - 门控机制控制信息流 - 残差学习 """ def __init__(self, channels: int, kernel_size: int = 3, dropout: float = 0.1): super().__init__() self.gate_conv = nn.Conv1d( channels, channels, kernel_size, padding=kernel_size // 2, groups=channels ) self.gate_act = nn.Sigmoid() self.feat_conv = nn.Conv1d( channels, channels, kernel_size, padding=kernel_size // 2, groups=channels ) self.feat_act = nn.GELU() self.channel_mix = nn.Sequential( nn.Conv1d(channels, channels * 2, 1), nn.GELU(), nn.Conv1d(channels * 2, channels, 1), ) self.norm = nn.BatchNorm1d(channels) self.dropout = nn.Dropout(dropout) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: [B, C, T] Returns: out: [B, C, T] """ gate = self.gate_act(self.gate_conv(x)) feat = self.feat_act(self.feat_conv(x)) gated = gate * feat mixed = self.channel_mix(gated) out = self.norm(x + self.dropout(mixed)) return out
class DeltaGateNet(nn.Module): """ DeltaGateNet完整模型 论文架构: 1. 输入EEG → 双向Delta 2. 门控时序卷积 × N 3. 全局平均池化 4. 分类输出 """ def __init__(self, n_channels: int = 4, n_classes: int = 2, hidden_dim: int = 64, n_blocks: int = 3): super().__init__() self.input_proj = nn.Conv1d(n_channels, hidden_dim, 1) self.delta = BidirectionalDelta(hidden_dim, hidden_dim) self.blocks = nn.ModuleList([ GatedTemporalConvolution(hidden_dim) for _ in range(n_blocks) ]) self.head = nn.Sequential( nn.AdaptiveAvgPool1d(1), nn.Flatten(), nn.Linear(hidden_dim, 32), nn.GELU(), nn.Dropout(0.2), nn.Linear(32, n_classes) ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: [B, C, T] EEG信号(有限通道) Returns: logits: [B, n_classes] """ x = self.input_proj(x) x = self.delta(x) for block in self.blocks: x = block(x) return self.head(x)
if __name__ == "__main__": model = DeltaGateNet( n_channels=4, n_classes=2, hidden_dim=64, n_blocks=3 ) x = torch.randn(8, 4, 6000) output = model(x) print(f"输入: {x.shape}") print(f"输出: {output.shape}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}") print(f"模型大小: {sum(p.numel() for p in model.parameters()) * 4 / 1024:.1f} KB")
|