1. 程式人生 > >深度學習伺服器配置 Ubuntu server 16.04+CUDA8.0+cuDNN7+tensorflow1.3

深度學習伺服器配置 Ubuntu server 16.04+CUDA8.0+cuDNN7+tensorflow1.3

硬體配置

主機:Dell T620塔式伺服器

顯示卡:Nvidia Tesla K20c

系統&驅動配置

Ubuntu Server 16.04

Nvidia driver:375.26

CUDA 8.0

cuDNN 7.0.3

tensorflow1.3

1 ubuntu server系統安裝

系統:Ubuntu Server 16.04

安裝方式:光碟安裝(使用U盤安裝會出現ISO檔案無法掛載的問題)

光碟燒錄系統步驟:

  1. ISO檔案開啟方式選擇Windows光碟映像燒錄機
  2. 點選燒錄

1.1 網路

安裝後可能DHCP服務沒有啟動,需要手動啟動使用dhclient命令

使用ifconfig可以看到網絡卡資訊

[email protected]:~$ ifconfig
eno1      Link encap:Ethernet  HWaddr f0:1f:af:e8:79:0e
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (
0.0 B) TX bytes:0 (0.0 B) Memory:dad00000-dadfffff eno2 Link encap:Ethernet HWaddr f0:1f:af:e8:79:0f inet addr:219.223.196.65 Bcast:219.223.199.255 Mask:255.255.248.0 inet6 addr: 2001:250:3c02:200:760:ae61:106:5168/128 Scope:Global inet6 addr: fe80::306f:6961:652b:a64b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:16373 errors:0 dropped:0 overruns:0 frame:0 TX packets:6720 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:15745390 (
15.7 MB) TX bytes:971604 (971.6 KB) Memory:dae00000-daefffff idrac Link encap:Ethernet HWaddr f0:1f:af:e8:79:11 inet addr:169.254.0.2 Bcast:169.254.0.255 Mask:255.255.255.0 inet6 addr: fe80::b5f1:8170:e317:6c31/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2 errors:0 dropped:0 overruns:0 frame:0 TX packets:33 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:594 (594.0 B) TX bytes:4752 (4.7 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:232 errors:0 dropped:0 overruns:0 frame:0 TX packets:232 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:25105 (25.1 KB) TX bytes:25105 (25.1 KB)

本伺服器的地址就是eno2網絡卡的地址

在校園網需要通過瀏覽器圖形介面登陸賬號密碼才能上網,使用以下命令代替

curl -X POST -F 'action=login' -F 'username=賬戶' -F 'password=密碼' -F 'ac_id=1' -F 'ajax=1' 1' http://10.0.10.66/include/auth_action.php

在使用前需要先能ping通10.0.10.66這個地址,在登陸後測試是否已經聯網

[email protected]:~$ curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視訊</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登入</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登入</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

抓取了網站的內容,已經聯網

1.2 更換軟體源為清華大學

Ubuntu 的軟體源配置檔案是 /etc/apt/sources.list。將系統自帶的該檔案做個備份,將該檔案替換為下面內容,即可使用 TUNA 的軟體源映象。

$ sudo gedit /etc/apt/sources.list

修改如下:

# 預設註釋了原始碼映象以提高 apt update 速度,如有需要可自行取消註釋
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

# 預釋出軟體源,不建議啟用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse

修改後 sudo apt-get update 使修改生效

1.3 遠端登陸

安裝openssh: sudo apt-get install openssh-server

==================================================

檢視GPU使用情況: nvidia-smi

[email protected]:~$ nvidia-smi
Sat Sep 22 15:22:55 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.130                Driver Version: 384.130                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K20c          Off  | 00000000:02:00.0 Off |                    0 |
| 30%   33C    P0    52W / 225W |      0MiB /  4742MiB |     98%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

Ubuntu16.04 系統下K20c CUDA只能裝8.0以上版本

mark

查詢結果

mark

2 Pre-Installation Actions

[email protected]:~$ lspci | grep -i nvidia
02:00.0 3D controller: NVIDIA Corporation GK110GL [Tesla K20c] (rev a1)
[email protected]:~$  uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
[email protected]:~$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[email protected]:~$ uname -r
4.4.0-87-generic

安裝對應的kernels header和開發包:

$ sudo apt-get install linux-headers-$(uname -r)

runfile 推薦 / deb /

3 CUDA 8.0 安裝

解除安裝CUDA相關包:

sudo apt-get remove cuda  
sudo apt-get autoclean 
sudo apt-get --purge remove nvidia*	# 解除安裝Nvidia相關包

然後在目錄切換到/esr/local/下 cd /usr/local/

sudo rm -r cuda-*

3.1 runfile安裝

推薦使用runfile 方式安裝(deb方式解除安裝的時候麻煩)

可以用wget

wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run
sudo sh cuda_8.0.61_375.26_linux.run.26_linux-run

會出現一大堆選項,OPENGL安裝選no,其餘按照yes或者default選。如果已經安裝了新的驅動,不要選擇安裝驅動。

-------------------------------------------------------------
Do you accept the previously read EULA?
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?
(y)es/(n)o/(q)uit: n

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
 [ default is /usr/local/cuda-8.0 ]: 

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location
 [ default is /home/cmfchina ]: 

Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...
Missing recommended library: libGLU.so
Missing recommended library: libX11.so
Missing recommended library: libXi.so
Missing recommended library: libXmu.so

Installing the CUDA Samples in /home/cmfchina ...
Copying samples to /home/cmfchina/NVIDIA_CUDA-8.0_Samples now...
Finished copying samples.

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/cmfchina, but missing recommended libraries

Please make sure that
 -   PATH includes /usr/local/cuda-8.0/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run -silent -driver

3.2 設定環境變數

輸入命令,編輯環境變數配置檔案

sudo vim ~/.bashrc 

在文字末端追加以下兩行程式碼(按鍵“i”進行編輯操作)

export PATH=/usr/local/cuda-8.0/bin:$PATH  
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda

儲存退出,執行下面命令,使環境變數立刻生效

sudo source ~/.bashrc  
sudo ldconfig 

安裝完成後後重啟

3.3 檢查CUDA配置

[email protected]:/home/zjw# nvidia-smi
Sun Sep 23 17:31:45 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.26                 Driver Version: 375.26                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K20c          Off  | 0000:02:00.0     Off |                    0 |
| 30%   32C    P0    52W / 225W |      0MiB /  4742MiB |     98%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

檢查cuda是否配置正確

[email protected]:~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61

3.4 測試CUDA的sammples

進入cude sample code目錄,make 編譯所有demo。注意:因為這裡的make操作是將sample資料夾下所有的demo都編譯了一遍,所以比較好使,如果僅僅想測試某個例子,可以進入相應的資料夾去編譯即可。

# 切換到cuda-samples所在目錄
cd /usr/local/cuda-8.0/samples 或者 cd /home/NVIDIA_CUDA-8.0_Samples 

# 沒有make,先安裝命令 sudo apt-get install cmake,-j是最大限度的使用cpu編譯,加快編譯的速度
make –j

# 編譯完畢,切換release目錄(/usr/local/cuda-8.0/samples/bin/x86_64/linux/release完整目錄)
cd ./bin/x86_64/linux/release

# 檢驗是否成功,執行例項
./deviceQuery 

# 可以認真看看自行結果,它顯示了你的NVIDIA顯示卡的相關資訊,最後能看到Result = PASS就算成功。

編譯完成後切換到 bin 目錄

./deviceQuery
[email protected]:/home/zjw/CUDA_Samples/NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/releas                        e# ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "Tesla K20c"
  CUDA Driver Version / Runtime Version          8.0 / 8.0
  CUDA Capability Major/Minor version number:    3.5
  Total amount of global memory:                 4742 MBytes (4972412928 bytes)
  (13) Multiprocessors, (192) CUDA Cores/MP:     2496 CUDA Cores
  GPU Max Clock rate:                            706 MHz (0.71 GHz)
  Memory Clock rate:                             2600 Mhz
  Memory Bus Width:                              320-bit
  L2 Cache Size:                                 1310720 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536),                         3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Enabled
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 2 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simu                        ltaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Versi                        on = 8.0, NumDevs = 1, Device0 = Tesla K20c
Result = PASS

