'input_shape of Conv1D layer Keras
I am trying to make a CNN model for binary classification of a non-image dataset. My model/ code is working and producing very good results (accuracies are high) but I am unable to understand the input_shape
parameter for the 1st layer of Conv1D
.
The shape of X or input (here x_train_df
) is (2000, 28). It has 28 features and 2000 samples. And the shape of Y or labels (here y_train_df
) is (2000, 1).
model = Sequential()
model.add(Conv1D(filters = 64, kernel_size = 3, activation = 'relu', input_shape = (x_train_df.shape[1], 1)))
model.add(Conv1D(filters = 64, kernel_size = 3, activation = 'relu'))
model.add(MaxPooling1D(pool_size = 2))
model.add(Flatten())
model.add(Dense(100, activation = 'relu'))
model.add(Dense(1, activation = 'sigmoid'))
optimzr = Adam(learning_rate=0.005)
model.compile(loss='binary_crossentropy', optimizer=optimzr, metrics=[[tf.keras.metrics.AUC(curve="ROC", name = 'auc')], [tf.keras.metrics.AUC(curve="PR", name = 'pr')]])
# running the fitting
model.fit(x_train_df, y_train_df, epochs = 2, batch_size = 32, validation_data = (x_val_df, y_val_df), verbose = 2)
I have given input_shape
as (28, 1) (taken reference from this question).
But in Conv1D layer documentation it is written that,
When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, e.g. (10, 128) for sequences of 10 vectors of 128-dimensional vectors.
What I understood from this is the dimension of input_shape should be (2000, 1) as I have 2000 one-dimensional vectors. But giving this as input_shape
shows an error as,
ValueError: Input 0 of layer "sequential_25" is incompatible with the layer: expected shape=(None, 2000, 1), found shape=(None, 28)
So my question is what should be the correct input_shape
?
Solution 1:[1]
Let's check how "Conv1D" takes input.
>>> # The inputs are 128-length vectors with 10 timesteps, and the batch size
>>> # is 4.
>>> input_shape = (4, 10, 128)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.Conv1D(
... 32, 3, activation='relu',input_shape=input_shape[1:])(x)
>>> print(y.shape)
(4, 8, 32)
3+D tensor with shape: batch_shape + (steps, input_dim)
As seen above, there are 128 features, 10 timesteps and batch size of 4. So, Conv1D takes input as (batch_size,timesteps,features). It takes 3D input. Let's say you choose batch size as 1 for your case. You have to give input like (1,2000,28).
Solution 2:[2]
I use CNN-Conv1D for non-image dataset too. I have 188 features. So I doing this to my input : X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1],1))
#in my initialing RNN
model.add(Reshape((188, 1), input_shape(X_train[1], 1))
so the input shape (None, 188, 1)
and it's work for me ,I got 97 % of training and 91 % for testing the data with real data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | I_am |