1.Tensorflow的主要依賴包
(1) Protocol Buffer
Protocol Buffer是谷歌開發(fā)的處理結(jié)構(gòu)化數(shù)據(jù)的工具,類似于XML和JSON這兩種比較常用的結(jié)構(gòu)化數(shù)據(jù)處理工具。但是Protocal Buffer格式的數(shù)據(jù)和XML或者JSON又有很大的區(qū)別:首先,使用Protocol Buffer時需要先定義數(shù)據(jù)格式schema(Protocol Buffer的具體編碼方式),其序列化后得到的數(shù)據(jù)不是可讀字符串,而是二進制流;其次,Protocol Buffer格式的數(shù)據(jù)不需要任何其他信息就能還原序列化之后的數(shù)據(jù)。Protcol Buffer序列化出來的數(shù)據(jù)要比XML格式的數(shù)據(jù)笑3到10倍,解析時間要快20到100倍。
Protocol Buffer是TensorFlow系統(tǒng)中使用到的重要工具,Tensorflow中的數(shù)據(jù)基本是通過Protocol Buffer來組織的。
(2)Bazel
Bazel是谷歌開源的自動化構(gòu)建工具,谷歌內(nèi)部大部分的應用都是通過它來編譯的。相比傳統(tǒng)的Makefile、Ant或者 Maven,Bazel在速度、可伸縮性、靈活性以及對不同程序語言和平臺的支持上都要更加出色。TensorFlow本身以及谷歌給出的很多官方樣例都是通過Bazel來編譯的。
2.TensorFlow安裝
TensorFlow只支持Nvidia計算能力(compute capability)大于3.0的GPU。如果要支持GPU,那么還需要安裝Nvidia的Cuda Tookit(版本大于等于7.0)和cuDNN(版本大于等于v2)
(1) 使用Docker安裝
Docker是新一代的虛擬化技術,他可以將TensorFlow以及TensorFlow的所有依賴關系統(tǒng)一封裝到Docker鏡像中,從而大大簡化了安裝過程。Docker支持大部分的操作系統(tǒng),如:linux、Mac OS X、Windows。
(2)使用pip安裝
pip是一個安裝、管理Python軟件包的工具,通過pip可以安裝已經(jīng)打包好的TensorFlow以及TensorFlow所需要的依賴關系。
在Ubuntu下的安裝步驟如下:
注意:目前只有安裝了CUDA toolkit7.5和CuDNN v4的64為Ubuntu下才可以通過pip安裝支持GPU的TensorFlow,對于其他系統(tǒng)或者其他CUDA/CuDNN版本的用戶需要從源碼進行安裝來支持GPU是使用。
第一步:安裝pip
$ sudo apt-get install python-pip python-dev
第二步:找到合適的安裝包URL,并安裝
Python 2.7環(huán)境:
僅使用 CPU 的版本
$ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
開啟 GPU 支持的版本 (安裝該版本的前提是已經(jīng)安裝了 CUDA sdk)
$ pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
問題:
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
解決:
$ pip install --upgrade pip
問題:
Failed to establish a new connection: [Errno 101] Network is unreachable',)': /tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
解決:估計是網(wǎng)絡的的問題
直接將鏈接: https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl.zip 放到迅雷里面下載,下載完之后放到相應的目錄下,
$ mv tensorflow-0.5.0-cp27-none-linux_x86_64.whl.zip tensorflow-0.5.0-cp27-none-linux_x86_64.whl
$ sudo pip install tensorflow-0.5.0-cp27-none-linux_x86_64.whl
另外,這樣也可以
$ pip install tensorflow
或者要獲得gpu支持的話:
$ pip install tensorflow-gpu
3.第一個tensorflow程序:
$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('hello tensorflow')
>>> sess = tf.Session()
>>> print sess.run(hello)
hello tensorflow
>>>
>>> a = tf.constant(10)
>>> b = tf.constant(20)
>>> print sess.run(a+b)
30
直接:
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp27-none-linux_x86_64.whl