问题
I am trying to build a CNN network and wuld like to probe the layer dimention using output_shape. But it's giving me an error as follows:
ValueError: Input 0 is incompatible with layer conv2d_5: expected ndim=4, found ndim=2
Below is the code I am trying to execute
from keras.layers import Activation
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
print(model.output_shape)
回答1:
You can check if by default the number of channels is specified at the end
from keras import backend as K
print(K.image_data_format()) # print current format
On my system, this prints "channel_last", which means the last number of your input_shape
(28), is the number of channels and 1 is the number of rows.
This is also why Keras is giving the error as you cannot apply a 3 x 3 convolution mask to an image with only 1 row (with default padding set to "valid").
Most likely you want to set input_shape
to be (28, 28, 1)
.
On a separate note, if you want the kernel to be a 3 x 3 kernel, then it should be
model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
What you currently have is a convolutional layer with kernel that is size 3 x 3 and stride 3.
来源:https://stackoverflow.com/questions/54877516/valueerror-input-0-is-incompatible-with-layer-conv2d-5-expected-ndim-4-found