My input is a sequence of 21 amino acids label encoded. It is an array with 21 elements (1x21). I want to feed it through an Embedding layer and then through a Convolutional layer (among others). But its not allowing me to add the convolutional layer. I get why (that the dimensions outputted and inputted don't match), but I don't know how to fix it.
This is my code:
embeded_vector_size = 9
max_length = 21
vocab_size = len(labels)
model = Sequential()
model.add(Embedding(vocab_size,embeded_vector_size,input_length=max_length, name="embedding"))
model.add(Conv2D(filters=64,activation='relu', kernel_size=(10, 3), input_shape=(21, 9, 1)))
I am getting this error:
Input 0 of layer "conv2d_17" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (None, 21, 9)
Full traceback:
ValueError Traceback (most recent call last)
Cell In[177], line 7
5 model.add(Embedding(vocab_size,embeded_vector_size,input_length=max_length, name="embedding"))
6 print(model.summary())
----> 7 model.add(Conv2D(filters=3,activation='relu', kernel_size=(10, 3), input_shape=(21, 9, 1)))
8 # print(model.summary())
9 # model.add(MaxPooling2D())
File /opt/conda/lib/python3.10/site-packages/tensorflow/python/trackable/base.py:204, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
202 self._self_setattr_tracking = False # pylint: disable=protected-access
203 try:
--> 204 result = method(self, *args, **kwargs)
205 finally:
206 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /opt/conda/lib/python3.10/site-packages/keras/src/engine/input_spec.py:253, in assert_input_compatibility(input_spec, inputs, layer_name)
251 ndim = x.shape.rank
252 if ndim is not None and ndim < spec.min_ndim:
--> 253 raise ValueError(
254 f'Input {input_index} of layer "{layer_name}" '
255 "is incompatible with the layer: "
256 f"expected min_ndim={spec.min_ndim}, "
257 f"found ndim={ndim}. "
258 f"Full shape received: {tuple(shape)}"
259 )
260 # Check dtype.
261 if spec.dtype is not None:
ValueError: Input 0 of layer "conv2d_17" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (None, 21, 9)
How to resolve it?