@SanthoshSankar , my apologies - I am reviewing my code on a model I have that uses Conv1D - I was sure that it was receiving the 2D tensor, and after looking at the preprocessing function I built, I do have a condition where I reshape the 2D to 3D. My line of code reads like so:
if len(x.shape) == 2:
x = x[np.newaxis, :, :]
For some reason I was sure Conv1D was taking my 2D and had forgotten about it.
However, when using Conv1D as the first layer in the CNN model, it may accept the 2D vector, but you need to declare the shape. Please see this link on the keras documentation:
Keras Conv1D
It mentions:
" 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, or (None, 128)
for variable-length sequences of 128-dimensional vectors."
So putting all together:
input_shape = (10, 128)
x = tf.random.normal(input_shape)
if len(x.shape) == 2:
x = x[np.newaxis, :, :]
y = tf.keras.layers.Conv1D(32, 3, activation=‘relu’, input_shape=input_shape[2:])(x)
print(y.shape)
Note: the reshaping I originally did it in a separate, preprocess, method. I have included it inline with the call to keras for illustration.
Again, my apologies for the confusion.
Juan