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
| import torch import torch.nn as nn
class EfficientLSTM(nn.Module): """ EfficientLSTM 模型 论文核心设计: 1. 特征压缩模块 2. 双向 LSTM 3. 通道注意力 4. 时间注意力 """ def __init__(self, config: dict): super().__init__() input_size = config.get("input_size", 18) hidden_size = config.get("hidden_size", 64) num_layers = config.get("num_layers", 2) num_classes = config.get("num_classes", 2) self.feature_compressor = nn.Sequential( nn.Linear(input_size, hidden_size), nn.BatchNorm1d(hidden_size), nn.ReLU(), nn.Dropout(0.3) ) self.bilstm = nn.LSTM( input_size=hidden_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True, dropout=0.3 ) self.channel_attention = nn.Sequential( nn.Linear(hidden_size * 2, hidden_size // 4), nn.ReLU(), nn.Linear(hidden_size // 4, hidden_size * 2), nn.Softmax(dim=-1) ) self.temporal_attention = nn.Sequential( nn.Linear(hidden_size * 2, hidden_size // 4), nn.ReLU(), nn.Linear(hidden_size // 4, 1), nn.Softmax(dim=1) ) self.classifier = nn.Sequential( nn.Linear(hidden_size * 2, hidden_size), nn.ReLU(), nn.Dropout(0.3), nn.Linear(hidden_size, num_classes) ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: x: 输入 (batch, seq_len, features) Returns: output: 分类结果 (batch, num_classes) """ batch_size, seq_len, features = x.shape x = x.view(-1, features) x = self.feature_compressor(x) x = x.view(batch_size, seq_len, -1) lstm_out, _ = self.bilstm(x) channel_weights = self.channel_attention(lstm_out.mean(dim=1)) lstm_out = lstm_out * channel_weights.unsqueeze(1) temporal_weights = self.temporal_attention(lstm_out) weighted_out = (lstm_out * temporal_weights).sum(dim=1) output = self.classifier(weighted_out) return output
def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad)
model = EfficientLSTM({"input_size": 18, "hidden_size": 64}) print(f"参数量: {count_parameters(model):.3f}M") print(f"模型大小: {count_parameters(model) * 4 / 1024 / 1024:.2f}MB")
|