简体   繁体   English

不兼容的形状 Keras NN

[英]Incompatible Shapes Keras NN

I am trying to make use of NN for 28x28 grey scale images.我正在尝试将 NN 用于 28x28 灰度图像。 My training data is shaped as follows:我的训练数据形状如下:

Reshape data重塑数据

out:
x_train.shape
(24000, 28, 28, 1)
y_train.shape
(24000, 1)

Define the keras model定义 keras model

model = Sequential()
model.add(layers.Conv2D(28, (1, 1), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(56, (1, 1), activation='relu'))
model.add(layers.Conv2D(56, (1, 1), activation='relu'))
#model.add(layers.Flatten())
#model.add(layers.Dense(56, activation='relu'))
#model.add(layers.Dense(10))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(input_x, input_y, epochs=20, batch_size=28)

_, accuracy = model.evaluate(X_validation, y_validation)
print('Accuracy: %.2f' % (accuracy*100))

Model Summary Model 总结

conv2d_86 (Conv2D)           (None, 28, 28, 28)        56        
max_pooling2d_52 (MaxPooling (None, 14, 14, 28)        0         
conv2d_87 (Conv2D)           (None, 14, 14, 56)        1624      
conv2d_88 (Conv2D)           (None, 14, 14, 56)        3192      

Total params: 4,872
Trainable params: 4,872
Non-trainable params: 0

Error InvalidArgumentError: Incompatible shapes: [28,14,14] vs. [28,1] I've applied a simple NN structure and gives out this error, ill next add flatten and dense layers once I am able to run it with these ones.错误 InvalidArgumentError:不兼容的形状:[28,14,14] vs. [28,1] 我应用了一个简单的 NN 结构并给出了这个错误,接下来我可以使用这些来运行它,然后添加扁平和密集层那些。

The last Conv2D layer output [14*14] cannot be compared with the target shape [1] to calculate the loss.最后一个Conv2D层output[14*14]不能和目标形状[1]比较来计算损失。 Hence the error.因此错误。 Generally, Conv2D layers need to be flattened and passed through a DNN (the part that you have commented) for the model architecture to be complete.通常,Conv2D 层需要展平并通过 DNN(您已评论的部分)才能完成 model 架构。 The units(neurons) in the last Dense layer is determined by the nature of the problem that you are trying to solve.最后一个 Dense 层中的单元(神经元)由您尝试解决的问题的性质决定。 Here, you have mentioned it as 10 but then you have used the loss here as binary_crossentropy which is generally used for a binary classification problem.在这里,您提到它为 10,但是您在这里使用了损失作为 binary_crossentropy,它通常用于二进制分类问题。 If you are looking to solve a multi class classification problem (where the output in your case is one of 10 classes), then you need to use the loss as 'sparse_catergorical_crossentropy' or 'categorical_crossentropy'.如果您正在寻找解决多 class 分类问题(您的案例中的 output 是 10 个类别之一),那么您需要将损失用作“sparse_catergorical_crossentropy”或“categorical_crossentropy”。 You would also need to use the 'softmax' activation function in the last dense layer for Multi class classification and the 'sigmoid' activation function for binary classification.您还需要在最后一个密集层中使用“softmax”激活 function 进行多 class 分类,使用“sigmoid”激活 function 进行二元分类。

I would suggest that a basic understanding of Covolutional Neural Networks would be beneficial before proceeding on your task.我建议在继续你的任务之前对 Covolutional Neural Networks 有一个基本的了解是有益的。

One great source that I can suggest from which I personally benefited is: http://introtodeeplearning.com/ .我可以建议我个人从中受益的一个重要来源是: http://introtodeeplearning.com/ Lecture 3 deals with CNNs but it would be good to take a look at lecture 1 before you get to lecture 3. All the best.第 3 讲涉及 CNN,但最好在第 3 讲之前先看一下第 1 讲。祝一切顺利。

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

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