{"id":51689,"date":"2024-10-08T20:44:17","date_gmt":"2024-10-08T12:44:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51689\/"},"modified":"2024-11-14T10:42:13","modified_gmt":"2024-11-14T02:42:13","slug":"ubuntu-18-04-%e5%ae%89%e8%a3%9d-tensorflow-gpu","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51689\/","title":{"rendered":"Ubuntu 18.04 \u5b89\u88dd Tensorflow (GPU)"},"content":{"rendered":"<h1 id=\"ubuntu-18-04-%e5%ae%89%e8%a3%9d-tensorflow-gpu-unIePQoKBk\">Ubuntu 18.04 \u5b89\u88dd TensorFlow (GPU)<\/h1>\n<p>TensorFlow \u662f\u4e00\u500b\u958b\u6e90\u7684\u6a5f\u5668\u5b78\u7fd2\u6846\u67b6\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6df1\u5ea6\u5b78\u7fd2\u548c\u6578\u64da\u5206\u6790\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u8a08\u7b97\u7684\u4efb\u52d9\uff0c\u4f7f\u7528 GPU \u4f86\u52a0\u901f\u8a08\u7b97\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u6307\u5c0e\u60a8\u5982\u4f55\u5728 Ubuntu 18.04 \u4e0a\u5b89\u88dd TensorFlow \u7684 GPU \u7248\u672c\u3002<\/p>\n<h2 id=\"%e5%89%8d%e7%bd%ae%e6%a2%9d%e4%bb%b6-unIePQoKBk\">\u524d\u7f6e\u689d\u4ef6<\/h2>\n<p>\u5728\u958b\u59cb\u5b89\u88dd\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u7cfb\u7d71\u6eff\u8db3\u4ee5\u4e0b\u8981\u6c42\uff1a<\/p>\n<ul>\n<li>Ubuntu 18.04 \u64cd\u4f5c\u7cfb\u7d71<\/li>\n<li>\u652f\u6301 CUDA \u7684 NVIDIA GPU<\/li>\n<li>\u5b89\u88dd NVIDIA \u9a45\u52d5\u7a0b\u5e8f<\/li>\n<li>\u5b89\u88dd CUDA Toolkit<\/li>\n<li>\u5b89\u88dd cuDNN<\/li>\n<\/ul>\n<h2 id=\"%e6%ad%a5%e9%a9%9f-1%ef%bc%9a%e5%ae%89%e8%a3%9d-nvidia-%e9%a9%85%e5%8b%95%e7%a8%8b%e5%ba%8f-unIePQoKBk\">\u6b65\u9a5f 1\uff1a\u5b89\u88dd NVIDIA \u9a45\u52d5\u7a0b\u5e8f<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88dd NVIDIA \u9a45\u52d5\u7a0b\u5e8f\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nvidia-driver-460<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u91cd\u555f\u7cfb\u7d71\u4ee5\u4f7f\u9a45\u52d5\u7a0b\u5e8f\u751f\u6548\u3002<\/p>\n<h2 id=\"%e6%ad%a5%e9%a9%9f-2%ef%bc%9a%e5%ae%89%e8%a3%9d-cuda-toolkit-unIePQoKBk\">\u6b65\u9a5f 2\uff1a\u5b89\u88dd CUDA Toolkit<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5b89\u88dd CUDA Toolkit\u3002\u53ef\u4ee5\u5f9e NVIDIA \u7684\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09\u6700\u65b0\u7248\u672c\u7684 CUDA Toolkit\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u8a2a\u554f <a href=\"https:\/\/developer.nvidia.com\/cuda-downloads\" rel=\"nofollow noopener\" target=\"_blank\">NVIDIA CUDA Downloads<\/a> \u9801\u9762\u3002<\/li>\n<li>\u9078\u64c7\u60a8\u7684\u64cd\u4f5c\u7cfb\u7d71\u548c\u7248\u672c\uff0c\u7136\u5f8c\u4e0b\u8f09\u76f8\u61c9\u7684\u5b89\u88dd\u5305\u3002<\/li>\n<li>\u6839\u64da\u4e0b\u8f09\u7684\u6587\u4ef6\u985e\u578b\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/li>\n<\/ol>\n<pre><code>sudo dpkg -i cuda-repo-ubuntu1804-_amd64.deb\nsudo apt-key adv --fetch-keys https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/ubuntu1804\/x86_64\/7fa2af80.pub\nsudo apt update\nsudo apt install cuda<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u9700\u8981\u5c07 CUDA \u7684\u8def\u5f91\u6dfb\u52a0\u5230\u74b0\u5883\u8b8a\u91cf\u4e2d\u3002\u7de8\u8f2f <code>~\/.bashrc<\/code> \u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>export PATH=\/usr\/local\/cuda\/bin:$PATH\nexport LD_LIBRARY_PATH=\/usr\/local\/cuda\/lib64:$LD_LIBRARY_PATH<\/code><\/pre>\n<p>\u7136\u5f8c\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<pre><code>source ~\/.bashrc<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f-3%ef%bc%9a%e5%ae%89%e8%a3%9d-cudnn-unIePQoKBk\">\u6b65\u9a5f 3\uff1a\u5b89\u88dd cuDNN<\/h2>\n<p>cuDNN \u662f NVIDIA \u63d0\u4f9b\u7684\u6df1\u5ea6\u5b78\u7fd2\u52a0\u901f\u5eab\u3002\u60a8\u9700\u8981\u5f9e NVIDIA \u7684\u7db2\u7ad9\u4e0b\u8f09 cuDNN\uff0c\u7136\u5f8c\u5b89\u88dd\u5b83\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u8a2a\u554f <a href=\"https:\/\/developer.nvidia.com\/cudnn\" rel=\"nofollow noopener\" target=\"_blank\">NVIDIA cuDNN<\/a> \u9801\u9762\uff0c\u4e26\u4e0b\u8f09\u76f8\u61c9\u7248\u672c\u7684 cuDNN\u3002<\/li>\n<li>\u89e3\u58d3\u4e0b\u8f09\u7684\u6587\u4ef6\uff0c\u7136\u5f8c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/li>\n<\/ol>\n<pre><code>sudo cp cuda\/include\/cudnn*.h \/usr\/local\/cuda\/include\nsudo cp cuda\/lib64\/libcudnn* \/usr\/local\/cuda\/lib64\nsudo chmod a+r \/usr\/local\/cuda\/include\/cudnn*.h \/usr\/local\/cuda\/lib64\/libcudnn*<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f-4%ef%bc%9a%e5%ae%89%e8%a3%9d-tensorflow-unIePQoKBk\">\u6b65\u9a5f 4\uff1a\u5b89\u88dd TensorFlow<\/h2>\n<p>\u73fe\u5728\uff0c\u60a8\u53ef\u4ee5\u5b89\u88dd TensorFlow \u7684 GPU \u7248\u672c\u3002\u5efa\u8b70\u4f7f\u7528 Python \u7684\u865b\u64ec\u74b0\u5883\u4f86\u7ba1\u7406\u4f9d\u8cf4\u9805\u3002\u9996\u5148\uff0c\u5b89\u88dd <code>pip<\/code> \u548c <code>virtualenv<\/code>\uff1a<\/p>\n<pre><code>sudo apt install python3-pip\npip3 install virtualenv<\/code><\/pre>\n<p>\u7136\u5f8c\u5275\u5efa\u4e00\u500b\u865b\u64ec\u74b0\u5883\u4e26\u6fc0\u6d3b\u5b83\uff1a<\/p>\n<pre><code>virtualenv --system-site-packages -p python3 .\/venv\nsource .\/venv\/bin\/activate<\/code><\/pre>\n<p>\u6700\u5f8c\uff0c\u5b89\u88dd TensorFlow GPU \u7248\u672c\uff1a<\/p>\n<pre><code>pip install tensorflow-gpu<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f-5%ef%bc%9a%e9%a9%97%e8%ad%89%e5%ae%89%e8%a3%9d-unIePQoKBk\">\u6b65\u9a5f 5\uff1a\u9a57\u8b49\u5b89\u88dd<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b Python \u4ee3\u78bc\u4f86\u9a57\u8b49 TensorFlow \u662f\u5426\u6b63\u78ba\u5b89\u88dd\u4e26\u80fd\u5920\u4f7f\u7528 GPU\uff1a<\/p>\n<pre><code>import tensorflow as tf\nprint(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))<\/code><\/pre>\n<p>\u5982\u679c\u986f\u793a\u53ef\u7528\u7684 GPU \u6578\u91cf\uff0c\u5247\u8868\u793a\u5b89\u88dd\u6210\u529f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-unIePQoKBk\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Ubuntu 18.04 \u4e0a\u5b89\u88dd TensorFlow (GPU) \u6d89\u53ca\u591a\u500b\u6b65\u9a5f\uff0c\u5305\u62ec\u5b89\u88dd NVIDIA \u9a45\u52d5\u7a0b\u5e8f\u3001CUDA Toolkit \u548c cuDNN\uff0c\u6700\u5f8c\u5b89\u88dd TensorFlow\u3002\u9019\u4e9b\u6b65\u9a5f\u78ba\u4fdd\u4e86\u60a8\u7684\u7cfb\u7d71\u80fd\u5920\u5145\u5206\u5229\u7528 GPU \u7684\u8a08\u7b97\u80fd\u529b\uff0c\u5f9e\u800c\u52a0\u901f\u6a5f\u5668\u5b78\u7fd2\u4efb\u52d9\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u9019\u4e9b\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u670d\u52d9\uff0c\u4ee5\u7372\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to install TensorFlow with GPU support on Ubuntu 18.04 for enhanced performance in machine learning and deep learning projects.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-51689","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=51689"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51689\/revisions"}],"predecessor-version":[{"id":51690,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51689\/revisions\/51690"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}