Python Forum

Full Version: Issues with Shape/Reshape for CNN
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello - first time poster. I did search the forum for a related issue but did not find any. I received an error message " ValueError: Shapes (10, 40) and (10, 10) are incompatible" after trying to train a CNN model (to predict images of faces). Here is how I got to this point:

# One Hot Encode
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

print(y_train.shape) -->(200,40)
print(y_test.shape) ---> (200,40)

-----------------------------------------------
#Reshape the image arrays so that they have 4 dimensions: (number of images, width of image, height 
of image, number of channels

X_train = X_train.reshape(200, 64, 64, 1)
X_test = X_test.reshape(200, 64, 64, 1)

-----------------------------------------------

#Fit a convolutional neural network 

model1=Sequential()
model1.add(Conv2D(16,kernel_size=(3,3),strides=1,activation='relu',
                  padding='same',input_shape=(64,64,1)))
model1.add(MaxPooling2D(pool_size=(2,2),strides=2))
model1.add(Flatten())
model1.add(Dense(10,activation='softmax'))
model1.summary()

-----------------------------------------------
# Train the model

model1.compile(optimizer='adam',
               loss='categorical_crossentropy',
               metrics=['accuracy'])
model1.fit(X_train, y_train, epochs=20, batch_size=10, 
           validation_data=(X_test, y_test))
model1.evaluate(X_test, y_test, verbose=0)

-----------------------------------------------
OUTPUT: ValueError: Shapes (10, 40, 2) and (10, 10) are incompatible
Thanks in advance,