Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of calculation accuracy, regression rate, F1score and other indicators in pytorch

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report