scogit-learn中的accuracy_score与Keras的准确度之间的差异

时间:2017-09-11 13:36:46

标签: python scikit-learn keras

我在Keras上实施并训练了一个多类卷积神经网络。得到的测试精度为0.9522。但是,当我使用scikit-learn中的accuracy_score计算精度时,我得到0.6224。这是我做的:

X_train = X[:60000, :, :, :]
X_test = X[60000:, :, :, :]
y_train = y[:60000, :]
y_test = y[60000:, :]
print ('Size of the arrays:')
print ('X_train: ' + str(X_train.shape))
print ('X_test: ' + str(X_test.shape))
print ('y_train: ' + str(y_train.shape))
print ('y_test: ' + str(y_test.shape))

结果:

Size of the arrays:
X_train: (60000, 64, 64, 3)
X_test: (40000, 64, 64, 3)
y_train: (60000, 14)
y_test: (40000, 14)

适合Keras模型(我不在这里添加整个模型以保持代码简单):

model = Sequential()
model.add(Conv2D(10, (5,5), padding='same', input_shape=(64, 64, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(14))
model.add(Activation('softmax'))
model.compile(optimizer='rmsprop', loss='mean_squared_error', metrics['accuracy'])
model.fit(X_train, y_train, batch_size=100, epochs=5, verbose=1, validation_data=(X_test, y_test))

Scikit-Learn的准确性:

y_pred = model.predict(X_test, batch_size=100)
y_pred1D = y_pred.argmax(1)
y_pred = model.predict(X_test, batch_size=100)
y_test1D = y_test.argmax(1)
print ('Accuracy on validation data: ' + str(accuracy_score(y_test1D, y_pred1D)))

分:

Accuracy on validation data: 0.6224

Keras的准确性:

score_Keras = model.evaluate(X_test, y_test, batch_size=200)
print('Accuracy on validation data with Keras: ' + str(score_Keras[1]))

结果:

Accuracy on validation data with Keras: 0.95219109267

我的问题是:为什么两个准确度不同,我应该使用哪一个来评估我的多类分类器的性能?

提前致谢!

1 个答案:

答案 0 :(得分:3)

您的代码中有拼写错误,为什么要定义y_pred两次?

y_pred = model.predict(X_test, batch_size=100)
y_pred1D = y_pred.argmax(1)
y_pred = model.predict(X_test, batch_size=100)
y_test1D = y_test.argmax(1)
print ('Accuracy on validation data: ' + str(accuracy_score(y_test1D, y_pred1D)))

应该是:

y_pred = model.predict(X_test, batch_size=100)
y_pred1D = y_pred.argmax(1)
y_test1D = y_test.argmax(1)
print ('Accuracy on validation data: ' + str(accuracy_score(y_test1D, y_pred1D)))

尽管如此,您应该提供y_pred1Dy_test1D的值和形状,当您执行y_pred1D = y_pred.argmax(1)y_test1D = y_test.argmax(1)以便使用scikit时,错误就在于此度量。我的猜测是,它不是你想象的那样,否则两个指标就会相同。