为什么二进制Keras CNN总是预测1? [英] Why does a binary Keras CNN always predict 1?
问题描述
我想使用Keras CNN构建一个二进制分类器. 我大约有6000行输入数据,如下所示:
I want to build a binary classifier using a Keras CNN. I have about 6000 rows of input data which looks like this:
>> print(X_train[0])
[[[-1.06405307 -1.06685851 -1.05989663 -1.06273152]
[-1.06295958 -1.06655996 -1.05969803 -1.06382503]
[-1.06415248 -1.06735609 -1.05999593 -1.06302975]
[-1.06295958 -1.06755513 -1.05949944 -1.06362621]
[-1.06355603 -1.06636092 -1.05959873 -1.06173742]
[-1.0619655 -1.06655996 -1.06039312 -1.06412326]
[-1.06415248 -1.06725658 -1.05940014 -1.06322857]
[-1.06345662 -1.06377347 -1.05890365 -1.06034568]
[-1.06027557 -1.06019084 -1.05592469 -1.05537518]
[-1.05550398 -1.06038988 -1.05225064 -1.05676692]]]
>>> print(y_train[0])
[1]
然后我通过这种方式构建了一个CNN:
And then I've build a CNN by this way:
model = Sequential()
model.add(Convolution1D(input_shape = (10, 4),
nb_filter=16,
filter_length=4,
border_mode='same'))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dropout(0.2))
model.add(Convolution1D(nb_filter=8,
filter_length=4,
border_mode='same'))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(1))
model.add(Activation('softmax'))
reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.9, patience=30, min_lr=0.000001, verbose=0)
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(X_train, y_train,
nb_epoch = 100,
batch_size = 128,
verbose=0,
validation_data=(X_test, y_test),
callbacks=[reduce_lr],
shuffle=True)
y_pred = model.predict(X_test)
但是它返回以下内容:
>> print(confusion_matrix(y_test, y_pred))
[[ 0 362]
[ 0 608]]
为什么所有预测都是预测? CNN为什么表现这么差? 以下是损失和acc图表:
Why all predictions are ones? Why does the CNN perform so bad? Here are the loss and acc charts:
推荐答案
由于您网络中的输出,它始终会预测一个.您具有一个带有一个神经元的密集层,并具有Softmax激活. Softmax通过每个输出的指数总和进行归一化.由于只有一个输出,因此唯一可能的输出是1.0.
It always predicts one because of the output in your network. You have a Dense layer with one neuron, with a Softmax activation. Softmax normalizes by the sum of exponential of each output. Since there is one output, the only possible output is 1.0.
对于二进制分类器,您可以使用Sigmoid激活并损失"binary_crossentropy",也可以在最后一层放置两个输出单元,继续使用softmax并将损耗更改为categorical_crossentropy.
For a binary classifier you can either use a sigmoid activation with the "binary_crossentropy" loss, or put two output units at the last layer, keep using softmax and change the loss to categorical_crossentropy.
这篇关于为什么二进制Keras CNN总是预测1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!