Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras 1D convolution input shape

I am trying to create a model for 1D convolution, but I cant seem to get the input shape correct. Here is what I have:

#this is actually shape (6826, 9000) but I am shortening it
train_dataset_x = np.array([[0, 1, 5, 1, 10], [0, 2, 4, 1, 3]])
#this is actually shape (6826, 1)
train_dataset_y = np.array([[0], [1]])

model.add(Conv1D(32, 11, padding='valid', activation='relu', strides=1, input_shape=( len(train_dataset_x[0]), train_dataset_x.shape[1]) ))
model.add(Conv1D(32, 3, padding='valid', activation='relu', strides=1) )
model.add(MaxPooling1D())

model.add(Conv1D(64, 3, padding='valid', activation='relu', strides=1) )
model.add(Conv1D(64, 3, padding='valid', activation='relu', strides=1) )
model.add(MaxPooling1D())


model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

I get this error:

ValueError: Error when checking input: expected conv1d_1_input to have 3 dimensions, but got array with shape (6826, 9000)

Anyone have suggestions?

like image 508
freed-radical Avatar asked Aug 02 '17 21:08

freed-radical


1 Answers

Input to keras.layers.Conv1D should be 3-d with dimensions (nb_of_examples, timesteps, features). I assume that you have a sequence of length 6000 with 1 feature. In this case:

X = X.reshape((-1, 9000, 1))

Should do the job.

like image 117
Marcin Możejko Avatar answered Nov 04 '22 21:11

Marcin Możejko