我為不正確和正確的分類創建了一個輸出。我想通過 prediction_prob 對輸出進行正確分類。到目前為止,這是我的代碼:
for input, prediction_prob, prediction, label in zip(test_text, test_pred_prob, test_pred_class, test_label):
if prediction == label:
print(input, '\n\n', prediction_prob, '\n\n', 'has been machine coded as', prediction, 'and should be', label, 'according to human coding.' '\n\n-----\n')
這是我的輸出示例:
mailtext1
tf.Tensor([0.7113831 0.28861693], shape=(2,), dtype=float32)
has been machine coded as 0 and should be 0 according to human coding.
-----
mailtext2
tf.Tensor([0.02235095 0.97764903], shape=(2,), dtype=float32)
has been machine coded as 1 and should be 1 according to human coding.
這是我的第一個問題,我對 python 比較陌生。提前感謝您的幫助。
uj5u.com熱心網友回復:
我知道你想zip(test_text, test_pred_prob, test_pred_class, test_label)按第二個引數(test_pred_prob)中的第二個值降序排序
你可以這樣做。首先我們存根一些資料
test_text = ['mailtext1', 'mailtext2']
test_pred_prob = [tf.constant(np.array([0.7113831, 0.28861693])), tf.constant(np.array([0.02235095, 0.97764903]))]
test_pred_class = [0,1]
test_label = [0,1]
接下來我們壓縮并排序
all_test_data = zip(test_text, test_pred_prob, test_pred_class, test_label)
all_test_data_sorted = sorted(all_test_data, reverse = True, key = lambda t: t[1][1])
排序函式是sorted()并且值得注意的是key選擇正確分類概率的引數。reverse=True因為我們想要一個降序
我們現在可以檢查這個串列,看看我們是否按正確的順序得到它:
all_test_data_sorted
生產
[('mailtext2',
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.02235095, 0.97764903])>,
1,
1),
('mailtext1',
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.7113831 , 0.28861693])>,
0,
0)]
正如預期的那樣。現在您可以對列印回圈進行適當修改以遍歷已排序的元組:
for input, prediction_prob, prediction, label in all_test_data_sorted:
if prediction == label:
print(input, '\n\n', prediction_prob, '\n\n', 'has been machine coded as', prediction, 'and should be', label, 'according to human coding.' '\n\n-----\n')
如果您想按張量中的第一個值排序,您將key = lambda t: t[1][0]在sorted()函式中使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/409760.html
標籤:
上一篇:如何調整albumentations標簽中的資料集標簽以與tensorflowimage_dataset_from_directory函式一起使用?
