2

I am using the CNN architecture (see code below) for text classification task (with 5 classes). The data I am using is reviews_Home_and_Kitchen_5.json downloaded from here

I created a sentence embedding matrix for 1000 sentences taking the embedding from Glove model ('glove.840B.300d.txt')

The model compiles and you can see the summary below. However, whenever I am trying to fit the model I keep getting the following error: ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 896 but received input with shape [None, 128]

The solutions I found online involved using different version of TF or using different loss function. I tried both and couldn't solve the issue.

Can anyone help?

sequence_input = Input(shape=(len(glove_sentence_embedding_matrix),), dtype='int32')
embedded_sequences = Embedding(input_dim=glove_results.shape[0], output_dim=300, weights=[glove_results], trainable=False, name='embedding')(sequence_input)
l_cov1= Conv1D(128, 5, activation='relu', name='conv1D_1')(embedded_sequences)  #  padding='same'
l_pool1 = MaxPooling1D(5, name='MaxPool_1')(l_cov1)
l_cov2 = Conv1D(128, 5, activation='relu', name='conv1D_2')(l_pool1)
l_pool2 = MaxPooling1D(5, name='MaxPool_2')(l_cov2)
l_cov3 = Conv1D(128, 5, activation='relu', name='conv1D_3')(l_pool2)
l_pool3 = MaxPooling1D(5, name='MaxPool_3')(l_cov3)  
l_flat = Flatten(name='flatten')(l_pool3)
l_dense = Dense(128, activation='relu',  name='dense')(l_flat)
preds = Dense(5, activation='softmax', name='preds')(l_dense)
model = Model(sequence_input, preds, name='CNN_for_text_classification')

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

print(model.summary())
model.fit(glove_sentence_embedding_matrix, df.overall, epochs=2, verbose=1)  ## glove_sentence_embedding_matrix.shape = (1000,300)

enter image description here

Idanlench
  • 31
  • 1
  • 2

1 Answers1

0

Change the number of units to 896 in l_dense. Or add a Dense layer with 896 units before it.

Abhishek Verma
  • 1,671
  • 1
  • 8
  • 12
  • tried that. still getting the same error **ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 896 but received input with shape [None, 128]** – Idanlench Jun 21 '20 at 12:11