Ant*_*até 5 python scikit-learn keras
我在 Keras 上实现并训练了一个多类卷积神经网络。所得测试精度为 0.9522。然而,当我使用 scikit-learn 中的 precision_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))
Run Code Online (Sandbox Code Playgroud)
结果:
Size of the arrays:
X_train: (60000, 64, 64, 3)
X_test: (40000, 64, 64, 3)
y_train: (60000, 14)
y_test: (40000, 14)
Run Code Online (Sandbox Code Playgroud)
拟合 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))
Run Code Online (Sandbox Code Playgroud)
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)))
Run Code Online (Sandbox Code Playgroud)
分数:
Accuracy on validation data: 0.6224
Run Code Online (Sandbox Code Playgroud)
Keras 的准确性:
score_Keras = model.evaluate(X_test, y_test, batch_size=200)
print('Accuracy on validation data with Keras: ' + str(score_Keras[1]))
Run Code Online (Sandbox Code Playgroud)
结果:
Accuracy on validation data with Keras: 0.95219109267
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么这两种精度不同,我应该使用哪一种来评估多类分类器的性能?
提前致谢!
你的代码有错字,为什么要定义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)))
Run Code Online (Sandbox Code Playgroud)
应该 :
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)))
Run Code Online (Sandbox Code Playgroud)
尽管如此,您应该提供y_pred1D
和的值和形状y_test1D
,当您这样做时会出现错误y_pred1D = y_pred.argmax(1)
,y_test1D = y_test.argmax(1)
以便使用 scikit learn 指标。我的猜测是,这不是您想象的那样,否则这两个指标将是相同的。