0
votes

I'm currently trying to run a model with a conv1d layer as the input. And I think my issue is with the input_shape value.

model = models.Sequential()
model.add(layers.Conv1D(input_shape=(100,), kernel_size=5, filters=100, activation='relu'))
model.add(layers.MaxPooling1D())
model.add(layers.Flatten())
model.add(layers.Dense(units=3, activation='relu'))
model.add(layers.Dense(units=1, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model.fit(x_vec, y_train_encoded, epochs=10 , batch_size=64)

ValueError: Input 0 of layer sequential_27 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (None, 1, 100)

x_vec.shape = (52579, 100)

I thought my input_shape was supposed to be the input shape for a single input, which would be (100,). Is that wrong, and if so why is it wrong?

1
Input should be 3d - Muhammad hassan

1 Answers

0
votes

The input shape for a convolutional layer should be at least 2 numbers. Depending on the data_format, it should be either: (SPATIAL_DIM, NUM_CHANNELS) or (NUM_CHANNELS, SPATIAL_DIM).

In your case there are two viable solutions:

model = models.Sequential()
model.add(layers.Conv1D(input_shape=(100, 1), kernel_size=5, filters=100, activation='relu'))
# THE REST IS THE SAME

or

model = models.Sequential()
model.add(layers.Conv1D(input_shape=(1, 100), kernel_size=5, filters=100, activation='relu', data_format='channels_first'))
model.add(layers.MaxPooling1D(data_format='channels_first'))
# THE REST IS THE SAME

Notice that in the second example you have to give the MaxPool1D the same data_format. Otherwise, the max pooling will happen over the channels, which is not what you are trying to do (is it?).