简体   繁体   English

卷积神经网络期望 X 但得到 Y

[英]Convolution Neural Network Expected X but got Y

I a doing a basic CNN for handwritten digits recognition.我正在做一个用于手写数字识别的基本 CNN。 The most basic example.最基本的例子。 And my formulas should be OK but the code isnt adding up.我的公式应该没问题,但代码没有加起来。

First I handled the data from MNIST首先我处理了来自 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...接下来我开始构建我的 CNN,以便层和输入匹配......

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添加配件 function 后,我收到一条错误消息

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.看看第一层是怎样的(28,28,1),那里的输入是错误的,那一定是数据格式的问题。 But that also doesnt make sense bc the data is reshaped to fit (28,28,1) Hence I am stuck.但这也没有意义,因为数据被重新调整以适应 (28,28,1) 因此我被卡住了。 Also in the variable explorer it says the data is saved as 28,28,2 which makes no sense either.同样在变量资源管理器中,它说数据保存为 28,28,2,这也没有任何意义。

You can comment this line in your code:您可以在代码中注释此行:

X_train = to_categorical(X_train) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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