【问题标题】:Loss: NaN in Keras while performing regression损失:执行回归时 Keras 中的 NaN
【发布时间】:2019-05-07 12:39:44
【问题描述】:

我正在尝试预测一个连续值(第一次使用神经网络)。我已经对输入数据进行了标准化。我不明白为什么我从第一个纪元开始得到loss: 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)

【问题讨论】:

标签: python tensorflow keras deep-learning linear-regression


【解决方案1】:

您可以采取以下步骤来找出问题的原因:

  1. 确保您的数据集是应有的:

    • 在您的数据集中查找任何 nan/inf 并修复它。
    • 编码不正确(将其转换为 UTF-8)。
    • 您的列或行中的值无效。
  2. 使用 Dropout、BatchNormalization、L1/L2 regularization 标准化您的模型,更改您的 batch_size,或将您的数据缩放到其他范围(例如 [-1, 1])。

  3. 缩小网络规模。

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

您可以查看this 和this 链接以获得额外帮助。

【讨论】:

  • 第一项解决了我的问题。我的数据集中有NaN 值,我没有注意到。谢谢!
【解决方案2】:

当学习率太高时,有时会出现 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')

看看是否有帮助。我也会先尝试一个隐藏层,看看效果如何。

【讨论】:

  • 使用上述参数为 lstm 顺序添加多个密集层会给我 Nan,如果没有其他变化的话。大家有什么建议吗?如果我的输出只有一列,我是否需要多个密集层?
【解决方案3】:

NaN 在输入数据框中。在获取数据框值之前,应替换 NaN 值。否则会爆炸渐变。

【讨论】:

  • 没错! df.isnull().sum()
猜你喜欢
  • 2020-12-30
  • 2021-03-26
  • 2018-11-13
  • 2021-12-17
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 2021-09-06
  • 2018-11-07
相关资源
最近更新 更多