{"id":849,"date":"2024-11-07T13:30:53","date_gmt":"2024-11-07T05:30:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/849\/"},"modified":"2024-11-07T13:30:53","modified_gmt":"2024-11-07T05:30:53","slug":"%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e8%ae%ad%e7%bb%83%e4%b8%ad-gpu-%e5%88%a9%e7%94%a8%e7%8e%87%e4%bd%8e%ef%bc%8ccpu-%e5%8d%a0%e7%94%a8%e7%8e%87%e9%ab%98%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/849\/","title":{"rendered":"\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u4e2d GPU \u5229\u7528\u7387\u4f4e\uff0cCPU \u5360\u7528\u7387\u9ad8\uff1a\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1140\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173003167448851.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173003167448851.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173003167448851-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173003167448851-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173003167448851-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u4e2d GPU \u5229\u7528\u7387\u4f4e\uff0cCPU \u5360\u7528\u7387\u9ad8\uff1a\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u4e2d GPU \u5229\u7528\u7387\u4f4e\uff0cCPU \u5360\u7528\u7387\u9ad8\uff1a\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7528 gpu \u8bad\u7ec3\u6a21\u578b\u65f6 cpu \u5360\u7528\u7387\u9ad8\uff1a\u63a2\u7a76\u5e76\u89e3\u51b3<\/strong><\/p>\n<p>\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n<p>\u5728\u8bad\u7ec3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u65f6\uff0c\u867d\u7136\u5df2\u786e\u8ba4 gpu \u548c pytorch \u7248\u672c\u6b63\u786e\uff0c\u4f46 gpu \u5229\u7528\u7387\u5374\u5f88\u4f4e\uff0c\u800c cpu \u5229\u7528\u7387\u5374\u5f88\u9ad8\u3002\u539f\u56e0\u4f55\u5728\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5217\u4e3e\u4e86\u51e0\u79cd\u53ef\u80fd\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>1. gpu \u672a\u6b63\u786e\u914d\u7f6e<\/strong><\/p>\n<p>\u5c1d\u8bd5\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5 gpu \u662f\u5426\u914d\u7f6e\u6210\u529f\uff1a<\/p>\n<pre>import torch\nprint(torch.cuda.is_available())<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u8f93\u51fa\u4e3a false\uff0c\u5219\u8868\u793a gpu \u672a\u914d\u7f6e\u597d\u3002<\/p>\n<p><strong>2. \u672a\u6307\u5b9a cuda \u8bbe\u5907<\/strong><\/p>\n<p>\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u4e86\u8981\u4f7f\u7528\u7684 cuda \u8bbe\u5907\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>import torch\ndevice = 'cuda:0' if torch.cuda.is_available() else 'cpu'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u5c06\u6a21\u578b\u79fb\u52a8\u5230\u6307\u5b9a\u8bbe\u5907\uff1a<\/p>\n<pre>ResNet(......).to(device)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4ee3\u7801\u81ea\u8eab\u95ee\u9898<\/strong><\/p>\n<p>\u5982\u679c\u4e0a\u8ff0\u6b65\u9aa4\u65e0\u6548\uff0c\u5219\u53ef\u80fd\u662f\u4ee3\u7801\u4e2d\u6709\u5176\u4ed6\u95ee\u9898\u5bfc\u81f4 cpu \u5360\u7528\u7387\u9ad8\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5f00\u542f\u4e86\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u8fdb\u884c\u6570\u636e\u5904\u7406\u6216\u5176\u4ed6\u4efb\u52a1\uff0c\u5bfc\u81f4 cpu \u5360\u7528\u7387\u5347\u9ad8\u3002<\/li>\n<li>\u4ee3\u7801\u4e2d\u5b58\u5728\u5176\u4ed6\u8017\u8d39 cpu \u7684\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><strong>\u964d\u4f4e cpu \u5360\u7528\u7387\u7684\u5efa\u8bae\uff1a<\/strong><\/p>\n<ul>\n<li>\u51cf\u5c11\u4ee3\u7801\u4e2d\u7684\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6570\u91cf\uff0c\u4f46\u4f1a\u727a\u7272\u901f\u5ea6\u3002<\/li>\n<li>\u4f18\u5316\u4ee3\u7801\uff0c\u51cf\u5c11\u8017\u8d39 cpu \u7684\u8ba1\u7b97\u3002<\/li>\n<li>\u5bf9\u4e8e\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u6765\u8bf4\uff0ccpu \u5360\u7528\u7387\u8f83\u9ad8\u662f\u6b63\u5e38\u7684\u3002<\/li>\n<li>\u5c1d\u8bd5\u589e\u5927\u6279\u6b21\u5927\u5c0f\u4ee5\u5145\u5206\u5229\u7528 gpu \u5185\u5b58\uff0c\u4ece\u800c\u964d\u4f4e gpu \u5229\u7528\u7387\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u4e2d GPU \u5229\u7528\u7387\u4f4e\uff0cCPU \u5360\u7528\u7387\u9ad8\uff1a\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 gpu \u8bad\u7ec3\u6a21\u578b\u65f6 cpu \u5360\u7528\u7387\u9ad8\uff1a\u63a2\u7a76\u5e76\u89e3\u51b3 \u95ee\u9898\u63cf\u8ff0\uff1a \u5728\u8bad\u7ec3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u65f6\uff0c\u867d\u7136\u5df2\u786e\u8ba4 gpu \u548c pytorch \u7248\u672c\u6b63\u786e\uff0c\u4f46 gpu \u5229\u7528\u7387\u5374\u5f88\u4f4e\uff0c\u800c cpu \u5229\u7528\u7387\u5374\u5f88\u9ad8\u3002\u539f\u56e0\u4f55\u5728\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u4ee5\u4e0b\u5217\u4e3e\u4e86\u51e0\u79cd\u53ef\u80fd\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff1a 1. gpu \u672a\u6b63\u786e\u914d\u7f6e \u5c1d\u8bd5\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5 gpu \u662f\u5426\u914d\u7f6e\u6210\u529f\uff1a import torch print(torch.cuda.is_available()) \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u8f93\u51fa\u4e3a false\uff0c\u5219\u8868\u793a gpu \u672a\u914d\u7f6e\u597d\u3002 2. \u672a\u6307\u5b9a cuda \u8bbe\u5907 \u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u4e86\u8981\u4f7f\u7528\u7684 cuda \u8bbe\u5907\uff0c\u4f8b\u5982\uff1a import torch device = &#8216;cuda:0&#8217; if torch.cuda.is_available() else &#8216;cpu&#8217; \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5c06\u6a21\u578b\u79fb\u52a8\u5230\u6307\u5b9a\u8bbe\u5907\uff1a ResNet(&#8230;&#8230;).to(device) \u767b\u5f55\u540e\u590d\u5236 3. \u4ee3\u7801\u81ea\u8eab\u95ee\u9898 \u5982\u679c\u4e0a\u8ff0\u6b65\u9aa4\u65e0\u6548\uff0c\u5219\u53ef\u80fd\u662f\u4ee3\u7801\u4e2d\u6709\u5176\u4ed6\u95ee\u9898\u5bfc\u81f4 cpu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-849","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=849"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/849\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}