Ubuntu16.04下Tensorflow-gpu安裝


踩了無數坑之后(基本上網上出現的所有問題我都不止一次地遇到過:循環登錄,分辨率變差,黑屏等等),終於把帶GPU加速的tensorflow所有相關軟件都安裝好了。因為過程太不容易了,光linux系統我就重裝了兩次,每一次重裝都是相當地絕望了,所以我希望把安裝方法好好總結一下,希望能對別人有所幫助。

 

在我的電腦上各種軟件的版本:

linux: Ubuntu 16.04;      

uname -m && cat /etc/*release

anaconda:  conda 4.5.11

conda --version

python:    python3.7

顯卡:一般有兩塊顯卡,一是:Intel集成顯卡,二是:Nvidia顯卡 (參考:https://blog.csdn.net/yan_chou/article/details/72847943)

我的nvidia顯卡的型號:GeForce GTX 950M

顯卡驅動:410.48

cuda: cuda10.0

cudnn:  cudnn 7.3.0 (針對cuda10.0)

tensorflow:  1.11.0

關於cuda的介紹:

CUDA ® is a parallel computing platform and programming model invented by NVIDIA.
It enables dramatic increases in computing performance by harnessing the power of the
graphics processing unit (GPU).
CUDA was developed with several design goals
in mind: ‣Provide a small set of extensions to standard programming languages, like C, that enable a straightforward implementation of parallel algorithms. With CUDA C/C++, programmers can focus on the task of parallelization of the algorithms rather than spending time on their implementation.
Support heterogeneous computation
where applications use both the CPU and GPU. Serial portions of applications are run on the CPU, and parallel portions are offloaded to the GPU. As such, CUDA can be incrementally applied to existing applications. The CPU and GPU are treated as separate devices that have their own memory spaces. This configuration also allows simultaneous computation on the CPU and GPU without contention for memory resources.

關於cudnn的介紹:

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks.
cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling,
normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK. Deep learning researchers and framework developers worldwide rely on cuDNN for high-performance GPU acceleration.
It allows them to focus on training neural networks and developing software applications rather than spending time on
low-level GPU performance tuning. cuDNN accelerates widely used deep learning frameworks, including Caffe,Caffe2, Chainer,
Keras,MATLAB, MxNet, TensorFlow, and PyTorch. For access to NVIDIA optimized deep learning framework containers,
that has cuDNN integrated into the frameworks, visit NVIDIA GPU CLOUD to learn more and get starte

一個簡單的理解就是,cuda在GPU上進行並行計算的平台,cuda針對deep learning計算時,又有了一個針對deep learning進行優化的深度學習的庫。顯卡驅動是底層的硬件驅動。

 

介紹結束了,下面簡短歸納一下網上的幾類安裝方法(我這里寫的不是具體的步驟,只是現有方法的大概總結,具體安裝方法在后面):

1.先安裝NVIDIA顯卡驅動,再cuda,再cudnn,再tensorflow-gpu:

這種方法再安裝驅動部分又分為以下幾種類型:

(1).直接從ubuntu里的系統設置,軟件與更新,附加驅動里選擇添加新驅動(不成功,因為根本無法選);

(2). 終端添加ppa安裝源,然后apt-get install的形式安裝,會造成循環登錄,解決方法是卸載安裝的驅動(不可行);

(3). 直接從nvidia官網下載驅動文件,以sh **.run形式單獨進行驅動安裝(可以成功安裝驅動,但后續會出問題)。

關於第三種情況,我嘗試了很久,網上方法很多,我認為這兩篇最有幫助:

https://blog.csdn.net/Zafir_410/article/details/73188228?utm_source=blogxgwz0

https://blog.csdn.net/ksws0292756/article/details/79160742

但他們在安裝驅動這一步上也並沒有完全解決我的問題,最后我在google上直接貼上錯誤搜索,基本解決了安裝驅動的問題,

也就是在終端運行nvidia-smi能得到正確輸出:

Wed Nov  7 16:05:20 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.48                 Driver Version: 410.48                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 950M    Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   34C    P0    N/A /  N/A |      0MiB /  4046MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

先前我是卡在了:

Error:Unable to load the 'nvidia-drm' kernel module .

按那兩篇博客第二篇那樣並不能解決,在google搜到是修改一個.h文件中的兩行代碼,但歷史記錄找不到了,不能詳細告訴大家了。

然而就算我之前驅動安裝成功,依然還有一個大難題:無法將安裝好的驅動加載出來,打開nvidia x server setting時總是跳出:

You do not appear to be using the NVIDIA X driver on Linux Ubuntu

解決這個問題又有兩種主要方法:

(1). 再安裝Nvidia-prime顯卡驅動管理工具(我的並不好使);

(2).Google上說安裝bumlebee工具,下面這篇csdn博客講了如何安裝blmblebee工具。我安裝之后確實消除了上面那個問題,然而又出現了新問題,The graphical is low(具體記不太清),完全卡死在那里,各種方法都不行。

https://blog.csdn.net/geange/article/details/79284727

 后來我在stackflow看到,其實不支持單獨從nvidai官網下載Nvidia驅動安裝,會導致The graphical is low(具體記不太清)等等問題,再一次崩潰!

 

 

2.終極的正確安裝方法:安裝cuda時順帶安裝顯卡驅動,這才是正解,單獨安裝驅動問題一個接着一個。之前我還納悶,為啥nvidia顯卡驅動沒有官方的安裝指導,原來人家只給了cuda的安裝指導,但可以通過在安裝時選擇要不要安裝Nvidia驅動。都怪沒有過來人指點,才走了這么多彎路,否則可以節省很多時間。

貼上一篇給我指點了一條明路的博客:https://blog.csdn.net/QLULIBIN/article/details/78714596

這篇博客基本上也是翻譯了官方的cuda安裝指導,但是人家在一些地方都做了特別強調,所以非常有用。

可以看到底下博客評論,照着那個方法做基本都很順利。

當然你要自己願意去看官方安裝指導,也非常有幫助,這里給出鏈接:

https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=runfilelocal

這里提醒一點:一定要注意官方指導文檔里的

table 1 Native Linux Distribution Support in CUDA 10.0

,這里說明了ubuntu類型,gcc版本,kernel版本,怎樣的匹配才能支持cuda10.0,這個必須符合。

安裝前對整個過程一定要提前熟練,最好不要誤操作,否則又要走很多彎路。

 

cudnn的安裝比較簡單,只需要注意跟cuda的對應關系,nvidai官網上都明確指明了。

tensorflow-gpu的安裝我都是通過anaconda安裝的,網上有很多教程,這個比較簡單。貼上一篇我用到的:

https://blog.csdn.net/lukaslong/article/details/81092032

 

就這樣,希望每一個人都能安裝順利!

 

ps: 最后再說一下我的最終導致重裝linux的經歷,一次是手殘把linux自帶的python3.5文件夾刪除了,然后各種系統問題,后面在網上查,最有效率的方法就是重裝系統了;一次是安裝了nvidia驅動后,分辨率變差,錯誤修改了/etc/X11/xorg.conf顯卡配置文件,之前卻沒有備份,然后就是終端也打不開了,桌面也進不去了,甚至是ctrl+alt+F1都不行。希望大家不要犯我這樣的錯誤,配置文件修改之前一定要備份,考慮周全,防止誤操作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM