我在做 CNN 量化,我使用下面的代码来计算一些指标。
FP = confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)
FN = confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)
TP = np.diag(confusion_matrix)
TN = confusion_matrix.sum() - (FP + FN + TP)
FP = torch.Tensor(FP)
FN = torch.Tensor(FN)
TP = torch.Tensor(TP)
TN = torch.Tensor(TN)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Precision or positive predictive value
PPV = TP/(TP+FP)
# # Negative predictive value
# NPV = TN/(TN+FN)
# # Fall out or false positive rate
# FPR = FP/(FP+TN)
# # False negative rate
# FNR = FN/(TP+FN)
# # False discovery rate
# FDR = FP/(TP+FP)
F1 = 2 * (TPR * PPV) / (TPR+PPV)
# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)
# print('Sensitivity for each class',confusion_matrix.diag()/confusion_matrix.sum(1))
print('Acc',ACC)
print('Sensitivity',TPR)
print('Specificity',TNR)
print('Precision',PPV)
print('F1 score',F1)
但我得到了这样的结果:
Test_Quantization Epoch #0: 100% 129/129 [00:02<00:00, 57.30it/s, Acc=0.0785, F1 score=0.1589, Loss=3.7637, Sensitivity(recall)=1.0000, Specificity=0.0000, precision=0.0870]
/content/drive/My Drive/ECG_quantization/trainer.py:219: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:141.)
TP = torch.Tensor(TP)
Acc tensor([0.1743, 0.9743, 0.9350, 0.9924, 0.0760])
Sensitivity tensor([0., 0., 0., 0., 1.])
Specificity tensor([1., 1., 1., 1., 0.])
Precision tensor([ nan, nan, nan, nan, 0.0760])
F1 score tensor([ nan, nan, nan, nan, 0.1412])
Normalized confusion matrix
我不知道为什么除了准确性之外的所有指标都如此奇怪。有谁知道这是怎么回事?