Issues with Shape/Reshape for CNN - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Issues with Shape/Reshape for CNN (/thread-35254.html) |
Issues with Shape/Reshape for CNN - moddy10 - Oct-12-2021 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 incompatibleThanks in advance, |