原文出處:http://www.cnblogs.com/jacklu/p/6377820.html
個人知乎主頁歡迎關注:https://www.zhihu.com/people/jack_lu,相信我會提供高質量的timeline。
“站在岸上學不會游泳。”看了各種深度學習的新聞、有意思的paper,要開始搭建深度學習環境入坑了。昨天看到一視頻展現了tensorflow在Android平台上的應用,感覺潛力巨大,所以選擇了tensorflow。
結合幾篇安裝博客總結了安裝方法,可能是最簡便的一種了~
筆記本Y430p 顯卡GTX850M
操作系統Ubuntu 16.04(經本人測試 14.04 14.10 15.04 15.10 對雙顯卡的支持都不是特別好)安裝好后建議關掉所有更新選項。
python版本 2.7
1、首先保證安裝好NVIDIA驅動。如下圖所示:
2、安裝CUDA
sudo apt-get update sudo apt-get install nvidia-cuda-toolkit
默認安裝cuda 7.5.18 安裝之后,是沒有/usr/local/cuda*這個文件夾,也沒有sample的
3、由於Ubuntu16.04的gcc和g++都是5.0版的,不兼容CUDA7.5版本,需要降級
sudo apt-get install gcc-4.9 g++-4.9 cd /usr/bin sudo rm gcc sudo rm g++ sudo ln -s gcc-4.9 gcc sudo ln -s g++-4.9 g++
4、安裝cudnn
下載cudnn 5.0 for cuda7.5 需要nvidia的開發者帳號登錄
tar -zxf cudnn-7.5-linux-x64-v5.0-ga.tgz cd cuda
復制頭文件到/usr/local/include
sudo cp include/cudnn.h /usr/local/include/
復制lib文件到/usr/local/lib
sudo cp lib64/* /usr/local/lib/
並編輯~/.bashrc 添加環境變量
export LD_LIBRARY_PATH=/usr/local/lib
5、安裝theano
sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose python-mock python-wheel g++ libopenblas-dev git sudo pip install Theano
編輯配置文件
sudo gedit ~/.theanorc
加入
[global] device = gpu floatX = float32 [nvcc] flags=-D_FORCE_INLINES
注意有符號-
測試,注意cuDNN版本5005
6、安裝tensorflow
根據自己的實際情況參照官網的這張表選擇適合的下載鏈接。
# Ubuntu/Linux 64-bit, CPU only, Python 2.7 export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 # Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl # Mac OS X, CPU only, Python 2.7: export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py2-none-any.whl # Ubuntu/Linux 64-bit, CPU only, Python 3.4 export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 3.4 # Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl # Ubuntu/Linux 64-bit, CPU only, Python 3.5 export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 3.5 # Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl # Mac OS X, CPU only, Python 3.4 or 3.5: export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py3-none-any.whl
我在這里選擇 64-bit GPU Python 2.7
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
然后根據自己情況選擇
# Python 2 pip install --upgrade $TF_BINARY_URL # Python 3 pip3 install --upgrade $TF_BINARY_URL
我在這里選擇Python 2
pip install --upgrade $TF_BINARY_URL
測試Tensorflow是否安裝成功並使用了CUDA,依次執行以下python代碼
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)
實驗結果如下,表示安裝成功!可以開始新的征程啦~
remark:
cudnn version should be 5.1
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda
參考資料:
https://zhuanlan.zhihu.com/p/23042536?refer=tomasen
https://www.zhihu.com/question/48027732?from=profile_question_card
http://www.ifcoder.us/2003