问题描述
我正在使用带有tensorflow背景的keras进行简单的cnn分类器工作.
I am working on a simple cnn classifier using keras with tensorflow background.
def cnnKeras(training_data, training_labels, test_data, test_labels, n_dim):
print("Initiating CNN")
seed = 8
numpy.random.seed(seed)
model = Sequential()
model.add(Convolution2D(64, 1, 1, init='glorot_uniform',
border_mode='valid',input_shape=(16, 1, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 1)))
model.add(Convolution2D(32, 1, 1, init='glorot_uniform',
activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 1)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='softmax'))
# Compile model
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
model.fit(training_data, training_labels, validation_data=(
test_data, test_labels), nb_epoch=30, batch_size=8, verbose=2)
scores = model.evaluate(test_data, test_labels, verbose=1)
print("Baseline Error: %.2f%%" % (100 - scores[1] * 100))
# model.save('trained_CNN.h5')
return None
这是一个二进制分类问题,但我不断收到消息Received a label value of 1 which is outside the valid range of [0, 1)
,这对我来说没有任何意义.有任何建议吗?
It is a binary classification problem, but I keep getting the message Received a label value of 1 which is outside the valid range of [0, 1)
which does not make any sense to me. Any suggesstions?
推荐答案
Range [0, 1)
表示0到1之间的每个数字,不包括1.因此1不在[0, 1).
Range [0, 1)
means every number between 0 and 1, excluding 1. So 1 is not a value in the range [0, 1).
我不确定100%,但是问题可能是由于您选择了损失函数.对于二进制分类,binary_crossentropy
应该是一个更好的选择.
I am not 100% sure, but the issue could be due to your choice of loss function. For a binary classification, binary_crossentropy
should be a better choice.
这篇关于收到的标签值1超出有效范围[0,1)-Python,Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!