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

class FNNModel(nn.Module):
    def __init__(self, input_size, output_size, seq_length):
        super(FNNModel, self).__init__()
        self.nn1 = torch.nn.Sequential(
            nn.Linear(input_size*seq_length, output_size)
        )
        self.nn3 = torch.nn.Sequential(
            nn.Linear(input_size*seq_length, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 64),
            torch.nn.ReLU(),
            nn.Linear(64, output_size)
        )
        self.nn4 = torch.nn.Sequential(
            nn.Linear(input_size*seq_length, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 256),
            torch.nn.ReLU(),
            nn.Linear(256, 128),
            torch.nn.ReLU(),
            nn.Linear(128, output_size)
        )
        self.nn5 = torch.nn.Sequential(
            nn.Linear(input_size*seq_length, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 256),
            torch.nn.ReLU(),
            nn.Linear(256, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 64),
            torch.nn.ReLU(),
            nn.Linear(64, output_size)
        )
    def forward(self, x, option=1):
        # x(batch_size, seq_len, input_size)
        x = x.view(x.shape[0], -1)
        # x(batch_size, seq_len * input_size)
        if option == 1:
            x = self.nn1(x)
        elif option == 3:
            x = self.nn3(x)
        elif option == 4:
            x = self.nn4(x)
        elif option == 5:
            x = self.nn5(x)
        else:
            raise ValueError("option 输入不合法")
        return x

# 改变模型深度

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

层数 活性数据 1 活性数据 2 色谱数据
1 iloc1_epoch100_seq7_layer1_predict iloc3_epoch100_seq7_layer1_predict iloc5_epoch100_seq7_layer1_predict
3 iloc1_epoch100_seq7_layer3_predict iloc3_epoch100_seq7_layer3_predict iloc5_epoch100_seq7_layer3_predict
4 iloc1_epoch100_seq7_layer4_predict iloc3_epoch100_seq7_layer4_predict iloc5_epoch100_seq7_layer4_predict
5 iloc1_epoch100_seq7_layer5_predict iloc3_epoch100_seq7_layer5_predict iloc5_epoch100_seq7_layer5_predict

🚩 加深网络模型,会发现该模型预测会越来越符合我们的要求,也就是离峰值越来越远,有助于我们检测峰值,并将其归类为异常值。因此我们接下来要以 5 层深度继续探讨改动其他参数的影响。

# 调整训练参数

修改的是 seq_length 长度

长度 活性数据 1 活性数据 2 色谱数据
7 iloc1_epoch100_seq7_layer5_predict iloc3_epoch100_seq7_layer1_predict iloc5_epoch100_seq7_layer1_predict
9 iloc1_epoch100_seq9_layer5_predict iloc3_epoch100_seq9_layer5_predict iloc5_epoch100_seq9_layer5_predict
11 iloc1_epoch100_seq11_layer5_predict iloc3_epoch100_seq11_layer5_predict iloc5_epoch100_seq11_layer5_predict
15 iloc1_epoch100_seq15_layer5_predict iloc3_epoch100_seq15_layer5_predict iloc5_epoch100_seq15_layer5_predict

🚩 增加 seq_length 长度,对活性数据影响不大,但对色谱数据有略微影响,尤其将 seq_length 长度增加到 15,出现了严重反方向预测,偏离了数据趋势增长方向。因此,我们将 seq_length 长度定义为 9,来继续探讨其他情况的影响。

# 改变训练数据比例

只改变训练数据比例,5%,10%,15%,20% 的训练数据

各个比例数据分配情况如下图所示,红色之前的为前 5% 数据 ,蓝色之前的为前 10% 数据,黄色之前的为前 15% 数据,黑色之前的数据为 20% 数据。

训练比例图像

其中还有 10% 的验证集,用来筛选模型,10% 的验证集在训练数据后面。

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

比例 活性数据 1 活性数据 2 色谱数据
5 iloc1_epoch100_seq9_layer5_predict iloc3_epoch100_seq9_layer5_predict iloc5_epoch100_seq9_layer5_predict
10 iloc1_epoch100_seq9_layer5_predict iloc3_epoch100_seq9_layer5_predict iloc5_epoch100_seq9_layer5_predict
15 iloc1_epoch100_seq9_layer5_predict iloc3_epoch100_seq9_layer5_predict iloc5_epoch100_seq9_layer5_predict
20 iloc1_epoch100_seq9_layer5_predict iloc3_epoch100_seq9_layer5_predict iloc5_epoch100_seq9_layer5_predict

结合有效数据和上述预测图像分析,我们确定训练比例为原始数据的 5%