Example Analysis of calculation accuracy, regression rate, F1score and other indicators in pytorch
This article introduces the relevant knowledge of "calculation accuracy, regression rate, F1score and other indicators in pytorch". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
After training the network in pytorch, you need to test the results. The methods used in the official website routines are all correct rates, using the torch.eq() function.
But in order to evaluate the results more precisely, we also need to calculate other indicators. After looking through the API website, I found that there is no function for calculating TP, TN, FP, FN...
After using countless crooked brains, I thought that pytorch fully supported numpy, so I could directly judge it. I tried it and it worked. The code:
# TP predict and label are both 1TP +=((pred_choice == 1) &(target.data == 1)).cpu().sum()# TN predict and label are both 0TN +=((pred_choice == 0) &(target.data == 0)).cpu().sum()# FN predict 0 label 1FN +=((pred_choice == 0) &(target.data == 1)).cpu().sum()# FP predict 1 label 0FP +=((pred_choice == 1) & (target.data == 0)).cpu().sum()p = TP / (TP + FP)r = TP / (TP + FN)F1 = 2 * r * p / (r + p)acc = (TP + TN) / (TP + TN + FP + FN
So you can see the indicators.
Because target is Variable, you need to use target.data to get the corresponding tensor, and because it is calculated on GPU, you need to use.cpu() to move to CPU.
Because this is a batch statistic, you need to accumulate the entire epoch statistic with +=. Of course, it needs to be cleared before the epoch begins.
"Example analysis of calculating precision, regression rate, F1score and other indicators in pytorch" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!