【问题标题】:Keras LSTM network predicts all signals belong to the same category (among 3 different ones)Keras LSTM 网络预测所有信号属于同一类别(在 3 个不同的类别中)
【发布时间】:2020-08-11 15:58:09
【问题描述】:

我正在从事一个涉及信号分类的项目。我正在使用 keras 尝试不同的 ANN 模型,看看哪个更好,目前专注于简单的网络,但我正在努力使用 LSTM 模型,遵循以下示例:https://machinelearningmastery.com/how-to-develop-rnn-models-for-human-activity-recognition-time-series-classification/

我的输入是从电子传感器获得的一维信号,该传感器将分为 3 个不同的类别。请参阅此处的两个不同类别的信号,因此您会看到它们随着时间的推移而完全不同。 https://www.dropbox.com/s/9ctdegtuyjamp48/example_signals.png?dl=0

从一个简单模型开始,我们正在尝试以下简单模型。由于信号的长度不同,因此对它们进行了屏蔽处理,将每个信号放大到最长的信号,屏蔽值为 -1000(我们的信号中不可能出现值)。使用以下命令将数据正确地从 2D 重塑为 3D(因为 LSTM 层需要 3D),因为我只有一个特征:

Inputs = Inputs.reshape((Inputs.shape[0],Inputs.shape[1],1))

然后,将数据分为训练和验证一并输入以下模型:

model = Sequential()
model.add(Masking(mask_value=-1000, input_shape=(num_steps,1)))
model.add(LSTM(20, return_sequences=False))
model.add(Dense(15, activation='sigmoid'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])

但是,由于某种原因,每次训练网络时,它总是预测所有信号都来自同一类别,每次训练可能是不同的类别,通常是输入数据中包含更多案例的类别。如果我强制对每个类别使用相同数量的数据对网络进行训练,那么会一直给我相同的结果。

我认为这种行为不正常:可能会出现准确度不佳的情况,但这一定与模型中的一些基本错误有关,因为训练中的给定数据输入正确,没有错误在那里,多次重新检查。任何人都知道为什么会这样? 让我知道是否有更多信息对添加到这篇文章有用。

【问题讨论】:

    标签: keras classification lstm


    【解决方案1】:

    仅供任何好奇的读者参考:最后我可以通过规范化数据来解决它。

    def LSTM_input_normalize(inputs):
        new_inputs = []
        for in_ in inputs:
            if -1000 in in_:
                start_idx = np.where(in_ == -1000)[0][0]  # index of the first "-1000" in the sequence
            else:
                start_idx = in_.shape[0]
    
            # compute mean and std of the current sequence
            curr_mean = np.mean(in_[:start_idx])
            curr_std = np.std(in_[:start_idx])
    
            # normalize the single sequence
            in_[:start_idx] = (in_[:start_idx] - curr_mean) / curr_std
    
            new_inputs.append(in_)
        return np.array(new_inputs)
    

    【讨论】:

      猜你喜欢
      • 2017-05-20
      • 2015-08-11
      • 2018-05-03
      • 2021-07-24
      • 1970-01-01
      • 2021-11-08
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多