輸出結果看到顯示卡相關資訊,並且最後Result = PASS ,這說明CUDA才真正完全安裝成功了

再檢查一下系統和CUDA-Capable device的連線情況

[email protected]:/home/zjw/CUDA_Samples/NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release# ./bandwidthTest      [CUDA Bandwidth Test] - Starting...
Running on...

 Device 0: Tesla K20c
 Quick Mode

 Host to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     6160.9

 Device to Host Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     6550.6

 Device to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     146967.3

Result = PASS

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

4 cuDNN

官方安裝文件(照做基本不出問題)

4.1 下載cuDNN

cuDNN是GPU加速計算深層神經網路的庫。首先去官網(https://developer.nvidia.com/rdp/cudnn-download)下載cuDNN,需要註冊一個賬號才能下載,沒有的話自己註冊一個。由於本人的顯示卡是K20c,CUDA 8.0,最新的版本是v7:

mark

下載速度幾KB。Nvidia把國內IP遮蔽了,建議代理換全域性模式,用國外IP就可以下載了,親測。

4.2 安裝cuDNN

安裝cudnn比較簡單,簡單地說,就是複製幾個檔案:庫檔案和標頭檔案。將cudnn的標頭檔案複製到cuda安裝路徑的include路徑下,將cudnn的庫檔案複製到cuda安裝路徑的lib64路徑下。具體操作如下

# 解壓檔案
[email protected]:~$ cp cudnn-8.0-linux-x64-v7.solitairetheme8 cudnn-8.0-linux-x64-v7.tgz
[email protected]:~$ tar -zxvf cudnn-8.0-linux-x64-v7.tgz

#切換到剛剛解壓出來的資料夾路徑
cd cuda 
#複製include裡的標頭檔案(記得轉到include檔案裡執行下面命令)
sudo cp include/cudnn.h  /usr/local/cuda/include/

#複製lib64下的lib檔案到cuda安裝路徑下的lib64(記得轉到lib64檔案裡執行下面命令)
sudo cp lib*  /usr/local/cuda/lib64/

#設定許可權
sudo chmod a+r /usr/local/cuda/include/cudnn.h 
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

#======更新軟連線======
cd /usr/local/cuda/lib64/ 
sudo rm -rf libcudnn.so libcudnn.so.7   #刪除原有動態檔案,版本號注意變化,可在cudnn的lib64資料夾中檢視   
sudo ln -s libcudnn.so.7.0.5 libcudnn.so.7  #生成軟銜接(注意這裡要和自己下載的cudnn版本對應,可以在/usr/local/cuda/lib64下檢視自己libcudnn的版本)
sudo ln -s libcudnn.so.7 libcudnn.so #生成軟連結
sudo ldconfig -v #立刻生效

備註:上面的軟連線的版本號要根據自己實際下載的cudnn的lib版本號

最後我們看看驗證安裝cudnn後cuda是否依舊可用

[email protected]:/usr/local/cuda/lib64$ nvcc --version	# or nvcc -V 
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61

4.3 檢驗cuDNN是否安裝成功

到目前為止,cuDNN已經安裝完了,但是,是否成功安裝,我們可以通過cuDNN sample測試一下(https://developer.nvidia.com/rdp/cudnn-archive 頁面中找到對應的cudnn版本,裡面有 cuDNN v5 Code Samples,點選該連結下載即可,版本可能不一樣,下載最新的就行)

下載完,轉到解壓出的目錄下的mnistCUDNN

# Copy the cuDNN sample to a writable path. 
$cp -r /usr/src/cudnn_samples_v7/ $HOME

# Go to the writable path
$ cd $HOME/cudnn_samples_v7/mnistCUDNN

# Compile the mnistCUDNN sample
$make clean 
$make

Run the mnistCUDNN sample

[email protected]:~/cudnn_samples_v7/mnistCUDNN$ ./mnistCUDNN
cudnnGetVersion() : 7005 , CUDNN_VERSION from cudnn.h : 7005 (7.0.5)
Host compiler version : GCC 5.4.0
There are 1 CUDA capable devices on your machine :
device 0 : sms 13  Capabilities 3.5, SmClock 705.5 Mhz, MemSize (Mb) 4742, MemClock 2600.0 Mhz, Ecc=1, boardGroupID=0
Using device 0

Testing single precision
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 2
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.041376 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.079680 time requiring 57600 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.092288 time requiring 100 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.153120 time requiring 203008 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 4: 0.172800 time requiring 207360 memory
Resulting weights from Softmax:
0.0000000 0.9999399 0.0000000 0.0000000 0.0000561 0.0000000 0.0000012 0.0000017 0.0000010 0.0000000
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 0.9999288 0.0000000 0.0000711 0.0000000 0.0000000 0.0000000 0.0000000
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 0.9999820 0.0000154 0.0000000 0.0000012 0.0000006

Result of classification: 1 3 5

Test passed!

Testing half precision (math in single precision)
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 2
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.061024 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.061312 time requiring 100 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.086560 time requiring 28800 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.172704 time requiring 203008 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 4: 0.186944 time requiring 207360 memory
Resulting weights from Softmax:
0.0000001 1.0000000 0.0000001 0.0000000 0.0000563 0.0000001 0.0000012 0.0000017 0.0000010 0.0000001
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000714 0.0000000 0.0000000 0.0000000 0.0000000
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 1.0000000 0.0000154 0.0000000 0.0000012 0.0000006

Result of classification: 1 3 5

Test passed!

Test passed! 至此,cuDNN已經成功安裝了

5 Anaconda3

Anaconda是python的一個科學計算髮行版,內建了數百個python經常會使用的庫,很多是TensorFlow的依賴庫。安裝好Anaconda可以提供一個好的環境直接安裝TensorFlow。

bash Anaconda3-4.2.0-Linux-x86_64.sh

安裝anaconda,回車後,是許可檔案,接收許可。直接回車即可。最後會詢問是否把anaconda的bin新增到使用者的環境變數中,選擇yes。在終端輸入python發現依然是系統自帶的python版本,這是因為環境變數的更新還沒有生效,命令列輸入如下命令是安裝的anaconda生效。如果conda --version沒有找到任何資訊,說明沒有加入到環境變數沒有,需要手動加入,如圖所示:

mark

[email protected]:/home/zjw# vim ~/.bashrc
[email protected]:/home/zjw# source ~/.bashrc

檢查環境變數是否生效

[email protected]:~$ conda --version
conda 4.4.10

[email protected]:~$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

python版本為 Anaconda版本而非系統自帶,說明環境變數更新生效

6 Tensorflow

大家可以參考TensorFlow的官方安裝教程(https://www.tensorflow.org/install/),官網提供的了 Pip, Docker, Virtualenv, Anaconda 或 原始碼編譯的方法安裝 TensorFlow,我們這裡主要介紹以Anaconda安裝。其他安裝方式,大家可以到官方安裝教程檢視。

6.1 安裝TensorFlow

通過Anaconda安裝TensorFlow CPU,TensorFlow 的官方下載源現在已經在GitHub上提供了(https://github.com/tensorflow/tensorflow),找到對應的版本號,如圖所示:

6.2 建立一個名為tensorflow的conda環境Python 3.6

#Python 2.7
conda create -n tensorflow python=2.7

#Python 3.4
conda create -n tensorflow python=3.4

#Python 3.5
conda create -n tensorflow python=3.5

#Python 3.6
conda create -n tensorflow python=3.6   #我下的TensorFlow對應的Python是3.6版本,那麼我就使用這行

備註:(根據TensorFlow版本號,一定要設定Python版本號,切記切記切記!!!!!重要的事情說三遍!否則後面會報各種錯的)

建立時出錯

[email protected]:~$ conda create -n tensorflow python=3.6
Solving environment: failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.continuum.io/pkgs/main/linux-64/repoa.json.bz2>
Elapsed: -
...

解決方法:(關閉VPN)

以下是輔助,不一定成功

# 首先新增清華的映象源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

6.3 啟用 conda 環境

[email protected]:~$ conda create -n tensorflow pip python=3.6
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.4.10
  latest version: 4.5.11

Please update conda by running

    $ conda update -n base conda



## Package Plan ##

  environment location: /home/zjw/.conda/envs/tensorflow

  added / updated specs:
    - pip
    - python=3.6


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    xz-5.2.3                   |                0         667 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    tk-8.5.18                  |                0         1.9 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    wheel-0.29.0               |           py36_0          88 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    openssl-1.0.2l             |                0         3.2 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    readline-6.2               |                2         606 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    python-3.6.2               |                0        16.5 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    pip-9.0.1                  |           py36_1         1.7 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    sqlite-3.13.0              |                0         4.0 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    certifi-2016.2.28          |           py36_0         216 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    setuptools-36.4.0          |           py36_1         563 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    zlib-1.2.11                |                0         109 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    ------------------------------------------------------------
                                           Total:        29.3 MB

The following NEW packages will be INSTALLED:

    certifi:    2016.2.28-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    openssl:    1.0.2l-0         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    pip:        9.0.1-py36_1     https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    python:     3.6.2-0          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    readline:   6.2-2            https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    setuptools: 36.4.0-py36_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    sqlite:     3.13.0-0         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    tk:         8.5.18-0         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    wheel:      0.29.0-py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    xz:         5.2.3-0          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    zlib:       1.2.11-0         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

Proceed ([y]/n)? y


Downloading and Extracting Packages
xz 5.2.3: ################################################################################### | 100%
tk 8.5.18: ################################################################################## | 100%
wheel 0.29.0: ############################################################################### | 100%
openssl 1.0.2l: ############################################################################# | 100%
readline 6.2: ############################################################################### | 100%
python 3.6.2: ############################################################################### | 100%
pip 9.0.1: ################################################################################## | 100%
sqlite 3.13.0: ############################################################################## | 100%
certifi 2016.2.28: ########################################################################## | 100%
setuptools 36.4.0: ########################################################################## | 100%
zlib 1.2.11: ################################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate tensorflow
#
# To deactivate an active environment, use
#
#     $ conda deactivate

[email protected]:~$ conda activate tensorflow
source activate tensorflow

6.4 在conda環境中安裝TensorFlow GPU版

因為我們前面選擇了conda環境為Python3.6的,所以我們選擇Python3.6版本的GPU連結地址,進行安裝

#如何進行安裝,我們這裡安裝Python版本為3.6的TensorFlow

sudo pip3 install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl

# 備註:連線裡的cpxx和cpxxm的xx是對應Python的版本號#

失敗,我們需要下載GPU版的安裝包,在安裝包下載之後,然後手動進入環境,安裝TensorFlow whl安裝包。

(tensorflow) [email protected]:~$ wget https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl
source activate tensorflow    #啟用tensorflow環境(這步操作了,就忽略)
(tensorflow) [email protected]:~$ pip install --ignore-installed --upgrade tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl
Processing ./tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl
Collecting wheel>=0.26 (from tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/81/30/e935244ca6165187ae8be876b6316ae201b71485538ffac1d718843025a9/wheel-0.31.1-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 96kB/s
Collecting numpy>=1.11.0 (from tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/22/02/bae88c4aaea4256d890adbf3f7cf33e59a443f9985cf91cd08a35656676a/numpy-1.15.2-cp36-cp36m-manylinux1_x86_64.whl (13.9MB)
    100% |████████████████████████████████| 13.9MB 46kB/s
Collecting protobuf>=3.3.0 (from tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/c2/f9/28787754923612ca9bfdffc588daa05580ed70698add063a5629d1a4209d/protobuf-3.6.1-cp36-cp36m-manylinux1_x86_64.whl (1.1MB)
    100% |████████████████████████████████| 1.1MB 110kB/s
Collecting six>=1.10.0 (from tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting tensorflow-tensorboard<0.2.0,>=0.1.0 (from tensorflow-gpu==1.3.0)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. (read timeout=15)",)': /simple/tensorflow-tensorboard/
  Downloading https://files.pythonhosted.org/packages/93/31/bb4111c3141d22bd7b2b553a26aa0c1863c86cb723919e5bd7847b3de4fc/tensorflow_tensorboard-0.1.8-py3-none-any.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 531kB/s
Collecting setuptools (from protobuf>=3.3.0->tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/6e/9c/cc2eb661d85f4aa541910af1a72b834a0f5c9209079fcbd1438fa6da17c6/setuptools-40.4.2-py2.py3-none-any.whl (569kB)
    100% |████████████████████████████████| 573kB 303kB/s
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 329kB/s
Collecting bleach==1.5.0 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/33/70/86c5fec937ea4964184d4d6c4f0b9551564f821e1c3575907639036d9b90/bleach-1.5.0-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/ae/ae/bcb60402c60932b32dfaf19bb53870b29eda2cd17551ba5639219fb5ebf9/html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 274kB/s
Collecting markdown>=2.6.8 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow-gpu==1.3.0)
  Downloading https://files.pythonhosted.org/packages/7a/fd/e22357c299e93c0bc11ec8ba54e79f98dd568e09adfe9b39d6852c744938/Markdown-3.0-py2.py3-none-any.whl (89kB)
    100% |████████████████████████████████| 92kB 331kB/s
Building wheels for collected packages: html5lib
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /home/zjw/.cache/pip/wheels/50/ae/f9/d2b189788efcf61d1ee0e36045476735c838898eef1cad6e29
Successfully built html5lib
Installing collected packages: wheel, numpy, six, setuptools, protobuf, werkzeug, html5lib, bleach, markdown, tensorflow-tensorboard, tensorflow-gpu
Successfully installed bleach-1.5.0 html5lib-0.9999999 markdown-3.0 numpy-1.15.2 protobuf-3.6.1 setuptools-40.4.2 six-1.11.0 tensorflow-gpu-1.3.0 tensorflow-tensorboard-0.1.8 werkzeug-0.14.1 wheel-0.31.1
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

6.5 當你不用 TensorFlow 的時候,關閉環境

source deactivate tensorflow

6.6 安裝成功後,每次使用 TensorFlow 的時候需要啟用 conda 環境(操作步驟2就可以了)

6.7 常見問題

出現“ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory”錯誤資訊

Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:3