在keras多元NN中获得nan培训成本

时间:2018-08-03 18:26:44

标签: python pandas tensorflow keras

我正在训练一个神经网络,使其具有6个输入和2个输出。我正在将Keras与Tensorflow后端一起使用。经过预处理,这是我的代码:

training_examples = features.head(2584)
training_targets = targets.head(2584)

validation_examples = features.tail(650)
validation_targets = targets.tail(650)

model = Sequential()
model.add(Dense(12, input_dim=6))
model.add(Dense(8))
model.add(Dense(8))
model.add(Dense(2))
model.compile(loss='mse', optimizer='sgd')
print("Training--------")
for step in range(500):
  cost = model.train_on_batch(training_examples, training_targets)
  if step % 100 == 0:
    print('train cost: ', cost)

每次运行此命令都会得到类似

的输出
Training--------
train cost:  6670.4097
train cost:  nan
train cost:  nan
train cost:  nan
train cost:  nan

第一次培训的费用通常在2000-14000之间变化。数字上的特征和目标均小于100。我不确定为什么会这样。

编辑:我添加了features.info()targets.info()来检查是否为空值,数据帧中没有空值。

<class 'pandas.core.frame.DataFrame'> Int64Index: 3231 entries, 0 to 3230 Data columns (total 6 columns): TBRG_Rain_infield 3231 non-null float64 numRange_infield 3231 non-null float64 Air_T_edge 3231 non-null float64 RH_edge 3231 non-null float64 TBRG_Rain_edge 3231 non-null float64 numRange_edge 3231 non-null float64 dtypes: float64(6) memory usage: 176.7 KB <class 'pandas.core.frame.DataFrame'> Int64Index: 3231 entries, 0 to 3230 Data columns (total 2 columns): Air_T 3231 non-null float64 RH 3231 non-null float64 dtypes: float64(2) memory usage: 75.7 KB

1 个答案:

答案 0 :(得分:1)

您的数据框看起来正确,但是您可能应该将输入要素缩放为介于0和1之间或均值0和单位方差。我尝试重现您的示例,一次缩放,一次缩放。

不缩放:

from keras.models import Sequential
from keras.layers import Dense
import pandas as pd
import numpy as np

features = pd.DataFrame(np.random.randint(0, 100, size=(1000, 6)).astype(float))
targets = pd.DataFrame(np.random.rand(1000, 2), dtype=np.float64)

training_examples = features.head(100)  
training_targets = targets.tail(100)

model = Sequential()
model.add(Dense(12, input_dim=6))
model.add(Dense(8))
model.add(Dense(8))
model.add(Dense(2))
model.compile(loss='mse', optimizer='sgd')
print("Training--------")
for step in range(500):
    cost = model.train_on_batch(training_examples, training_targets)
    if step % 100 == 0:
        print('train cost: ', cost)

提供输出:

Training--------
train cost:  6834.277
train cost:  nan
train cost:  nan
train cost:  nan
train cost:  nan

但是,如果我将功能初始化为介于0和1之间:

features = pd.DataFrame(np.random.rand(1000, 6), dtype=np.float64)

这是输出:

Training--------
train cost:  1.1240386
train cost:  0.09793612
train cost:  0.08868038
train cost:  0.084703445
train cost:  0.0826226

您可以通过scikit-learn查看StandardScaler,以扩展数据。