一、TensorFlow介紹
TensorFlow?是一個采用數據流圖(data flow graphs),用于數值計算的開源軟件庫。節點(Nodes)在圖中表示數學操作,圖中的線(edges)則表示在節點間相互聯系的多維數據數組,即張量(tensor)。它靈活的架構讓你可以在多種平臺上展開計算,例如臺式計算機中的一個或多個CPU(或GPU),服務器,移動設備等等。TensorFlow最初由Google大腦小組(隸屬于Google機器智能研究機構)的研究員和工程師們開發出來,用于機器學習和深度神經網絡方面的研究,但這個系統的通用性使其也可廣泛用于其他計算領域
二、Tensorflow的安裝機器的準備
cuda準備:cuda_8.0.61_375.26_linux.run
下載網址:https://developer.nvidia.com/cuda-downloads
cudnn準備:cudnn-8.0-linux-x64-v6.0.tgz
下載網址:https://developer.nvidia.com/cudnn
安裝系統:Ubuntu16.04 LTS
下載網址:https://www.ubuntu.com/download/desktop
Python版本:Python-2.7
下載網址:https://www.python.org/downloads/release/python-2713/
說明:如果你的電腦顯卡為AMD的顯卡,此處我推薦你安裝CPU版,如果你的電腦為Nvidia的顯卡,此處我推薦你安裝GPU版,AMD的顯卡最好不要安裝GPU版的,而對于Nvidia的顯卡,兩個版本均可。(如果我理解的有錯,希望各位可以提出你們的寶貴意見,我一定會改的)
三、Tensorflow的安裝方式
關于TensorFlow的安裝方式此處介紹三種安裝方式,分別為:pip安裝、virtualenv安裝、源碼安裝。
TensorFlow的版本:TensorFlow分為CPU版和GPU版
3.1、通過pip方式安裝
a)、首先安裝python環境的依賴。
sudo apt-get install python-py python-dev python-pip numpy
b)、直接通過pip安裝,首先將安裝過的pip更新下。
pip install --upgrade pip ------更新pip
pip install tensorflow ------ TensorFlow CPU版
pip install tensorflow-gpu ------TensorFlow GPU版
c)、如果通過上述方法安裝失敗,此處提供另外一種pip的安裝方式
僅CPU版:
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0.cp27-none-linux_x86_64.whl
然后再將tensorflow更新
pip install -- upgrade tensorflow
GPU支持:
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.3.0-cp27-none-linux_x86_64.whl
至此pip版的安裝成功.
測試是否安裝成功,運行一下代碼:
# /usr/bin/env Python
# coding:utf-8
import tensorflow as tf
hello = tf.constant(‘Hello,TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
如果輸出結果為:Hello,TensorFlow!表示安裝成功
3.2、通過virtualenv安裝
a)、首先需要安裝虛擬環境,虛擬環境的好處就是虛擬環境中的變量不會“污染“主系統環境中的變量。
sudo apt-get install python-py python-dev python-pip python-vertualenv numpy
b)、創建并啟動虛擬環境
virtualenv (創建環境的名字。例如:virtualenv test表示創建一個名為test的虛擬環境)
cd (創建的環境中)
source ./bin/activate
c)、安裝tensorflow的過程見pip安裝的過程。
d)、退出虛擬環境,輸入deactivate
例如:(test)$:deactivate
e)、驗證是否安裝成功的方式見pip的驗證方式。
3.3、通過源碼安裝tensorflow,此處我要說明下,tensorflow通過源碼的方式安裝的tensorflow的過程中會出現很多的錯誤需要調試,希望電腦前的你有足夠的耐心調試完畢。(在此小編我替你們向上天祈禱,愿你們有耐心解決問題。)
a)、安裝cuda.首先先安裝cuda,在安裝cuda的過程中需要先關閉桌面,進入tty模式,按ctrl+alt+F1進入tty模式。找到并進入下載的cuda文件夾,然后在終端輸入:
service lightdm stop
sudo sh cuda_8.0.61_375.26_linux.run
以下圖片內容為cuda安裝的具體操作,由于在tty模式下沒有方法截屏,所以在終端中演示了一遍:
在安裝好cuda之后配置cuda的環境變量:
export PATH=”/usr/local/cuda-8.0/bin”
export LD_LIBRARY_PATH=”/usr/local/cuda-8.0/lib64”
然后輸入nvidia-smi如果能出現下面圖片中的內容,表示安裝成功。
此處最好是重啟下機器,輸入reboot重啟。
b)、cudnn的安裝,使用以下操作:
tar zxvf cudnn-8.0-linux-x64-v6.0.tgz
sudo cp cuda/include/* /usr/local/cuda/include/
sudo cp cuda/lib64/* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
c)、在~/.bashrc文件中添加環境變量
sudo gedit ~/.bashrc
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
使設置的環境變量生效:source ~/.bashrc
d)、下載tensorflow源碼
此處沒有安裝git的可以使用sudo apt-get install git安裝GIT
git clone --recurse-submodules https://github.com/tensorflow/tensorflow
--recurse-submodules參數是必須的,用于獲取TensorFlow依賴的protobuf庫
e)、bazel的安裝。在此說明下bazel有兩種安裝方式,一種為自動安裝,一種為二進制安裝。此處我選擇自動安裝。
安裝bazel官網的鏈接.https://docs.bazel.build/versions/master/install-ubuntu.html
首先需要安裝openjdk8:
sudo apt-get install openjdk-8-jdk
其次添加Bazel分發URL作為包源
echo "deb[arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudotee /etc/apt/sources.list.d/bazel.list
curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add–
然后更新并安裝bazel
sudo apt-get update&&sudo apt-getinstall bazel
升級bazel:
sudo apt-get upgrade bazel
驗證bazel是否安裝:輸入bazel會顯示如下圖內容:
f)、安裝tensorflow見以下內容:
cd tensorflow
./configure之后會顯示如下內容:
root@fm-GREATWALL-PC:~/tensorflow#./configure
You have bazel 0.5.3 installed.
Please specify the location of python. [Default is /usr/bin/python]:
Found possible Python library paths:
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.Default is /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:
jemalloc as malloc support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]:
No Google Cloud Platform support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Hadoop File System support? [y/N]:
No Hadoop File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [y/N]:
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with GDR support? [y/N]:
No GDR support will be enabled for TensorFlow.
Do you wish to build TensorFlow with VERBS support? [y/N]:
No VERBS support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL support? [y/N]:
No OpenCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDAsupport will be enabled for TensorFlow.
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to defaultto CUDA 8.0]:
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.mdfor more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN6.0]:
Please specify the location where cuDNN 6 library is installed. Refer to README.md formore details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to buildwith.
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 buildtime and binary size. [Default is: 3.5,5.2]
Do you want to use clang as CUDA compiler? [y/N]:
nvccwill 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]:
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]:
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 theenvironment variable "TF_MKL_ROOT" every time before build.
Configuration finished
當出現Configuration finished時表示配置成功
之后編譯tensorflow(編譯的過程中可能會出現很多WARNING,可以不用管):
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel編譯命令建立了一個名為build_pip_package的腳本。運行如下的命令會在/tmp/tensorflow_pkg路徑中生成一個.whl文件:
bazel-bin/tensorflow/tools/pip_package/build_pip_package/tmp/tensorflow_pkg
安裝pip包:
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.3.0rc2-cp27-cp27mu-linux_x86_64.whl
等pip安裝完成。驗證是否導包完成,使用上文pip的驗證方式。
注:如果首次運行沒有出現Hello,TensorFlow!的字樣,可以選擇重新從./configure開始,也可以試著重啟下機器然后在驗證是否成功
g)、驗證導入成功的效果圖:
test.py文件中的內容如下圖:
四、參考文檔
【1】http://blog.topspeedsnail.com/archives/10116
【2】https://www.tensorflow.org/install/install_sources
【3】http://blog.csdn.net/u013832707/article/details/73161071
【4】http://www.linuxidc.com/Linux/2016-11/137561.htm