提问者:小点点

损失:执行回归时Keras中的NaN


我正在尝试预测一个连续的值(第一次使用神经网络)。我已经对输入数据进行了归一化。我不知道为什么我从第一个纪元开始得到损失:nan输出。

我阅读并尝试了以前对同一问题的回答中的许多建议,但没有一个对我有帮助。我的训练数据形状是:(201917,64)。这是我的代码:

model = Sequential()
model.add(Dense(100, input_dim=X.shape[1], activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))

# Output layer
model.add(Dense(1, activation='linear'))

# Construct the neural network inside of TensorFlow
model.compile(loss='mean_squared_error', optimizer='Adam')

# train the model
model.fit(X_train, y_train, epochs=10, batch_size=32,
shuffle=True, verbose=2)

共3个答案

匿名用户

以下是您可以采取的步骤,以找到问题的原因:

>

  • 确保您的数据集是它应该是的:

    • 在您的数据集中查找任何nan/inf并修复它。
    • 不正确的编码(将其转换为UTF-8)。
    • 列或行中的值无效。

    使用Dropout、BatchNormalize、L1/L2正则化、更改batch_size或将数据缩放到其他范围(例如[-1,1])来规范化模型。

    减少网络的大小。

    更改其他超参数(例如优化器或激活函数)。

    你可以检查这个和这个链接来获得额外的帮助。

  • 匿名用户

    有时,当学习率过高时,会丢失nan。一种解决方案可能是减少它。替换此代码:

    # Construct the neural network inside of TensorFlow
    model.compile(loss='mean_squared_error', optimizer='Adam')
    

    与:

    from keras.optimizers import Adam #maybe put this at the top of your file
    opt = Adam(lr=0.0001) #0.001 was the default, so try a smaller one
    model.compile(optimizer=opt, loss='mean_squared_error')
    

    看看这是否有帮助。我也会先尝试一个隐藏层,看看它是如何进行的。

    匿名用户

    输入数据帧中的NaN。在获取数据帧值之前,应替换NaN值。否则,会爆炸梯度。