Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 668
user4343712 Avatar asked May 07 '16 07:05

user4343712


People also ask

Why do we use Keras in CNN?

Keras Dense class The final layers in a CNN are fully (densely) connected layers. In Keras, these layers are created using the Dense() class. The Multilayer Perceptron (MLP) part in a CNN is created using multiple fully connected layers. In Keras, a fully connected layer is referred to as a Dense layer.

What is Keras CNN?

Keras is a simple-to-use but powerful deep learning library for Python. In this post, we'll build a simple Convolutional Neural Network (CNN) and train it to solve a real problem with Keras.

How does CNN choose number of filters?

More than 0 and less than the number of parameters in each filter. For instance, if you have a 5x5 filter, 1 color channel (so, 5x5x1), then you should have less than 25 filters in that layer. The reason being is that if you have 25 or more filters, you have at least 1 filter per pixel.


2 Answers

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))
like image 106
Primoz Avatar answered Oct 19 '22 19:10

Primoz


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

like image 29
dontloo Avatar answered Oct 19 '22 20:10

dontloo