1. 程式人生 > >Tensorflow 入門學習1 pip安裝

Tensorflow 入門學習1 pip安裝

環境:Ubuntu

準備環境

apt-get 更換源

cd /etc/apt
sudo apt-get install vim 
sudo vim sources.list

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

sudo apt-get update
sudo apt-get upgrade

修改pip源:

mkdir ~/.pip
vi ~/.pip/pip.conf
[global]
trusted-host =  pypi.douban.com
index-url = http://pypi.douban.com/simple
sudo apt-get install python-pip
sudo apt-get install python-numpy swig python-dev python-wheel

安裝Nvidia驅動

在系統設定->軟體更新->附加驅動->選擇nvidia最新驅動->應用更改

驗證:輸入命令:

nvidia-smi

顯示NVIDIA-SMI結果。

下載並安裝cuda8

sudo sh cuda_8.0.61_375.26_linux.run

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?
注意是否安裝顯示卡驅動選n。
安裝位置:/usr/local/cuda-8.0
sample:/home/admin1

修改環境配置

sudo vim ~/.bashrc

加上

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

source ~/.bashrc 

對gcc降版,降到5.3以下

gcc --version
sudo apt-get install gcc-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 100

接下來編譯測試cuda的sample

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

正常的話會輸出顯示卡型號資訊。

下載安裝cuDNN

sudo dpkg -i cuda-repo-ubuntu1604-8-0-rc_8.0.27-1_amd64​.deb

安裝 tensorflow

sudo apt-get install libcupti-dev
pip install tensorflow-gpu
**執行:**
sudo ldconfig /usr/local/cuda/lib64
或者
export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH

否則會提示libcublas.so.8.0:cannot open shared object file:No such file or directory

測試

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))