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

class CNNLSTM(nn.Module):
    def __init__(self, input_size, output_size):
        super(CNNLSTM, self).__init__()
        out_channels = 32
        hidden_size = 16
        self.relu = nn.ReLU(inplace=True)
        # (batch_size=30, seq_len=24, input_size=7) ---> permute(0, 2, 1)
        self.conv = nn.Sequential(
            nn.Conv1d(in_channels=input_size, out_channels=out_channels, kernel_size=3),
            nn.ReLU(),
            # nn.MaxPool1d(kernel_size=3, stride=1)
        )
        # (batch_size=30, out_channels=32, seq_len-4=20) ---> permute(0, 2, 1)
        # (30, 20, 32)
        self.lstm = nn.LSTM(input_size=out_channels, hidden_size=hidden_size,
                            batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)
    def forward(self, x, option):
        # x(batch_size, seq_len, input_size)
        x = x.permute(0, 2, 1)
        if option == 1:
            # x(batch_size, input_size, seq_len)
            x = self.conv(x)
            x = x.permute(0, 2, 1)
            x, _ = self.lstm(x)
            x = self.fc(x[:, -1, :])
        else:
            raise ValueError("option 输入不合法")
        return x

# 模型效果

本次是将 CNNLSTM 结合使用,没有对比加深模型效果,只是探讨一下模型融合后的效果。

模型 活性数据 1 活性数据 2 色谱数据
1 iloc1_epoch100_seq7_layer1_predict iloc3_epoch100_seq7_layer1_predict iloc5_epoch100_seq7_layer1_predict

😮仰天长叹,这个效果也忒好了吧,跟我们这次想要的效果不契合啊

# 调整训练参数

主要修改的是 seq_length 长度

长度 活性数据 1 活性数据 2 色谱数据
7 iloc1_epoch100_seq7_layer1_predict iloc3_epoch100_seq7_layer1_predict iloc5_epoch100_seq7_layer1_predict
9 iloc1_epoch100_seq9_layer1_predict iloc3_epoch100_seq9_layer1_predict iloc5_epoch100_seq9_layer1_predict
11 iloc1_epoch100_seq11_layer1_predict iloc3_epoch100_seq11_layer1_predict iloc5_epoch100_seq11_layer1_predict
15 iloc1_epoch100_seq15_layer1_predict iloc3_epoch100_seq15_layer1_predict iloc5_epoch100_seq15_layer1_predict

🚩 增加 seq_length 长度,整体没有什么影响,随便挑一个看看下一个效果吧,我们折中选择 seq_length 9

# 改变训练数据比例

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

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

比例 活性数据 1 活性数据 2 色谱数据
5 iloc1_epoch100_seq9_layer1_predict iloc3_epoch100_seq9_layer1_predict iloc5_epoch100_seq9_layer1_predict
10 iloc1_epoch100_seq9_layer1_predict iloc3_epoch100_seq9_layer1_predict iloc5_epoch100_seq9_layer1_predict
15 iloc1_epoch100_seq9_layer1_predict iloc3_epoch100_seq9_layer1_predict iloc5_epoch100_seq9_layer1_predict
20 iloc1_epoch100_seq9_layer1_predict iloc3_epoch100_seq9_layer1_predict iloc5_epoch100_seq9_layer1_predict

随着训练数据比例扩大,模型能够很好的拟合数据趋势,以至于一些小峰也得到了很好的拟合效果,不是我们期望的结果,因此训练比例可以确定为 5%