'ValueError: Shapes (None, 200, 3) and (1, 3) are incompatible

This is the model that I am trying to train for identifying possible tag(out of three tags) for each word, also I have added a layer from another model whose output shape is [1, 100]tensors and then I have concatenate it with BiLSTM output-

input1_entity = Input(shape = (200,))

last_hidden_layer_output = last_hidden_layer(tensorflow.reshape(input1_entity, [1, 200]))

embedding_entity = Embedding((4817), 200, input_length = 200, weights = [embedding_matrix], trainable = False)(input1_entity)
bilstm1_entity = Bidirectional(LSTM(100, return_sequences = True, recurrent_dropout = 0.2), merge_mode = 'concat')(embedding_entity)
lstm1_entity = Bidirectional(LSTM(100, return_sequences = True, dropout = 0.5, recurrent_dropout = 0.2))(bilstm1_entity)
lstm2_entity = Bidirectional(LSTM(50))(lstm1_entity)

merge_layer = concatenate([lstm2_entity, last_hidden_layer_output])

dense1_entity = Dense(128, activation = 'relu')(merge_layer)

dense2_entity = Dense(128, activation = 'relu')(dense1_entity)
dropout1_entity = Dropout(0.5)(dense2_entity)

dense3_entity = Dense(64, activation = 'tanh')(dropout1_entity)
    
output1_entity = Dense(3, activation = 'softmax')(dense3_entity)
    
model_entity = Model(inputs = input1_entity, outputs = output1_entity)
    
model_entity.compile(
    loss = 'categorical_crossentropy',
    optimizer = 'adam',
    metrics = [tensorflow.keras.metrics.CategoricalAccuracy()],
    sample_weight_mode = 'temporal'
)

And this is how I am training the model -

history = model_entity.fit(pad_tokens_train, 
                    np.array(pad_tags_train),
                    batch_size=250, 
                    verbose=1, 
                    epochs=50,
                    sample_weight = sample_weight,
                    validation_split=0.2)

But I keep on getting this error -

ValueError: in user code:

    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/engine/training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/engine/training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/engine/training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/engine/training.py", line 809, in train_step
        loss = self.compiled_loss(
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/losses.py", line 1664, in categorical_crossentropy
        return backend.categorical_crossentropy(
    File "/Users/kawaii/miniforge3/envs/tensor_no_gpu/lib/python3.8/site-packages/keras/backend.py", line 4994, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 200, 3) and (1, 3) are incompatible


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source