简体   繁体   中英

Convolution Neural Network Expected X but got Y

I a doing a basic CNN for handwritten digits recognition. The most basic example. And my formulas should be OK but the code isnt adding up.

First I handled the data from MNIST

(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000,28,28,1)
X_train = X_train.astype('float32') / 255
X_test = X_test.reshape(10000,28,28,1)
X_test = X_test.astype('float32') / 255
X_train = to_categorical(X_train)
X_test = to_categorical(X_test)

Next I start building my CNN so that layers and inputs match...

small_ConvN_model = models.Sequential()
small_ConvN_model.add(layers.Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)))
small_ConvN_model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(26, 26, 64)))
small_ConvN_model.add(layers.Flatten())
small_ConvN_model.add(layers.Dense(10, activation='softmax'))
small_ConvN_model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

After adding the fitting function I get an error saying

Error when checking input: expected conv2d_51_input to have shape (28, 28, 1) but got array with shape (28, 28, 2)

Seeing how the first layer is (28,28,1) and the input there is wrong it has to be the problem with the format of the data. But that also doesnt make sense bc the data is reshaped to fit (28,28,1) Hence I am stuck. Also in the variable explorer it says the data is saved as 28,28,2 which makes no sense either.

You can comment this line in your code:

X_train = to_categorical(X_train) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM