2

我正在使用 CNN 架构(见下面的代码)进行文本分类任务(有 5 个类)。我使用的数据是从这里下载的reviews_Home_and_Kitchen_5.json

我从 Glove 模型('glove.840B.300d.txt')中为 1000 个句子创建了一个句子嵌入矩阵

模型编译,您可以在下面看到摘要。但是,每当我尝试拟合模型时,我都会不断收到以下错误: 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 ]

我在网上找到的解决方案涉及使用不同版本的 TF 或使用不同的损失函数。我都尝试了,但无法解决问题。

任何人都可以帮忙吗?

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)

在此处输入图像描述

4

1 回答 1

0

将单位数更改为 896 in l_dense。或者在它之前添加一个具有 896 个单位的 Dense 层。

于 2020-06-20T15:32:19.763 回答