6

I want to apply 1-dimensional convolution on my 29 feature input data (as in 29x1 shape). I tell Keras that input_shape=(29,1) but I get an error that it was expecting the input "to have 3 dimensions, but got array with shape (4000, 29)". Why is Keras expecting 3 dimensions?

Keras docs give this weird example of how to use input_shape:

(None, 128) for variable-length sequences with 128 features per step.

I'm not sure what they mean by variable-length sequence, but since I have 29 features I also tried (None,29) and (1,29) and got similar errors with those.

Am I misunderstanding something about what a 1-dimensional convolution does?

Here is a visual depiction of what I expect a Conv1D to do with a kernel size of 3, given 7x1 input.

[x][x][x][ ][ ][ ][ ]
[ ][x][x][x][ ][ ][ ]
[ ][ ][x][x][x][ ][ ]
[ ][ ][ ][x][x][x][ ]
[ ][ ][ ][ ][x][x][x]
Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89

1 Answers1

7

Why is Keras expecting 3 dimensions?

The three dimensions are (batch_size, feature_size, channels).

Define a 1D Conv layer

Conv1D(32, (3), activation='relu' , input_shape=( 29, 1 ))

Feed (4000, 29, 1) samples to this layer.

Simple example:

from keras import models, layers
import numpy as np

x = np.ones((10, 29, 1))
y = np.zeros((10,))
model = models.Sequential()
model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=( 29,1)))
model.add(layers.Flatten())
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer= "adam", metrics=['accuracy'])
print(model.summary())
model.fit(x,y)
Manoj Mohan
  • 5,654
  • 1
  • 17
  • 21
  • So the `batch_size` parameter is (by convention?) omitted from `input_shape`, where a definition of `(29,1)` actually means `(batch_size_omitted, 29, 1)`. And Keras refuses to eat a 1d np array, because CNN is typically used for images where we have 3 dimensions ("channels" R,G,B). Even though we only have 1 channel, we have to wrap our 1d array into a matrix of size 29x1. – Atte Juvonen May 02 '19 at 20:03
  • Yes, it is mentioned in the documentation to not include batch axis(https://keras.io/layers/convolutional/). Even for grayscale images with one channel, the channel dimension has to be specified. https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py – Manoj Mohan May 03 '19 at 05:14