This error is not so obvious. so I have a model like this:
It looks like I need to change the input shape, input = (?, ?, ?) instead of input(None, 64) inside my LSTM() layer, but what?
EMBEDDING_DIM = 100
MAXLEN = 16
TRUNCATING = 'post'
PADDING = 'post'
OOV_TOKEN = "<OOV>"
MAX_EXAMPLES = 160000
TRAINING_SPLIT = 0.9
def create_model(vocab_size, embedding_dim, maxlen, embeddings_matrix):
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size+1, embedding_dim, input_length=maxlen, weights=[embeddings_matrix], trainable=False),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv1D(64, 6 , activation='relu'),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.LSTM(32, dropout=0.2), #, return_sequences=True),
# tf.keras.layers.LSTM(32),
tf.keras.layers.Dense(8, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model.summary()
My error is:
ValueError Traceback (most recent call last)
Input In [25], in <cell line: 2>()
1
----> 2 model = create_model(VOCAB_SIZE, EMBEDDING_DIM, MAXLEN, EMBEDDINGS_MATRIX)5 history = model.fit(train_pad_trunc_seq, train_labels, epochs=20, validation_data=(val_pad_trunc_seq, val_labels))
Input In [24], in create_model(vocab_size, embedding_dim, maxlen, embeddings_matrix)
4 def create_model(vocab_size, embedding_dim, maxlen, embeddings_matrix):
5
6
----> 8 model = tf.keras.Sequential([
9
10 tf.keras.layers.Embedding(vocab_size+1, embedding_dim, input_length=maxlen, weights=[embeddings_matrix], trainable=False),
11 tf.keras.layers.Dropout(0.2),
12 tf.keras.layers.Conv1D(64, 6 , activation=‘relu’),
13 tf.keras.layers.GlobalAveragePooling1D(),
14
15 tf.keras.layers.LSTM(32, dropout=0.2), #, return_sequences=True),
16 # tf.keras.layers.LSTM(32),
17
18 # tf.keras.layers.Dropout(0.2),
19 tf.keras.layers.Dense(8, activation=‘relu’),
20 tf.keras.layers.Dense(1, activation=‘sigmoid’)
21 ])
23 model.compile(loss=‘binary_crossentropy’,
24 optimizer=‘adam’,
25 metrics=[‘accuracy’])File ~.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\training\tracking\base.py:629, in no_automatic_dependency_tracking.._method_wrapper(self, *args, **kwargs)
627 self._self_setattr_tracking = False # pylint: disable=protected-access
628 try:
→ 629 result = method(self, *args, **kwargs)
630 finally:
631 self._self_setattr_tracking = previous_value # pylint: disable=protected-accessFile ~.conda\envs\tf-gpu\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.traceback)
—> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tbFile ~.conda\envs\tf-gpu\lib\site-packages\keras\engine\input_spec.py:214, in assert_input_compatibility(input_spec, inputs, layer_name)
212 ndim = shape.rank
213 if ndim != spec.ndim:
→ 214 raise ValueError(f’Input {input_index} of layer “{layer_name}” ’
215 ‘is incompatible with the layer: ’
216 f’expected ndim={spec.ndim}, found ndim={ndim}. ’
217 f’Full shape received: {tuple(shape)}’)
218 if spec.max_ndim is not None:
219 ndim = x.shape.rankValueError: Input 0 of layer “lstm_3” is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 64)
Thank you.