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

时间:2020-09-08 14:50:10

标签: 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_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。

1 个答案:

答案 0 :(得分:0)

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

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

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

如果只有5个类别可用于预测,则在计算dummy_category变量时会犯一些错误。