keras - 为什么我在输出端得到nan?

时间:2016-07-13 08:30:43

标签: theano keras

关于任务:我将班级距离作为输入,并希望获得班级置信度(数字介于0.0和1.0之间)。 所以我有类似的东西:

[
  [
    0.0,
    0.0,
    0.0,
    6.371921190238224,
    0.0,
    3.3287083713830516,
    7.085957828217146,
    7.747408965761948,
    5.498717498872398,
    5.498717498872398,
    5.498717498872398,
    5.498717498872398,
    8.529725281060978
  ],
  [
    6.396501448825533,
    0.0,
    0.0,
    5.217483270813266,
    0.0,
    5.319046151560534,
    5.823161030197735,
    3.8991256371824976,
    6.269856323952211,
    5.517874167220461,
    6.396501448825533,
    5.328678274963717,
    3.8991256371824976
  ],
]

结果

[
  [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  ...
]

我有大约200个例子。我的网络构建代码是下一个:

def train(self, distances, classes):
    """
    Train network
    :param distances: array of distances to classes
    :type distances: list[list[float]]
    :param classes: array of class indicators
    :type classes: list[list[float]]
    """
    example_count, class_count = self._dimensions(distances, classes)
    self.model = Sequential()
    self.model.add(Dense(128, input_dim=class_count))
    self.model.add(Dense(class_count))
    self.model.compile(optimizer=SGD(), loss='mse')
    self.model.fit(array(distances), array(classes))

但在训练期间,我得到下一个输出:

Epoch 1/10
425/425 [==============================] - 0s - loss: nan     
Epoch 2/10
425/425 [==============================] - 0s - loss: nan     
Epoch 3/10
425/425 [==============================] - 0s - loss: nan     
Epoch 4/10
425/425 [==============================] - 0s - loss: nan     
Epoch 5/10
425/425 [==============================] - 0s - loss: nan     
Epoch 6/10
425/425 [==============================] - 0s - loss: nan     
Epoch 7/10
425/425 [==============================] - 0s - loss: nan     
Epoch 8/10
425/425 [==============================] - 0s - loss: nan     
Epoch 9/10
425/425 [==============================] - 0s - loss: nan     
Epoch 10/10
425/425 [==============================] - 0s - loss: nan    

当我尝试使用model.predict(numpy.array([[ 0.0, 0.0, 0.0, 6.371921190238224, 0.0, 3.3287083713830516, 7.085957828217146, 7.747408965761948, 5.498717498872398, 5.498717498872398, 5.498717498872398, 5.498717498872398, 8.529725281060978]]))时(火车套装中的例子) - 我得到[[ nan nan nan nan nan nan nan nan nan nan nan nan nan]]

数据或构建代码有什么问题?

1 个答案:

答案 0 :(得分:0)

好像我有错误的适合参数(学习率和其他)。现在我有下一个代码(是的,我在测试期间将神经元添加到隐藏层并增加了火车epochscount):

    example_count, class_count = self._dimensions(distances, classes)
    self.model = Sequential()
    self.model.add(Dense(1024, input_dim=class_count))
    self.model.add(Dense(class_count))
    self.model.compile(optimizer=SGD(lr=0.002, momentum=0.0, decay=0.0, nesterov=True), loss='mse', metrics=['accuracy'])
    self.model.fit(array(distances), array(classes), nb_epoch=80)

它给出了

...
Epoch 79/80
425/425 [==============================] - 0s - loss: 0.0381 - acc: 0.6729     
Epoch 80/80
425/425 [==============================] - 0s - loss: 0.0382 - acc: 0.6871     
[[ 0.19048974  0.1585739   0.28798762 -0.23555818  0.4293299   0.10981751
-0.08614585 -0.06363138  0.05927059  0.07283521 -0.07852616 -0.02396417
-0.28515971]]

准确度不高,但主题问题已解决