Received label value 1, which is out of range [0, 1] - Python, Keras
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
This is a binary classification problem, but I keep getting a message Received a label value of 1 which is outside the valid range of [0, 1)
that doesn't make any sense to me. Any suggestions?
source to share
In the last Dense layer you used model.add(Dense(1, activation='softmax'))
. Here 1 limits its value from [0, 1)
changing its shape to the maximum output label. For example if you are model.add(Dense(7, activation='softmax'))
from a label [0,7)
then usemodel.add(Dense(7, activation='softmax'))
source to share