前言
上一篇文章中搭建了CUDA9.0 + cuDNN7.0的基礎環(huán)境.
先簡單說下即將要進行的步驟以及原因。
1. 通過源碼編譯tensorflow指定CUDA版本
然而在我使用pip安裝tensorflow-gpu之后,在import tensorlfow時報錯"libcudart.so.8.0 cannot open shared object file: No such file or directory"熙掺。然后我到cuda的路徑下查看型豁,相關文件的版本都是libcudart.so.9.0xxxxx模叙,再到網(wǎng)上查看相關資料發(fā)現(xiàn)梁丘,pip安裝的tensorflow不支持CUDA9趋厉,但是通過源碼編譯tensorflow缝龄,可以指定CUDA版本汰现。
所以通過源碼編譯的形式安裝tensorflow GPU版。
2. 編譯tensorflow需要bazel( 0.5.4以上版本)
config tensorflow時報錯需要bazel支持叔壤,這里需要安裝bazel.
這里也是一把辛酸淚.
bazel有兩種安裝方式瞎饲,官網(wǎng)提供。我之前貪圖省事選擇的第二種.sh文件直接安裝炼绘,然而官網(wǎng)下載的特別慢嗅战,我就到某網(wǎng)站下載了0.5.2的版本,并且按照官網(wǎng)的步驟安轉(zhuǎn)完畢饭望,一切順利仗哨。但是config tensorflow時提示要0.5.4以上版本,所以只能卸載重新安裝铅辞,因為找了很多資料還是不很清楚upgradd方法厌漂。這里卸載的時候步驟:
????????????????? (1).直接刪掉~/bin文件夾
????????????????? (2).修改環(huán)境變量,去掉之前添加的
????????????????? (3).刪掉~/.bazel文件夾
卸載完之后使用官網(wǎng)提供的第一種方式來安裝bazel以及相關文件斟珊,即Using Bazel custom APT repository 苇倡。
下面正文
一、安裝Bazel
Using Bazel custom APT repository (recommended)
Install JDK 8 by using:
sudo apt-get install openjdk-8-jdk
On Ubuntu 14.04 LTS you'll have to use a PPA:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update&&sudo apt-get install oracle-java8-installer
2. Add Bazel distribution URI as a package source (one time setup)
echo"deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8"|sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg|sudo apt-key add -
If you want to install the testing version of Bazel, replace stable with testing.
3. Install and update Bazel
sudo apt-get update&&sudo apt-get install bazel
Once installed, you can upgrade to a newer version of Bazel with:
sudo apt-get upgrade bazel
其實都是官網(wǎng)的安裝步驟囤踩,親身實踐有效旨椒。
二、源碼編譯安裝Tensorflow
1.克隆tensorflow源碼
git clone--recurse-submodules https://github.com/tensorflow/tensorflow
2.配置tensorflow
進入到tensorflow目錄下堵漱,有一個configure文件
輸入:
./configure
3.配置選項
按如下規(guī)則配置tensorflow
You have bazel 0.6.1 installed.
Please specify the location of python. [Default is /usr/bin/python]:/usr/bin/python3
Found possible Python library paths:
/home/shengchun/tensorflow/models/
/bin
/usr/local/cuda/bin
/sbin
/home/shengchun/tensorflow/models/slim
/usr/bin
/usr/local/sbin
/usr/local/games
/usr/lib/python3/dist-packages
/usr/games
/home/shengchun/mxnet-ssd/mxnet/python
/usr/sbin
/usr/local/bin
/usr/local/lib/python3.5/dist-packages
Please input the desired Python library path to use.? Default is [/home/shengchun/tensorflow/models/]/usr/local/lib/python3.5/dist-packages
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:[Enter]
jemalloc as malloc support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:[Enter]
Google Cloud Platform support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:[Enter]
Hadoop File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [y/N]:[Enter]
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with GDR support? [y/N]:[Enter]
No GDR support will be enabled for TensorFlow.
Do you wish to build TensorFlow with VERBS support? [y/N]:[Enter]
No VERBS support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL support? [y/N]:[Enter]
No OpenCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]:y
CUDA support will be enabled for TensorFlow.
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]:9.0
Please specify the location where CUDA 9.0 toolkit is
installed. Refer to README.md for more details. [Default is
/usr/local/cuda]:[Enter]
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]:7.0.3
Please specify the location where cuDNN 7.0.3 library is
installed. Refer to README.md for more details. [Default is /usr/local/cuda]:[Enter]
**Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at:https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0]**[Enter]
Do you want to use clang as CUDA compiler? [y/N]:[Enter]
nvcc will be used as CUDA compiler.
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Do you wish to build TensorFlow with MPI support? [y/N]:[Enter]
No MPI support will be enabled for TensorFlow.
Please specify optimization flags to use during compilation
when bazel option “–config=opt” is specified [Default is -march=native]:[Enter]
Add “–config=mkl” to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading,
please set the environment variable “TF_MKL_ROOT” every time before
build.
Configuration finished
4.編譯tensorflow, 開啟 GPU 支持:
bazelbuild--config=opt--config=cuda//tensorflow/tools/pip_package:build_pip_package
時間很長综慎。
5.生成whl文件
bazel編譯命令建立了一個名為build_pip_package的腳本。運行如下的命令將會在 /tmp/tensorflow_pkg路徑下生成一個.whl文件:
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
6.pip安裝tensorflow
sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl
7.驗證安裝
打開任意一個新的終端勤庐,注意不要在tensorflow的安裝路徑下示惊,運行
python3
輸入以下代碼
import tensorflow as tf
hello = tf.constant(‘Hello,TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
tensorflow源碼編譯安裝完成好港。