ValueError: Shapes (None, 6) and (None, 5) are incompatible

ValueError: Shapes (None, 6) and (None, 5) are incompatible

我正在尝试训练我的顺序模型,但出了点问题:

aspect_categories_model = Sequential()
aspect_categories_model.add(Dense(512, input_shape=(6000,), activation='relu'))
aspect_categories_model.add(Dense(5, activation='softmax'))
aspect_categories_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

当尝试预测值时: aspect_categories_model.fit(aspect_tokenized, dummy_category, epochs=5, verbose=1). 它给我一个值错误:

ValueError: Shapes (None, 6) and (None, 5) are incompatible

假人代码是:

from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical

label_encoder = LabelEncoder()
integer_category = label_encoder.fit_transform(dataset.aspect_category)
dummy_category = to_categorical(integer_category)

标签是 5。

dummy_category的形状是(batch_size, 6),模型的输出形状是(batch_size, 5)

尝试改变最后一层的神经元数量。

aspect_categories_model.add(Dense(6, activation='softmax'))

如果您只有 5 个类别用于预测,那么您在计算 dummy_category 变量时犯了一些错误。