Ubuntu 16.04 安裝 Tensorflow Gpu版本

ACool發表於2018-06-20

原文地址:blogof33.com/post/2/

Tensorflow有Pip,Docker, Virtualenv, Anaconda 或 原始碼編譯的方法安裝 ,本文中採用Pip安裝。

因為國內中文教程中有關Pip安裝的很少,官方中文文件也有一些順序上的錯誤,語義的模糊,按照上面的來很容易出錯。所以我就在這裡寫一篇教程吧。

注:建議 Ubuntu 16.04版本

配置:

Linux Distribution: Ubuntu 16.04 64位

Cpu: Intel Core i5 6300HQ

Gpu:GTX 960M

Python:2.7

因為筆者是裝的雙系統,用UEFI引導的沒有關Security Boot(安全啟動),所以出了一點問題。

1.開機進入Bios,關閉Security Boot

2.系統設定(setting)->軟體和更新->附加驅動

  • 先開啟terminal輸入:

    sudo apt-get update

  • 然後將顯示卡驅動選擇為NVIDIA的顯示卡驅動(更改以後需要等待一段時間。)

3.安裝CUDA8

  • cuda toolkit 8.0下載,如圖,選擇deb(local)安裝,網上有些說神馬一定要選runfile,其實deb也很簡單。
  • 注:cuda 7.5只有ubuntu15.04和14.04版本,所以要用cuda 8.0

圖1

  • 下載成功以後按照圖中的命令操作即可安裝完成。 安裝完成以後,cuda預設安裝在了/usr/local/cuda-8.0/目錄處,然後: vim ~/.profile

    注:很多其他發行版是開啟.bash_profile,ubuntu沒有這個,而是.profile

    設定環境變數(加在檔案末尾,每次登陸後自動生效): export PATH="$PATH:/usr/local/cuda-8.0" export LD_LIBRARY_PATH="/usr/local/cuda-8.0/lib64" 儲存以後,繼續在terminal中輸入:: source ~/.profile #使更改的環境變數立即生效 nvidia-smi #測試是否配置成功 結果出現以下相識輸出說明配置成功:

    圖2

4.降低gcc版本

因為ubuntu16.04的gcc編譯器是5.4.0,然而cuda8.0不支援5.0以上的編譯器,因此需要降級,把編譯器版本降到4.9:

  sudo apt-get install g++-4.9
  sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
  sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
  sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
  sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
  sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
  sudo update-alternatives --set cc /usr/bin/gcc
  sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
  sudo update-alternatives --set c++ /usr/bin/g++
  gcc --version

複製程式碼

如果版本顯示出來是4.9就成功了

5.安裝深度學習庫cuDNN

一定要下載cuDNN5.1!!!!,至少現在下載6.0版本後面是有問題的,我之前下載的6.0版本,後面匯入tensorflow時一直報錯,在gaythub上看見有人說6.0“don not work”而5.1可以,不得已退回到5.1,然後便成功了。

  • 下載之前要註冊一個NVIDA DEVELOPER帳號,然後填三個問題就可以啦。如圖選擇cuDNN v5.1 Library for Linux:

    圖3

  • 下載完成以後,解壓並拷貝 CUDNN 檔案到 Cuda Toolkit 8.0安裝路徑下. 假設 Cuda Toolkit 8.0 安裝在 /usr/local/cud-8.0(預設路徑), 執行以下命令(若/usr/local/cuda-8.0/include目錄不存在,先建立一個include目錄):

      tar xvzf cudnn-8.0-linux-x64-v5.1.tgz
      sudo cp cuda/include/cudnn.h /usr/local/cuda-8.0/include
      sudo cp cuda/lib64/libcudnn* /usr/local/cuda-8.0/lib64
      sudo chmod a+r /usr/local/cuda-8.0/include/cudnn.h /usr/local/cuda-8.0/lib64/libcudnn*
複製程式碼

6.Install and upgrade PIP

  • 首先要確保安裝和更新pip:
     sudo apt-get install python-pip python-dev
     pip install --upgrade pip
複製程式碼

7.安裝 Tensorflow Gpu enable python 2.7 版本

pip install --upgrade tensorflow-gpu

測試一下:

       $python 
       Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
       [GCC 5.4.0 20160609] on linux2
       Type "help", "copyright", "credits" or "license" for more information.
       >>> import tensorflow as tf
       >>> hello = tf.constant('Hello, TensorFlow!')
       >>> sess = tf.Session()
       2017-08-25 14:34:54.825013: W tensorflow/core/platform/cpu_feature_guard.cc:45] The 
       TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your 
       machine and could speed up CPU computations.
       2017-08-25 14:34:54.825065: W tensorflow/core/platform/cpu_feature_guard.cc:45] The 
       TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your 
       machine and could speed up CPU computations.
       2017-08-25 14:34:54.825081: W tensorflow/core/platform/cpu_feature_guard.cc:45] The 
       TensorFlow library wasn't compiled to use AVX instructions, but these are available on your 
       machine and could speed up CPU computations.
       2017-08-25 14:34:54.825093: W tensorflow/core/platform/cpu_feature_guard.cc:45] The 
       TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your 
       machine and could speed up CPU computations.
       2017-08-25 14:34:54.825105: W tensorflow/core/platform/cpu_feature_guard.cc:45] The 
       TensorFlow library wasn't compiled to use FMA instructions, but these are available on your 
       machine and could speed up CPU computations.
       2017-08-25 14:34:55.071951: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:893] 
       successful NUMA node read from SysFS had negative value (-1), but there must be at least one 
       NUMA node, so returning NUMA node zero
       2017-08-25 14:34:55.072542: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found 
       device 0 with properties: 
       name: GeForce GTX 960M
       major: 5 minor: 0 memoryClockRate (GHz) 1.176
       pciBusID 0000:01:00.0
       Total memory: 1.95GiB
       Free memory: 1.31GiB
       2017-08-25 14:34:55.072632: I tensorflow/core/common_runtime/gpu/gpu_device.cc:961] DMA: 
       0 
       2017-08-25 14:34:55.072695: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   Y 
       2017-08-25 14:34:55.072730: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] 
       Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960M, pci bus id: 
       0000:01:00.0)
       >>> print(sess.run(hello))
       Hello, TensorFlow!

 
檢視一下GPU開啟情況:
   - "/cpu:0": The CPU of your machine.
   - "/gpu:0": The GPU of your machine, if you have one.
   - "/gpu:1": The second GPU of your machine, etc.


複製程式碼
    >>> import tensorflow as tf
    >>> a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    >>> b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    >>> c = tf.matmul(a, b)
    >>> sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    >>> print(sess.run(c))
    MatMul: (MatMul): /job:localhost/replica:0/task:0/gpu:0
    2017-08-09 09:47:39.461702: I tensorflow/core/common_runtime/simple_placer.cc:847] MatMul: (MatMul)/job:localhost/replica:0/task:0/gpu:0
    b: (Const): /job:localhost/replica:0/task:0/gpu:0
    2017-08-09 09:47:39.461942: I tensorflow/core/common_runtime/simple_placer.cc:847] b: (Const)/job:localhost/replica:0/task:0/gpu:0
    a: (Const): /job:localhost/replica:0/task:0/gpu:0
    2017-08-09 09:47:39.461976: I tensorflow/core/common_runtime/simple_placer.cc:847] a: (Const)/job:localhost/replica:0/task:0/gpu:0
    [[ 22.  28.]
     [ 49.  64.]] 
複製程式碼

至此,Tensorflow安裝成功。

相關文章