Ubuntu14.04+tensorflow1.7+python3.6+CUDA8.0+CUDNN5.0(雙版本tensorflow+雙版本python)

前言

自己電腦已有tensorflow1.2+python2.7+CUDA8.0+CUDNN5.0的實驗環境,但新的實驗要求tensorflow1.5以上+python3的環境,不想破壞已有的實驗環境,所以想用anaconda環境配置一下新的,另外tensorflow1.5以上的已編譯好的whl安裝文件都要求CUDA9的版本,所以不想換CUDA版本的前提下選擇源碼編譯tensorflow

綜上,本文是在ubuntu14.04系統已有tensorflow1.2+python2.7+CUDA8.0+CUDNN5.0的前提下利用anaconda增加tensorflow1.7+python3.6+CUDA8.0+CUDNN5.0的環境,兩個環境並存。

step 1:安裝anaconda

下載:

官網下載對應想要的python3.6版本的anaconda,下載網址:https://www.anaconda.com/download/#linux。我下載的是Anaconda3-5.1.0-Linux-x86_64.sh。

安裝:

bash Anaconda3-5.1.0-Linux-x86_64.sh

提示回車,閱讀協議後輸入yes安裝,後面提示加環境變量選擇yes,之後安裝完成!

新打開一個終端,輸入”conda -V”查看anaconda版本。

成功安裝anaconda後可以創建環境了。

step 2: 創建tensorflow1.7環境

創建名爲tf1-7的環境:

conda create -n tf1-7 python=3.6

創建成功之後激活環境

source activate tf1-7

此時命令行前面就會有創建的環境名。

因爲CUDA環境不想改變,所以下面源碼編譯tensorflow1.7.

step 3: tensorflow安裝

後面的操作都是在anaconda下激活的tf1-7環境下.
因爲採用源碼安裝tensorflow,需要用到bazel,這裏提前說一下我一開始安裝的bazel是0.12版本的,但是後來碰到一個錯誤,查閱資料後發現是bazel版本過高導致的,所以又把bazel0.12卸載了裝的bazel0.11.這裏先放出這個錯誤:

`ERROR: /home/UserHome/.cache/bazel/_bazel_UserHome/ab33c8274551e1ea3125872a4c4e7db9/external/jpeg/BUILD:126:12: Illegal ambiguous match on configurable attribute "deps" in @jpeg//:jpeg:
@jpeg//:k8
@jpeg//:armeabi-v7a
Multiple matches are not allowed unless one is unambiguously more specialized.
ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted:

/home/UserHome/.cache/bazel/_bazel_UserHome/ab33c8274551e1ea3125872a4c4e7db9/external/jpeg/BUILD:126:12: Illegal ambiguous match on configurable attribute "deps" in @jpeg//:jpeg:
@jpeg//:k8
@jpeg//:armeabi-v7a
Multiple matches are not allowed unless one is unambiguously more specialized.
INFO: Elapsed time: 1.086s
FAILED: Build did NOT complete successfully (3 packages loaded)`

所以可以注意一下遇到這個錯誤的時候會不會是bazel版本的問題。下面講整個tensorflow的安裝過程。

tensorflow下載https://codeload.github.com/tensorflow/tensorflow/tar.gz/v1.7.0-rc0
安裝bazel0.11:
首先下載bazel0.11:https://github.com/bazelbuild/bazel/releases
安裝bazel之前有一些依賴需要安裝,具體查看:https://docs.bazel.build/versions/master/install-ubuntu.html
安裝bazel:

chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user

注意要帶着–user安裝,表示將會安裝到/home/bin目錄下。
安裝完成後設置環境變量在~/.bashrc添加:PATH=/home/*/bin:$PATH,保存後source一下。
這樣bazel就安裝完成了,下面用bazel來編譯tensorflow。

進入tensorflow源碼文件下,配置tensorflow

./configure

會有一些配置選項要選擇。

$ ./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with GPU support? [y/N] y
GPU support will be enabled for TensorFlow
Please specify which gcc nvcc should use as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 7.5 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5.0
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
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: "3.5,5.2"]: 6.1
Do you wish to build TensorFlow with MPI support? [y/N] n
MPI support will not be enabled for TensorFlow
Configuration finished

過程中不相關的選項選擇no,要指定cuda,cudnn版本及安裝位置,顯卡計算能力可以去給定網址查看。
配置完成後編譯目標程序

bazel編譯tensorflow:

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

這個過程比較長,耐心等待。

創建pip包並安裝。

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
mkdir _python_build
cd _python_build
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* .
ln -s ../tensorflow/tools/pip_package/* .
python setup.py develop

安裝完成,python下import tensorflow就可以調用tensorflow1.7了。

錯誤記錄:
記得碰到一個問題是:
編譯安裝tensorflow GPU版本時報錯:Cannot find libdevice.10.bc under /usr/local/cuda-8.0
解決辦法是:將/usr/local/cuda-8.0/nvvm/libdevice/libdevice.compute_50.10.bc改爲libdevice.10.bc,並複製一份至/usr/local/cuda-8.0/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章