Python Forum
Issues with Shape/Reshape for CNN
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with Shape/Reshape for CNN
#1
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,
Larz60+ write Oct-12-2021, 04:24 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [pandas] How to reshape the list Mekala 6 7,380 Jul-26-2020, 12:49 AM
Last Post: Mekala
  cannot reshape array of size 0 into shape Roro 2 6,261 Jun-14-2020, 11:28 AM
Last Post: Roro
  Bad input shape for SVC Scott 1 4,601 Dec-07-2019, 08:03 AM
Last Post: scidam
  reshape from 3D to 3D matrix paul18fr 0 1,736 Nov-12-2019, 11:26 AM
Last Post: paul18fr
  Simple numpy reshape error wih contour3D AdeIsHere 0 2,190 Sep-17-2019, 12:01 PM
Last Post: AdeIsHere
  ValueError: could not broadcast input array from shape (75) into shape (25) route2sabya 0 6,484 Mar-14-2019, 01:14 PM
Last Post: route2sabya
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,579 Nov-04-2018, 10:38 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020