When bulding a CNN, I am getting complaints from Keras that do not make sense to me.

喜夏-厌秋 提交于 2019-12-18 12:56:37

问题


My input shape is supposed to be 100x100. It represents a sentence. Each word is a vector of 100 dimensions and there are 100 words at maximum in a sentence.

I feed eight sentences to the CNN.I am not sure whether this means my input shape should be 100x100x8 instead.

Then the following lines

Convolution2D(10, 3, 3, border_mode='same',
                       input_shape=(100, 100))

complains:

Input 0 is incompatible with layer convolution2d_1: expected ndim=4, found ndim=3

This does not make sense to me as my input dimension is 2. I can get through it by changing input_shape to (100,100,8). But the "expected ndim=4" bit just does not make sense to me.

I also cannot see why a convolution layer of 3x3 with 10 filters do not accept input of 100x100.

Even I get thru the complains about the "expected ndim=4". I run into problem in my activation layer. There it complains:

Cannot apply softmax to a tensor that is not 2D or 3D. Here, ndim=4

Can anyone explain what is going on here and how to fix it? Many thanks.


回答1:


I had the same problem and I solved it adding one dimension for channel to input_shape argument.

I suggest following solution:

Convolution2D(10, 3, 3, border_mode='same', input_shape=(100, 100, 1))



回答2:


the missing dimension for 2D convolutional layers is the "channel" dimension.

For image data, the channel dimension is 1 for grayscale images and 3 for color images.

In your case, to make sure that Keras won't complain, you could use 2D convolution with 1 channel, or 1D convolution with 100 channels.

Ref: http://keras.io/layers/convolutional/#convolution2d



来源:https://stackoverflow.com/questions/37085653/when-bulding-a-cnn-i-am-getting-complaints-from-keras-that-do-not-make-sense-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!