问题描述
我正在使用本教程来学习如何在 MNIST 数据集上训练模型:
不过,这是我的问题.我尝试将 model.compile
中的指标更改为 metrics=[tf.keras.metrics.Precision()]
,但出现错误 ValueError: Shapes (32, 10) 和 (32, 1) 不兼容
.
我也尝试通过 scikit-learn 计算 precisiona 和召回率,但我的预测与真实标签不一致.
y_pred = model.predict(x_test)打印(y_pred)精度分数(y_test,y_pred)
输出:
[[ -4.7507367 -7.4252934 -2.8428416 ... 8.855136 -5.937388-2.1762638 ][ -5.0433793 5.554433 12.963128 ... -18.583 -1.6025407-18.721622 ][ -7.623428 6.3951 -1.8510209 ... 0.37932196 -1.2399373-6.59459 ]...---------------------------------------------------------------------------ValueError 回溯(最近一次调用)<ipython-input-44-a82c4d76f544>在 <module>()1 y_pred = model.predict(x_test)2 打印(y_pred)---->3 precision_score(y_test, y_pred)ValueError:分类指标无法处理多类和连续多输出目标的混合
我想我可能需要转换 y_pred,但我不确定如何转换.或者,如果有一种方法可以为指标增加精确度和召回率,那就更好了.我怎样才能得到这个模型的准确率和召回率?
假设您使用代码进行预测:
predicted_result=model.predict(x_test)
输出层有数字0到9的概率,即10.所以从预测结果中需要识别类别.
将 numpy 导入为 npclass_preds = np.argmax(predicted_result,axis=-1)
现在,y_test 和 class_preds 在类中,因此可以运行 precision_score.
from sklearn.metrics import precision_scoreprecision_score(y_test,class_preds,average='macro')
或
from sklearn.metrics 导入recall_score召回分数(y_test,class_preds,average='macro')
甚至可以将此自定义函数提供给指标:
from sklearn.metrics import precision_scoredef custom_prec_score(y_true, y_pred):y_true=y_true.numpy()y_pred=y_pred.numpy()y_pred=np.argmax(y_pred,axis=-1)返回 precision_score(y_true, y_pred,average='macro')
model.compile(optimizer='adam',损失=loss_fn,run_eagerly=真,指标=[准确度",custom_prec_score])模型.fit(x_train, y_train, epochs=5)
I'm using this tutorial to learn how to train a model on the MNIST dataset here: https://www.tensorflow.org/tutorials/quickstart/beginner
Currently, the model only trains on the accuracy, but I want to figure out the F1-score of the model (starting with precision and recall first).
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2895 - accuracy: 0.9151
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1393 - accuracy: 0.9586
...
Apparently the model also uses log-odd scores which are converted into probabilities by a softmax as well.
This is my problem, though. I tried changing the metrics in model.compile
to metrics=[tf.keras.metrics.Precision()]
, but I got the error ValueError: Shapes (32, 10) and (32, 1) are incompatible
.
I also tried calculating the precisiona and recall through scikit-learn, but my predictions aren't lined up with the true labels.
y_pred = model.predict(x_test)
print(y_pred)
precision_score(y_test, y_pred)
Output:
[[ -4.7507367 -7.4252934 -2.8428416 ... 8.855136 -5.937388
-2.1762638 ]
[ -5.0433793 5.554433 12.963128 ... -18.583 -1.6025407
-18.721622 ]
[ -7.623428 6.3951 -1.8510209 ... 0.37932196 -1.2399373
-6.59459 ]
...
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-44-a82c4d76f544> in <module>()
1 y_pred = model.predict(x_test)
2 print(y_pred)
----> 3 precision_score(y_test, y_pred)
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets
I'm thinking I might need to transform y_pred, but I'm not sure how. Or if there is a way to add precision and recall to the metrics that would be even better. How can I get the precision and recall of this model?
suppose you predicted using code:
predicted_result=model.predict(x_test)
the output layer has prob for digit 0 to 9, i.e 10. so from the predicted result need to identify the class.
import numpy as np
class_preds = np.argmax(predicted_result, axis=-1)
now, y_test and class_preds are in classes, so can run precision_score.
from sklearn.metrics import precision_score
precision_score(y_test, class_preds,average='macro')
or
from sklearn.metrics import recall_score
recall_score(y_test, class_preds,average='macro')
even can feed this custom function to metrics:
from sklearn.metrics import precision_score
def custom_prec_score(y_true, y_pred):
y_true=y_true.numpy()
y_pred=y_pred.numpy()
y_pred=np.argmax(y_pred, axis=-1)
return precision_score(y_true, y_pred,average='macro')
model.compile(optimizer='adam',
loss=loss_fn,run_eagerly=True,
metrics=["accuracy",custom_prec_score])
model.fit(x_train, y_train, epochs=5)
这篇关于使用 TensorFlow 查找 MNIST 数据集的精度和召回率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!