训练损失是 Nan – 但训练数据都在范围内,没有 null

原文标题training loss is Nan – but trainning data is all in range without null

当我执行我的model.fit(x_train_lstm, y_train_lstm, epochs=3, shuffle=False, verbose=2)

我总是输不起:

Epoch 1/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step
Epoch 2/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 74ms/step
Epoch 3/3
73/73 - 5s - loss: nan - accuracy: 0.5417 - 5s/epoch - 73ms/step

我的 x_training 形状为 (2475, 48),y_train 形状为 (2475,)

  • 我在 (2315, 160, 48) 中导出我的输入训练集,所以 2315 组训练数据,160 作为我的环回时间窗口,48 个特征
  • 相应地,y_train 在 (2315, 1) 的形状中为 0 或 1

所有在 (-1,1) 的范围内:Overview of training data

我的模型是这样的:

Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_6 (LSTM)               (None, 160, 128)          90624     
                                                                 
 dropout_4 (Dropout)         (None, 160, 128)          0         
                                                                 
 lstm_7 (LSTM)               (None, 160, 64)           49408     
                                                                 
 dropout_5 (Dropout)         (None, 160, 64)           0         
                                                                 
 lstm_8 (LSTM)               (None, 32)                12416     
                                                                 
 dense_2 (Dense)             (None, 1)                 33        
                                                                 
=================================================================
Total params: 152,481
Trainable params: 152,481
Non-trainable params: 0
  • 我尝试了不同的 LSTM 单元:48、60、128、160,它们都不起作用
  • 我检查了我的训练数据,它们都在(-1,1)的范围内
  • 我的数据集中没有“null”,x_train.isnull().values.any() 输出 False

现在不知道在哪里可以尝试更多~

我的模型代码是:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.layers import Dropout

def create_model(win = 100, features = 9):
    model = Sequential()
    model.add(LSTM(units=128, activation='relu', input_shape=(win, features),
        return_sequences=True))
    model.add(Dropout(0.1))
    model.add(LSTM(units=64, activation='relu', return_sequences=True))
    model.add(Dropout(0.2))

    # no need return sequences from 'the last layer'
    model.add(LSTM(units=32))

    # adding the output layer
    model.add(Dense(units=1, activation='sigmoid'))

    # may also try mean_squared_error
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    return model

在这里我绘制了一些 train_y 样本:y_train is 0 or 1 labels

原文链接:https://stackoverflow.com//questions/71494820/training-loss-is-nan-but-trainning-data-is-all-in-range-without-null

回复

我来回复
  • AloneTogether的头像
    AloneTogether 评论

    两件事:尝试规范化你的时间序列数据和使用relu作为lstm层的激活函数不是“传统的”。查看这篇文章以获取更多见解。一个例子:

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense, LSTM
    from tensorflow.keras.layers import Dropout
    import tensorflow as tf
    
    layer = tf.keras.layers.Normalization(axis=-1)
    x = tf.random.normal((500, 100, 9))
    y = tf.random.uniform((500, ), dtype=tf.int32, maxval=2)
    layer.adapt(x)
    
    def create_model(win = 100, features = 9):
        model = Sequential()
        model.add(layer)
        model.add(LSTM(units=128, activation='tanh', input_shape=(win, features),
            return_sequences=True))
        model.add(Dropout(0.1))
        model.add(LSTM(units=64, activation='tanh', return_sequences=True))
        model.add(Dropout(0.2))
    
        model.add(LSTM(units=32))
    
        model.add(Dense(units=1, activation='sigmoid'))
    
        model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
        return model
    
    model = create_model()
    
    model.fit(x, y, epochs=20)
    
    2年前 0条评论
  • yunfei的头像
    yunfei 评论

    2022.Mar.17 update

    经过进一步调试,我最终发现问题实际上是因为我新添加的特性包含np.inf,删除这些行后,我的问题解决了,我现在可以看到损失值

    6/6 [==============================] - 2s 50ms/step - loss: 0.6936 - accuracy: 0.5176
    

    注意,np.inf 有符号,所以确保np.inf-np.inf都被删除:

    all_dataset = all_dataset[all_dataset.feature_issue != np.inf]
    all_dataset = all_dataset[all_dataset.feature_issue != -np.inf]
    

    2022.Mar.16

    经过一些调试,我解决了我的 2 个新添加的功能实际上导致了问题。所以,问题来自数据,但与其他数据不同,我的数据不包含 nan,没有超出范围(最初我认为所有数据都需要归一化)

    但我还不能说出原因,因为它们看起来不错

    明天我会继续研究更多,不胜感激!

    enter image description here

    2年前 0条评论