【问题标题】:LSTM network loss is nan for batch size bigger than oneLSTM 网络损失为 nan,对于大于 1 的批大小
【发布时间】:2022-01-10 00:32:19
【问题描述】:

我正在尝试使用 LSTM 网络分析 EEG 数据,我将数据分成 4 秒的片段,从而产生大约 17000 个数据样本。为此,我构建了以下网络:

def load_model():
        model = Sequential()
        model.add(LSTM(5,recurrent_dropout=0.1,activation="relu",input_shape=(data_length, number_of_channels),
                    return_sequences=True, kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.00001, l2=0.00001)))
        model.add(Dense(512, activation = 'relu'))
        model.add(Dense(512, activation = 'relu'))
        model.add(Dropout(0.2))
        model.add(Dense(units=1, activation="sigmoid"))
        model.compile(optimizer=Adam(learning_rate=0.00001,clipvalue=1.5), loss='binary_crossentropy',
                    metrics=['accuracy', F1_scores,Precision,Sensitivity,Specificity],run_eagerly=True)
        return model

在训练时,损失从前几批立即变为 nan。为避免这种情况,我尝试添加经常性 dropout、le/l2 正则化、裁剪渐变以及正常 dropout。我还尝试更改学习率和批量大小的值。唯一有效的是经常性 dropout 为 0.9,并且 l1 和 l2 得分较低(0.00001),我还必须将 LSTM 网络中的单元数从最初的 30 降低到 5。 有没有其他方法可以避免这样做而造成损失,而不会降低这么多特征并且对梯度有很高的惩罚?

我正在使用微软提供的 tensorflow-directml 和 tensorflow 版本 1.15.1 和 keras 2.7.0。

【问题讨论】:

标签: python tensorflow keras lstm gradient-exploding


【解决方案1】:

通过将 LSTM 层的内核初始化为较小的值来解决问题。这是通过更改以下行来完成的:

model.add(LSTM(5,recurrent_dropout=0.1,activation="relu",input_shape=(data_length, number_of_channels),
                    return_sequences=True, kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.00001, l2=0.00001)))

收件人:

model.add(LSTM(5,recurrent_dropout=0.2, kernel_initializer=tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.00001, seed=7)
                    ,activation="relu",input_shape=(data_length, number_of_channels),return_sequences=True))

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 2018-08-09
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2021-06-14
    • 2017-12-17
    相关资源
    最近更新 更多