构建一个简单 LSTM 模型,通过加深模型深度、更改训练数据比例、调整训练参数等对比模型预测效果。

class LSTMModel(nn.Module):
    def __init__(self, input_size, output_size):
        super(LSTMModel, self).__init__()
        hidden_layer_size=100
        self.hidden_layer_size = hidden_layer_size
        self.lstm1 = nn.LSTM(input_size, hidden_layer_size, batch_first=True)
        self.lstm2 = nn.LSTM(input_size, hidden_layer_size, num_layers=2, batch_first=True)
        self.lstm3 = nn.LSTM(input_size, hidden_layer_size, num_layers=3, batch_first=True)
        self.linear = nn.Linear(hidden_layer_size, output_size)
    def forward(self, x, option=1):
        # x(batch_size, seq_len, input_size)
        # 初始化隐藏状态和细胞状态
        # [num_layers * num_directions, batch, hidden_size]
        h0 = torch.zeros(option, x.size(0), self.hidden_layer_size).to(x.device)
        c0 = torch.zeros(option, x.size(0), self.hidden_layer_size).to(x.device)
        if option == 1:
            out, _ = self.lstm1(x, (h0, c0))
        elif option == 2:
            out, _ = self.lstm2(x, (h0, c0))
        elif option == 3:
            out, _ = self.lstm3(x, (h0, c0))
        else:
            raise ValueError("option 输入不合法")        
        # 前向传播
        # out, _ = self.lstm(x, (h0, c0))
        # [batch_size, seq_length, num_directions * hidden_size]
        out = self.linear(out[:, -1, :])
        return out

# 改变模型深度

只改变模型深度,加深 LSTM 层,从 1 层加深到 3 层,对比查看测试图片效果。

层数 活性数据 1 活性数据 2 色谱数据
1 iloc1_epoch100_seq7_layer1_predict iloc3_epoch100_seq7_layer1_predict iloc5_epoch100_seq7_layer1_predict
2 iloc1_epoch100_seq7_layer3_predict iloc3_epoch100_seq7_layer3_predict iloc5_epoch100_seq7_layer3_predict
3 iloc1_epoch100_seq7_layer3_predict iloc3_epoch100_seq7_layer3_predict iloc5_epoch100_seq7_layer3_predict

🚩 加深网络模型发现模型预测越来越平滑,理论上是符合正常数据趋势,结合数据趋势和效果,选择以 2 层深度继续探讨改动其他参数的影响。但在色谱数据上,有点超乎我们的预期,在此先抛弃色谱数据,只讨论对活性数据的影响。

# 调整训练参数

主要修改的是 seq_length 长度

长度 活性数据 1 活性数据 2 色谱数据
7 iloc1_epoch100_seq7_layer2_predict iloc3_epoch100_seq7_layer2_predict iloc5_epoch100_seq7_layer2_predict
9 iloc1_epoch100_seq9_layer2_predict iloc3_epoch100_seq9_layer2_predict iloc5_epoch100_seq9_layer2_predict
11 iloc1_epoch100_seq11_layer2_predict iloc3_epoch100_seq11_layer2_predict iloc5_epoch100_seq11_layer2_predict
15 iloc1_epoch100_seq15_layer2_predict iloc3_epoch100_seq15_layer2_predict iloc5_epoch100_seq15_layer2_predict

🚩 增加 seq_length 长度,发现活性数据的预测逐渐趋于平滑,有点不利于筛选出我们想要的异常数据,对此,我们将 seq_length 长度定义为 11,来继续探讨其他情况的影响。色谱数据已经开始群魔乱舞了,暂时不管

# 改变训练数据比例

只改变训练数据比例,5%,10%,15%,20% 的训练数据,训练数据分配情况参考 FNN改变训练数据比例 部分。本小节只展示不同训练比例对该模型的影响。

根据不同比例训练得到的结果图像如下所示:

比例 活性数据 1 活性数据 2 色谱数据
5 iloc1_epoch100_seq11_layer2_predict iloc3_epoch100_seq11_layer2_predict iloc5_epoch100_seq11_layer2_predict
10 iloc1_epoch100_seq9_layer2_predict iloc3_epoch100_seq9_layer2_predict iloc5_epoch100_seq9_layer2_predict
15 iloc1_epoch100_seq11_layer2_predict iloc3_epoch100_seq11_layer2_predict iloc5_epoch100_seq11_layer2_predict
20 iloc1_epoch100_seq11_layer2_predict iloc3_epoch100_seq11_layer2_predict iloc5_epoch100_seq11_layer2_predict

🙅该模型整体构建效果不是很好,舍弃该模型的探讨。

💬可能添加 Ateention 机制效果会好一点,但不是本次探讨的重点,筛选出符合我们的模型即可。