问题描述
我正在尝试使用 Tensorflow 和 Keras 在 MNIST 数据集上获得较高的精度分数.如果我将指标设置为准确度,我的代码可以正常工作,但是当我将其设置为精度时,它会出现以下错误:
I am trying to achieve a high Precision score on the MNIST dataset using Tensorflow and Keras. My code is working if I set the metric to accuracy but when I set it to precision, it gives the following error:
ValueError: Shapes (32, 10) and (32, 1) are incompatible
这是我的代码:
import tensorflow as tf
import keras
from tensorflow.keras.datasets import mnist
def bulid_model(n = 1, neuron=30,lr = 3e-3,input_shape=(784,)):
model = keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape=input_shape))
for layer in range(n):
model.add(keras.layers.Dense(neuron, activation = 'relu'))
model.add(keras.layers.Dense(10,activation='softmax'))
optimizer = keras.optimizers.Adam(lr = lr)
model.compile(loss = 'sparse_categorical_crossentropy',optimizer=optimizer,metrics = [keras.metrics.Precision()])
return model
if __name__ == "__main__":
(X_train,Y_train),(X_test,Y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
model = bulid_model(3,20,0.0156)
history = model.fit(X_train,Y_train,epochs=50)
谁能帮我解决这个问题?
Can anyone help me with this?
推荐答案
精度,是二元分类的度量.它计算 true_positives
和 false_positives
然后简单地将 true_positives
除以 true_positives
和 false_positives
的总和>.
Precision, is a metric for binary classification. It computes true_positives
and false_positives
then simply divides true_positives
by the sum of true_positives
and false_positives
.
但是 Accuracy
指标可用于多类分类,如 MNIST,因为它计算预测与标签相等的频率.
But Accuracy
metric can be used for multi-class classification like MNIST, because it calculates how often predictions equal labels.
这篇关于使用精度度量进行 MNIST 数字分类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!