1. 程式人生 > >TensorflowGPU版安裝

TensorflowGPU版安裝

檢視GPU資訊

命令

lspci | grep -i nvidia
sudo lshw -numeric -C display

輸出

06:00.0 VGA compatible controller: NVIDIA Corporation GK110B [GeForce GTX TITAN Z] (rev a1)
06:00.1 Audio device: NVIDIA Corporation GK110 HDMI Audio (rev a1)
07:00.0 3D controller: NVIDIA Corporation GK110B [GeForce GTX TITAN Z] (rev
a1)

禁用 nouveau 驅動

操作

sudo vim /etc/modprobe.d/blacklist-nouveau.conf
# 輸入以下內容
blacklist nouveau
options nouveau modset=0

執行

sudo update-initramfs -u

重啟電腦

sudo lspci | grep nouveau

如果沒有內容,則禁用成功。

安裝cuda

官網 下載對應版本的cuda安裝包,我下載的是Ubuntu14.04,cuda8.0,runfile(local)

執行以下命令

wget https://developer.nvidia.com/compute/cuda/8.0
/Prod2/local_installers/cuda_8.0.61_375.26_linux-run sudo /etc/init.d/lightdm stop # 關閉X-server sudo init 3 chmod 777 cuda_8.0.61_375.26_linux-run sudo ./cuda_8.0.61_375.26_linux-run sudo vim ~/.bashrc # 新增以下三行內容 export CUDA_HOME=/usr/local/cuda-8.0 export PATH=/usr/local/cuda-8.0/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-8.0
/lib64:$LD_LIBRARY_PATH source ~/.bashrc #使配置的環境變數生效 sudo vim /etc/ld.so.conf.d/cuda-8-0.conf # 新增以下二行內容 /usr/local/cuda-8.0/lib64 /usr/local/cuda-8.0/extras/CUPTI/lib64 sudo ldconfig nvidia-smi #出現GPU相關資訊表示驅動安裝成功 nvcc --version #檢視cuda版本資訊 sudo service lightdm start

驗證

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery  
sudo make  
sudo ./deviceQuery

保證編譯沒有error,執行deviceQuery輸出資訊的最後類似以下:

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 2, Device0 = GeForce GTX TITAN Z, Device1 = GeForce GTX TITAN Z
Result = PASS

安裝cuDNN

為Deep Learning框架設計的一套GPU計算加速方案

下載地址,選擇cuDNN v5.1 Library for Linux

執行以下命令

tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz 
cd cuda
sudo cp lib64/* /usr/local/cuda-8.0/lib64/
sudo cp include/cudnn.h /usr/local/cuda-8.0/include/
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda-8.0/lib64/libcudnn*
sudo ln -sf libcudnn.so.5.1.10 libcudnn.so.5  
sudo ln -sf libcudnn.so.5 libcudnn.so  
sudo ldconfig  

安裝tensorflow

Install pip

sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pip python3-dev
sudo pip3 install --upgrade pip

Install TensorFlow

sudo pip3 install tensorflow         #CPU
sudo pip3 install tensorflow-gpu     #GPU

Validate your installation

  • 程式碼
#!/usr/bin/python3.5

import tensorflow as tf
print('Loaded TF version', tf.__version__)
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
  • 執行結果
Loaded TF version 1.2.1
b'Hello, TensorFlow!'

bug

ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory

cd /usr/local/cuda-8.0/lib64
sudo ln -s libcudnn.so.6.0.21 libcudnn.so.5

Not uninstalling pip at /usr/lib/python2.7/dist-packages, owned by OS

用root許可權執行以下指令碼

import pip
from subprocess import call

for dist in pip.get_installed_distributions():
    call("pip install --upgrade " + dist.project_name, shell=True)

如果還是不行sudo apt-get remove python-pip3

解除安裝

sudo apt-get remove --purge nvidia-*        #解除安裝驅動
sudo pip3 uninstall tensorflow