4
我跟隨this tutorial(第6部分:將它捆綁在一起),用我自己的數據集。我可以通過提供的示例數據集來獲得教程中的示例,沒有任何問題。Keras:Binary_crossentropy具有負值
我收到了一個二進制交叉熵錯誤,它是負面的,並且隨着時代的發展而沒有任何改進。我很確定二元交叉熵應該總是正面的,我應該看到損失有所改善。我已將截止到下面的示例輸出(和代碼調用)截至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)
在我的情況,我有一個autoencoder與這個問題。事實證明,我的矩陣中有4個列的值大於1,而剩餘的284列的範圍是[0,1]。重新設置大於1的值(使用最大值),修復了我的問題。 – shadi
我也想分享我的情況。我使用CNN U-Net訓練圖像分割w /交叉二元熵。我碰巧把我的口罩從True/False轉換成了255/0,這讓分類器感到困惑,並造成了負面的損失。 –