检查目标时出错:期望的dense_2有形状(7,)但是有形状的数组(4,)

时间:2018-06-12 21:28:51

标签: tensorflow neural-network keras convolutional-neural-network

我制作了7个班级的CNN分类器。我正在训练一个顺序模型,这里

model = Sequential()
model.add(Conv2D(C1, (3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(3*C1, (3, 3), activation='relu'))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(6*numPCAcomponents, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))

然后我正在编译模型:

sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

然后使用此模型评估测试集。问题出在这里。当我启动mode.evaluate函数时出现此错误:

Error when checking target: expected dense_2 to have shape (7,) but got array with shape (4,)

我评估的方式就在这里,

Y_pred = model.predict(X_test)
y_pred = np.argmax(Y_pred, axis=1)
target_names = ['A','B','C','D','E','F','G']

classification = classification_report(np.argmax(y_test, axis=1), y_pred, target_names=target_names)
confusion = confusion_matrix(np.argmax(y_test, axis=1), y_pred)
score = model.evaluate(X_test, y_test, batch_size=32)
Test_Loss =  score[0]*100
Test_accuracy = score[1]*100

为什么会出现这个错误?如何解决这个错误?我多次检查了一切,但找不到错误。

0 个答案:

没有答案