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
| import torch import torch.nn as nn
class EEGEyeFusionNet(nn.Module): """ EEG+眼动追踪混合融合网络 """ def __init__(self, eeg_channels=32, eye_features=10, num_classes=2): super().__init__() self.eeg_encoder = nn.Sequential( nn.Conv1d(eeg_channels, 64, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool1d(2), nn.Conv1d(64, 128, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool1d(2), nn.Conv1d(128, 256, kernel_size=3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool1d(1) ) self.eye_encoder = nn.Sequential( nn.Linear(eye_features, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU() ) self.cross_attention = nn.MultiheadAttention( embed_dim=256, num_heads=8, batch_first=True ) self.classifier = nn.Sequential( nn.Linear(256 + 128, 128), nn.ReLU(), nn.Dropout(0.5), nn.Linear(128, num_classes) ) def forward(self, eeg, eye_features): eeg_feat = self.eeg_encoder(eeg).squeeze(-1) eye_feat = self.eye_encoder(eye_features) eeg_feat = eeg_feat.unsqueeze(1) attn_out, _ = self.cross_attention(eeg_feat, eeg_feat, eeg_feat) eeg_feat = attn_out.squeeze(1) fused = torch.cat([eeg_feat, eye_feat], dim=1) output = self.classifier(fused) return output
|