In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the method of calculating accuracy, recall and F1 value in pytorch". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of calculating accuracy, recall and F1 value in pytorch".
Look at the code ~ predict = output.argmax (dim = 1) confusion_matrix = torch.zeros (2dag2) for t, p in zip (predict.view (- 1), target.view (- 1)): confusion_matrix [t.long () P.long ()] + = 1a_p = (confusion_matrix.diag () / confusion_matrix.sum (1)) [0] bauxp = (confusion_matrix.diag () / confusion_matrix.sum (1)) [1] astatr = (confusion_matrix.diag () / confusion_matrix.sum (0)) [0] biconr = (confusion_matrix.diag () / confusion_matrix.sum (0)) [1]
Add: pytorch recall, recall precision, precision F1 and average accuracy accuracy
Look at the code ~ def eval (): net.eval () test_loss = 0 correct = 0 total = 0 classnum = 9 target_num = torch.zeros (1)) predict_num = torch.zeros (1)) acc_num = torch.zeros (()) for batch_idx, (inputs, targets) in enumerate (testloader): if use_cuda: inputs, targets = inputs.cuda () Targets.cuda () inputs, targets = Variable (inputs, volatile=True), Variable (targets) outputs = net (inputs) loss = criterion (outputs, targets) # loss is variable, if add it (+ = loss) directly, there will be a bigger ang bigger graph. Test_loss + = loss.data [0] _, predicted = torch.max (outputs.data, 1) total + = targets.size (0) correct + = predicted.eq (targets.data). Cpu (). Sum () pre_mask = torch.zeros (outputs.size (). Roomter _ (1, predicted.cpu (). View (- 1,1), 1.) Predict_num + = pre_mask.sum (0) tar_mask = torch.zeros (outputs.size ()). Roomter _ (1, targets.data.cpu (). View (- 1,1), 1.) Target_num + = tar_mask.sum (0) acc_mask = pre_mask*tar_mask acc_num + = acc_mask.sum (0) recall = acc_num/predict_num F1 = 2*recall*precision/ (recall+precision) accuracy = acc_num.sum (1) / target_num.sum (1) # Precision adjustment recall = (recall.numpy () [0] * 100). 3) precision = (precision.numpy () [0] * 100). Round (3) F1 = (F1.numpy () [0] * 100). Round (3) accuracy = (accuracy.numpy () [0] * 100). Round (3) # print format makes it easy to copy print ('recall' "" .join ('% s'% id for id in recall) print ('precision', "" .join ('% s'% id for id in precision)) print ('F1permission, ".join ('% s'% id for id in F1)) print ('accuracy',accuracy)
Supplement: Python scikit-learn, evaluation of classification models, accuracy and recall rates, classification_report
The most common evaluation criteria of classification models are accuracy (estimator.score ()), that is, the percentage of correct prediction results.
Confusion matrix:
Accuracy is relative to all classification results; accuracy, recall and F1-score are the prediction and evaluation criteria relative to a certain classification.
Accuracy (Precision): the percentage of true positive cases in the predicted results (accurate) ()
Recall rate (Recall): the percentage of true positive samples whose predicted results are positive (all checked) ()
Other evaluation criteria for classification: F1-score, reflecting the robustness of the model
Demo.py (classified assessment, accuracy, recall rate, F1 Merchandise score reporting report):
From sklearn.datasets import fetch_20newsgroupsfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import classification_report # load dataset download news dataset (a total of 20 categories) from scikit-learn official website news = fetch_20newsgroups (subset='all') # all means download training set and test set # split the data (partition training set and test set) x_train, x_test, y_train Y_test = train_test_split (news.data, news.target, test_size=0.25) # feature extraction (feature extraction) from the dataset Tf = TfidfVectorizer () # tf-idf represents the importance of feature words # statistics the importance of feature words from training set data (extracting feature words from training set data) x_train = tf.fit_transform (x_train) print (tf.get_feature_names ()) # ["condensed", "condescend",...] X_test = tf.transform (x_test) # does not need to re-fit () data, but directly carries on the importance statistics according to the feature words extracted from the training set. # Forecast of naive Bayesian algorithm mlt = MultinomialNB (alpha=1.0) # alpha represents Laplace smoothing coefficient, and the default 1print (x_train.toarray ()) # toarray () displays sparse matrices in the form of dense matrices. [[0. 0. 0. ..., 0.04234873 0. 0. ] [0. 0. 0. ..., 0. 0. 0. ]..., [0. 0.03934786 0. ..., 0. 0. 0. ]''mlt.fit (x_train, y_train) # populate the training set data # Forecast Category y_predict = mlt.predict (x_test) print ("predicted article category is:", y_predict) # [4 18 8..., 15 15 4] # accuracy print ("accuracy:", mlt.score (x_test, y_test)) # 0.853565365025 print ("accuracy and recall rate for each category:" Classification_report (y_test, y_predict, target_names=news.target_names)''precision recall f1-score support alt.atheism 0.86 0.66 0.75 207 comp.graphics 0.85 0.75 0.80 238 sport.baseball 0.96 0.94 0.95 253.
Significance of recall rate (application scenario): product failure rate (do not want to miss any unqualified product, check); cancer prediction (do not want to miss any cancer patient)
Thank you for your reading, the above is the content of "the method of calculating accuracy, recall and F1 value in pytorch". After the study of this article, I believe you have a deeper understanding of the method of calculating accuracy, recall and F1 value in pytorch, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.