{"id":398,"date":"2022-04-21T19:49:53","date_gmt":"2022-04-21T11:49:53","guid":{"rendered":"https:\/\/scutvk.cn\/?p=398"},"modified":"2023-02-14T11:01:43","modified_gmt":"2023-02-14T03:01:43","slug":"importerror-cannot-import-name-get-num-classes-from-torchmetrics-utilities-data","status":"publish","type":"post","link":"https:\/\/scutvk.cn\/?p=398","title":{"rendered":"ImportError: cannot import name &#8216;get_num_classes&#8217; from &#8216;torchmetrics.utilities.data&#8217;"},"content":{"rendered":"\n<p style=\"font-size:28px\"><strong>\u89e3\u51b3\u529e\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"388\" src=\"https:\/\/x2.mday.top\/wp-content\/uploads\/2022\/04\/image-5-1024x388.png\" alt=\"\" class=\"wp-image-399\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5-1024x388.png 1024w, https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5-300x114.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5-768x291.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5-1536x583.png 1536w, https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5-769x292.png 769w, https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5.png 1814w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u95ee\u9898\u539f\u56e0\uff1atorchmetrics\u7248\u672c\u4e0e\u9879\u76ee\u4e0d\u5339\u914d\uff0c\u7b14\u8005\u51fa\u9519\u7684\u73af\u5883\u4f7f\u7528\u7684\u662f torchmetrics 0.8\uff0c\u5e93\u5347\u7ea7\u540e\u90e8\u5206\u4ee3\u7801\u8fdb\u884c\u4e86\u91cd\u6784\uff0c\u5bfc\u81f4\u67d0\u4e9b\u51fd\u6570\u201c\u8fc7\u65f6\u201d\uff0c\u4f8b\u5982\u6b64\u4f8b\u7684\u201cget_num_classes\u201d\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u964d\u7ea7torchmetrics\uff0c\u6b64\u5904\u6211\u9009\u62e9\u5b89\u88c5 torchmetrics 0.5<\/p>\n\n\n\n<p>how to downgrade torchmetrics\uff1f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">conda uninstall torchmetrics\nconda install torchmetrics=0.5<\/code><\/pre>\n\n\n\n<p>\u6b64\u5904\u76840.5\u53ea\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u540e\u7eed\u51fa\u73b00.5\u5bf9\u5176\u4ed6\u5e93\u4e0d\u517c\u5bb9\u7684\u8bdd\uff0c\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u6162\u6162\u8c03\u9ad8\u7248\u672c\uff0c\u6216\u8005\u4f7f\u7528\u65b0\u7684\u4f7f\u7528\u65b9\u6cd5\u5bf9get_num_classes\u76f8\u5173\u7684\u4ee3\u7801\u8fdb\u884c\u91cd\u5199\u3002<\/p>\n\n\n\n<p style=\"font-size:28px\"><strong>\u5173\u4e8etorchmetrics\u7684\u66f4\u591a\u5185\u5bb9\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u7b80\u4ecb\uff1a<\/strong><\/p>\n\n\n\n<p>torchmetrics\u662f\u4e00\u4e2a\u7528\u4e8e\u8bc4\u4f30PyTorch\u6a21\u578b\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5e38\u7528\u7684\u5ea6\u91cf\uff0c\u5982\u51c6\u786e\u6027\uff0c\u7cbe\u786e\u5ea6\uff0c\u53ec\u56de\u7387\u7b49\u3002\u8fd9\u4e9b\u5ea6\u91cf\u53ef\u4ee5\u7528\u6765\u8bc4\u4f30\u5206\u7c7b\u548c\u56de\u5f52\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p>torchmetrics github\uff1a<a href=\"https:\/\/github.com\/Lightning-AI\/metrics\" target=\"_blank\" rel=\"noopener\">Lightning-AI\/metrics: Machine learning metrics for distributed, scalable PyTorch applications. (github.com)<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u4e00\u3001torchmetrics\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ol>\n<li>\u5b89\u88c5torchmetrics\u5e93\uff1a\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c&#8221;pip install torchmetrics&#8221;\u3002<\/li>\n\n\n\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5ea6\u91cf\uff1a\u5728\u4ee3\u7801\u4e2d\u4ecetorchmetrics\u5e93\u4e2d\u5bfc\u5165\u6240\u9700\u7684\u5ea6\u91cf\uff0c\u5982Accuracy\uff0cPrecision\uff0cRecall\u7b49\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5ea6\u91cf\u8bc4\u4f30\u6a21\u578b\uff1a\u5728\u8bad\u7ec3\u6a21\u578b\u5e76\u5bf9\u5176\u8fdb\u884c\u8bc4\u4f30\u4e4b\u540e\uff0c\u4f7f\u7528\u6240\u9009\u62e9\u7684\u5ea6\u91cf\u8bc4\u4f30\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Accuracy()\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u7684\u51c6\u786e\u6027\u3002<\/li>\n\n\n\n<li>\u83b7\u53d6\u7ed3\u679c\uff1a\u8c03\u7528\u8be5\u5ea6\u91cf\u7684\u201ccompute\u201d\u65b9\u6cd5\u4ee5\u83b7\u53d6\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u8c03\u7528acc(y_pred, y_true)\u6765\u83b7\u53d6\u51c6\u786e\u6027\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u591a\u4e2a\u5ea6\u91cf\uff1a\u53ef\u4ee5\u5728\u540c\u4e00\u65f6\u95f4\u4f7f\u7528\u591a\u4e2a\u5ea6\u91cf\u8bc4\u4f30\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u5ea6\u91cf\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from torchmetrics import Accuracy\n\nacc = Accuracy()\n\ny_pred = torch.tensor([1, 2, 3, 4, 5])\ny_true = torch.tensor([1, 2, 3, 4, 5])\n\nacc_result = acc(y_pred, y_true)\nprint(acc_result)\n<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\u4e3a1.0,\u8868\u793a\u9884\u6d4b\u503c\u548c\u771f\u5b9e\u503c\u5b8c\u5168\u76f8\u540c\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001get_num_classes\u7684\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>get_num_classes\u662ftorchmetrics\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6\u7ed9\u5b9a\u6570\u636e\u96c6\u4e2d\u7c7b\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b:<\/p>\n\n\n\n<ol>\n<li>\u4ecetorchmetrics\u5e93\u4e2d\u5bfc\u5165\u51fd\u6570\uff1a\u4f7f\u7528from torchmetrics import get_num_classes<\/li>\n\n\n\n<li>\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u662fnumpy\u6570\u7ec4\u6216torch.Tensor<\/li>\n\n\n\n<li>\u8c03\u7528\u51fd\u6570\u5e76\u4f20\u5165\u6570\u636e\u96c6\uff0c\u5982 get_num_classes(y)<\/li>\n\n\n\n<li>\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u6570,\u8868\u793a\u7c7b\u522b\u6570\u91cf.<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from torchmetrics import get_num_classes\n\ny_true = torch.tensor([1, 2, 3, 4, 5])\nn_classes = get_num_classes(y_true)\nprint(n_classes)\n<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\u4e3a5,\u8868\u793a\u67095\u4e2a\u7c7b\u522b<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u4f60\u7684\u6570\u636e\u96c6\u662f\u4f7f\u7528one-hot\u7f16\u7801\u7684\uff0c\u90a3\u4e48\u4e0d\u80fd\u4f7f\u7528get_num_classes\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001attributeerror: module torchmetrics has no attribute f1<\/strong><\/p>\n\n\n\n<p>\u63d0\u5230\u7684\u9519\u8bef\u8868\u660etorchmetrics\u5e93\u4e2d\u6ca1\u6709\u540d\u4e3a&#8217;f1&#8217;\u7684\u5c5e\u6027\u3002F1\u5206\u6570\u662f\u4e00\u79cd\u7528\u4e8e\u8bc4\u4f30\u4e8c\u5143\u5206\u7c7b\u6a21\u578b\u6027\u80fd\u7684\u5e38\u7528\u5ea6\u91cf\uff0c\u5b83\u8861\u91cf\u4e86\u7cbe\u786e\u7387\u548c\u53ec\u56de\u7387\u4e4b\u95f4\u7684\u5e73\u8861\u3002\u5982\u679c\u5e0c\u671b\u5728torchmetrics\u5e93\u4e2d\u4f7f\u7528F1\u5206\u6570\uff0c\u5219\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u8be5\u5ea6\u91cf\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528torchmetrics\u5e93\u4e2d\u7684Precision\uff0cRecall\u5ea6\u91cf\u6765\u8ba1\u7b97F1\u5206\u6570\uff0c\u516c\u5f0f\u4e3a\uff1a F1 = 2 * (precision * recall) \/ (precision + recall)<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u60f3\u5b9e\u73b0F1\u5206\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5982 sklearn , scikit-learn \u4e2d\u7684f1_score\u6765\u8ba1\u7b97F1\u5206\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from sklearn.metrics import f1_score\n\ny_pred = [0, 1, 0, 0, 1]\ny_true = [0, 1, 0, 1, 1]\n\nf1 = f1_score(y_true, y_pred)\nprint(f1)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>torchmetrics\u7248\u672c\u4e0e\u9879\u76ee\u4e0d\u5339\u914d\uff0c\u7b14\u8005\u51fa\u9519\u7684\u73af\u5883\u4f7f\u7528\u7684\u662f torchmetrics 0.8\uff0c\u5e93\u5347\u7ea7\u540e\u90e8\u5206\u4ee3\u7801\u8fdb\u884c\u4e86\u91cd\u6784\uff0c\u5bfc\u81f4\u67d0\u4e9b\u51fd\u6570\u201c\u8fc7\u65f6\u201d\uff0c\u4f8b\u5982\u6b64\u4f8b\u7684\u201cget_num_classes\u201d\u3002<\/p>\n","protected":false},"author":1,"featured_media":399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[2,3],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/scutvk.cn\/wp-content\/uploads\/2022\/04\/image-5.png","_links":{"self":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":8,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/610"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/media\/399"}],"wp:attachment":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}