收到的标签值 1 超出了 [0, 1) 的有效范围 - Python、Keras
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44151760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Received a label value of 1 which is outside the valid range of [0, 1) - Python, Keras
提问by Tahjid Ashfaque
I am working on a simple cnn classifier using keras with tensorflow background.
我正在使用具有 tensorflow 背景的 keras 开发一个简单的 cnn 分类器。
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
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?
这是一个二元分类问题,但我不断收到对我Received a label value of 1 which is outside the valid range of [0, 1)
没有任何意义的消息。有什么建议吗?
回答by Michele Tonutti
Range [0, 1)
means every number between 0 and 1, excluding1. So 1 is not a value in the range [0, 1).
Range [0, 1)
表示 0 到 1 之间的每个数字,不包括1。所以 1 不是 [0, 1) 范围内的值。
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.
我不是 100% 确定,但问题可能是由于您选择了损失函数。对于二元分类,binary_crossentropy
应该是更好的选择。
回答by Shaili
In the last Dense layer you used model.add(Dense(1, activation='softmax'))
. Here 1 restricts its value from [0, 1)
change its shape to the maximum output label. For eg your output is from label [0,7)
then use model.add(Dense(7, activation='softmax'))
在您使用的最后一个 Dense 层中model.add(Dense(1, activation='softmax'))
。这里 1 限制它的值从[0, 1)
改变它的形状到最大输出标签。例如,您的输出来自标签,[0,7)
然后使用model.add(Dense(7, activation='softmax'))
回答by Peteris
Peculiarities of sparse categorical crossentropy
稀疏分类交叉熵的特点
The loss function sparse_categorical_crossentropy interprets the final layer in the context of classifiers as a set of probabilities for each possible class, and the output value as the number of the class. (The Tensorflow/Keras documentationgoes into a bit more detail.) So x neurons in output layer are compared against output values in the range from 0 to x-1; and having just one neuron in the output layer is an 'unary' classifier that doesn't make sense.
损失函数 sparse_categorical_crossentropy 将分类器上下文中的最后一层解释为每个可能类别的一组概率,并将输出值解释为类别的数量。(Tensorflow/Keras 文档有更详细的介绍。)因此,将输出层中的 x 个神经元与 0 到 x-1 范围内的输出值进行比较;并且在输出层只有一个神经元是一个没有意义的“一元”分类器。
If it's a classification task where you want to have output data in the form from 0 to x-1, then you can keep sparse categorical crossentropy, but you need to set the number of neurons in the output layer to the number of classes you have. Alternatively, you might encode the output in a one-hot vector and use categorical crossentropy loss function instead of sparse categorical crossentropy.
如果是分类任务,您希望输出数据的形式为从 0 到 x-1,那么您可以保持稀疏的分类交叉熵,但是您需要将输出层中的神经元数量设置为您拥有的类别数量. 或者,您可以将输出编码为 one-hot 向量,并使用分类交叉熵损失函数而不是稀疏分类交叉熵。
If it's nota classification task and you want to predict arbitrary real-valued numbers as in a regression, then categorical crossentropy is not a suitable loss function at all.
如果它不是分类任务,并且您想像在回归中那样预测任意实数值,那么分类交叉熵根本不是合适的损失函数。
回答by Barry DeCicco
Cray and Shaili's answer was correct! I had a range of outcomes from 1 to 6, and the line:
Cray 和 Shaili 的回答是正确的!我得到了从 1 到 6 的一系列结果,还有一行:
tf.keras.layers.Dense(6, activation = 'softmax')
Produced that error message, saying that things were outside of the range [0,6). I had thought that it was a labels problem (were all values present in both the training and validation label sets?), and was flogging them.
产生那个错误信息,说事情超出了范围 [0,6)。我原以为这是一个标签问题(所有值都存在于训练和验证标签集中吗?),并且正在鞭打它们。
)
)
回答by Romeo Kienzler
I had this problem when I had labels of type "float", cast them it "int" and the problem was solved...
当我有“float”类型的标签时,我遇到了这个问题,将它们转换为“int”,问题就解决了......