Keras ValueError:形状(无,6)和(无,5)不兼容

Keras ValueError:形状(无,6)和(无,5)不兼容,keras,neural-network,sequential,Keras,Neural Network,Sequential,我试图训练我的顺序模型,但出现了一些问题: 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

我试图训练我的顺序模型,但出现了一些问题:

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\u categories\u model.fit(aspect\u标记化,dummy\u 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。

虚拟\u类别的形状为
(批次大小,6)
,模型的输出形状为
(批次大小,5)

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

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

如果您只有5个预测类别,那么您在计算
dummy\u category
变量时犯了一些错误。

好的,谢谢,它解决了问题,但是的,我只有5个类别。我将此代码用于假人:从sklearn.preprocessing从keras.utils导入LabelEncoder到\u category label\u encoder=LabelEncoder()integer\u category=label\u encoder.fit\u transform(dataset.aspect\u category)dummy\u category=to\u category(integer\u category)您是否可以使用您试图生成的代码
dummy_category
以及您拥有的标签来更新问题?添加生成6个类别的示例数据。即,单行数据从
数据集
变量添加数据。如果可能,添加代码,我可以在我的机器上运行,重现您的问题。