Keras:Binary_crossentropy具有负值

时间:2017-02-16 03:58:48

标签: python keras

我正在跟随this tutorial(第6节:将它们全部捆绑在一起),使用我自己的数据集。我可以在教程中使用示例数据集提供示例。

我得到一个负面的二元交叉熵错误,并且没有随着时代的进展而改进。我很确定二进制交叉熵应该总是正的,我应该看到损失有所改善。我已将下面的示例输出(和代码调用)截断到5个时期。其他人似乎在训练CNN时遇到类似的问题,但在我的案例中我没有看到明确的解决方案。有谁知道为什么会这样?

示例输出:

Creating TensorFlow device (/gpu:2) -> (device: 2, name: GeForce GTX TITAN Black, pci bus id: 0000:84:00.0)
10240/10240 [==============================] - 2s - loss: -5.5378 - acc: 0.5000 - val_loss: -7.9712 - val_acc: 0.5000
Epoch 2/5
10240/10240 [==============================] - 0s - loss: -7.9712 - acc: 0.5000 - val_loss: -7.9712 - val_acc: 0.5000
Epoch 3/5
10240/10240 [==============================] - 0s - loss: -7.9712 - acc: 0.5000 - val_loss: -7.9712 - val_acc: 0.5000
Epoch 4/5
10240/10240 [==============================] - 0s - loss: -7.9712 - acc: 0.5000 - val_loss: -7.9712 - val_acc: 0.5000
Epoch 5/5
10240/10240 [==============================] - 0s - loss: -7.9712 - acc: 0.5000 - val_loss: -7.9712 - val_acc: 0.5000

我的代码:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import History

history = History()
seed = 7
np.random.seed(seed)

dataset = np.loadtxt('train_rows.csv', delimiter=",")

#print dataset.shape (10240, 64)

# split into input (X) and output (Y) variables
X = dataset[:, 0:(dataset.shape[1]-2)] #0:62 (63 of 64 columns)
Y = dataset[:, dataset.shape[1]-1]  #column 64 counting from 0

#print X.shape (10240, 62)
#print Y.shape (10240,)

testset = np.loadtxt('test_rows.csv', delimiter=",")

#print testset.shape (2560, 64)

X_test = testset[:,0:(testset.shape[1]-2)]
Y_test = testset[:,testset.shape[1]-1]

#print X_test.shape (2560, 62)
#print Y_test.shape (2560,)

num_units_per_layer = [100, 50]

### create model
model = Sequential()
model.add(Dense(100, input_dim=(dataset.shape[1]-2), init='uniform', activation='relu'))
model.add(Dense(50, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
## Fit the model
model.fit(X, Y, validation_data=(X_test, Y_test), nb_epoch=5, batch_size=128)

1 个答案:

答案 0 :(得分:9)

我应该打印出我的响应变量。类别被标记为1和2而不是0和1,这使分类器混淆。