Reputation: 240
I'm having this error, and from all I've seen from answers to similar questions it is due to not specifying the last layer to have 30 neurons, since your desired output is an array with size 30. But this is not my case, I've specified this, so I find it very weird that this error pops up if the number of neurons and output shape match perfectly. Below there's the part of the code that may be causing this error (images_gray
is a numpy array consisting of greyscale images with shape (2000, 128, 128) and result_array
is a list of numpy arrays of size 30).
images_gray = np.reshape(images_gray, (len(images_gray), img_rows, img_cols, 1))
to_shuffle = list(zip(images_gray, result_array))
random.shuffle(to_shuffle)
im_input, res_output = zip(*to_shuffle)
im_input, res_output = np.array(im_input[:10]), np.array(res_output[:10])
img_rows, img_cols, _ = np.shape(im_input[0])
input_shape = (img_rows, img_cols, 1)
if K.image_data_format() == 'channels_first':
input_shape = (1, img_rows, img_cols)
model = buildCNN(input_shape)
adadelta = keras.optimizers.Adadelta(lr=0.5)
model.compile(loss="sparse_categorical_crossentropy",optimizer=adadelta,metrics=['accuracy'])
model.fit(im_input, res_output, batch_size=128, epochs=10, verbose=1)
Here's the buildCNN()
function:
def buildCNN(input_shape):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(30, activation='sigmoid'))
return model
And what I get when I use model.sumary()
:
Upvotes: 0
Views: 267
Reputation: 14462
If your target (y values) have length of 30 then I assume that you want to do multiclass classification. In that case, your are using wrong activation function in your output layer which should be softmax
instead of sigmoid
.
model.add(Dense(30, activation='softmax'))
sigmoid
is used with binary classification.
And if you want to do binary classification, then your target (y values) should have a length of 1, instead of 30.
Also note that you will most likely receive another error because if your target values are one-hot encoded (assuming from the fact that they are arrays of the length of 30) then the correct loss function to pick is categorical_crossentropy
.
sparse_categorical_crossentropy
loss is used for multiclass classification when labels are provided as integers (not one-hot encoded).
So in the case of one-hot encoded labels, the compile
method should be called like this
model.compile(loss="categorical_crossentropy",optimizer=adadelta,metrics=['accuracy'])
Upvotes: 1