天天看點

[TensorFLow] tensorFlow安裝TensorFlow安裝附件

TensorFlow安裝

參考文獻

1. 上面的文獻是官方官網教程,在測試過程中使用到的是Ubuntu 14.04 64bit機器。

2. 推薦的方法有幾種,例如:pip 安裝,docker安裝,virtualEnv安裝,Anaconda安裝。以及從源碼安裝。

3. 下面這個使用了virtualEnv的安裝方式。

3.1 安裝依賴包。

# 在 Linux 上:
$ sudo apt-get install python-pip python-dev python-virtualenv
           

3.2 建立virtualEnv環境。

$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
#然後, 激活 virtualenv:
$ source bin/activate  # 如果使用 bash
$ source bin/activate.csh  # 如果使用 csh
(tensorflow)$  # 終端提示符應該發生變化
           

3.3 whl安裝。

(tensorflow)$ pip install --upgrade <$url_to_binary.whl>
           

3.3.1 對于那個whl使用pip安裝時需要判斷有無顯示卡,沒有

參考文獻

lshw -short
#或者使用下面的
sudo lshw -C display
           

3.3.2 然後選擇那個whl檔案為CPU only.

# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow--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.
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow--cp27-none-linux_x86_64.whl
           

3.3.3 是以在虛拟環境下應該使用如下指令。

但是在使用過程中發現連結逾時,是以可以把這個tensorflow-0.8.0-cp27-none-linux_x86_64.whl檔案單獨下載下傳下來,例如在網頁中直接輸入下面那個whl的網址就可以下載下傳下來,大概21M。建議放到那個virtualEnv的路徑下面去。

(tensorflow)$ pip install --upgrade (path-to-your-whl-file)/tensorflow--cp27-none-linux_x86_64.whl
           

4.安裝過程中需要聯網,如果可以聯網跳過這個步驟,如果聯網有限制,需要自己安裝依賴包,有如下依賴包:

4.1 安裝中需要Downloading/unpacking numpy>=1.8.2 即這個numpy包要大于1.8版本

4.2 Downloading/unpacking protobuf==3.0.0b2 (from tensorflow==0.8.0),這個protobuf安裝起來比較困難。

:~/proj$ protoc –version

程式“protoc”尚未安裝。 您可以使用以下指令安裝:

/* sudo apt-get install protobuf-compiler
:~/proj$ sudo apt-get install protobuf-c
protobuf-c-compiler  protobuf-compiler    
:~/proj$ sudo apt-get install protobuf-compiler 
                安裝那個 [protobuf](http://blog.csdn.net/wutbiao/article/details/45029779) 
                protobuf是google開源的一個序列化方案,根據google的benchmarking測試,各個方面的表現都非常不錯
                運作autogen.sh時
                1. 需要自己手動下載下傳gmock 注釋掉autogen.sh中這個curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip
                2.缺少autoconf,需要安裝下面兩個包。
                 sudo apt-get install autoconf
                sudo apt-get install dh-autoreconf
                3. 在運作autogen.sh完後,執行下面的指令。
                    ./configure --prefix=/usr  
                    make  
                    make check  
                    make install  
                4. 然後
                ~/tensorflow/protobuf-master/python$ cd python/
                接着
                ~/tensorflow/protobuf-master/python$ python setup.py build
                ~/tensorflow/protobuf-master/python$python setup.py install    //這個缺少six檔案。 
                Searching for six>=1.9  ----這個缺少檔案是通過[參考文獻](http://blog.csdn.net/walkandthink/article/details/45200597)中的連結下載下傳的
                Reading https://pypi.python.org/simple/six/
                這樣protobuf==3.0.0b3安裝完成    //這裡需要安裝的是3.0.0b2,版本需要配置成一樣。
*/
           

5.最後還是連接配接外網後下載下傳

(tensorflow)$    pip install tensorflow--cp27-none-linux_x86_64.whl
Requirement already satisfied (use --upgrade to upgrade): *numpy*>= in ./lib/python2/site-packages (from tensorflow==)
Downloading/unpacking *protobuf*==b2 (from tensorflow==)
  Downloading protobuf-b2-py2.py3-none-any.whl (kB): kB downloaded
Downloading/unpacking *wheel* (from tensorflow==)
  Downloading wheel--py2.py3-none-any.whl (kB): 
Requirement already satisfied (use --upgrade to upgrade): *six*>= in ./lib/python2/site-packages (from tensorflow==)
Requirement already satisfied (use --upgrade to upgrade): *setuptools* in ./lib/python2/site-packages (from protobuf==b2->tensorflow==)
Installing collected packages: tensorflow, protobuf, wheel
  Found existing installation: protobuf b3
    Uninstalling protobuf:
      Successfully uninstalled protobuf
Successfully installed tensorflow protobuf wheel
Cleaning up...
           

6.運作tensorflow的“hello world”程式

6.1 打開python終端

$ python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant()
>>> b = tf.constant()
>>> print sess.run(a+b)

>>>
           

6.2 下載下傳源碼後,可以運作下面的程式。從源碼安裝見附件。這個編譯需要聯網。

$ cd tensorflow/models/image/mnist
$ python convolutional.py
Succesfully downloaded train-images-idx3-ubyte.gz  bytes.
Succesfully downloaded train-labels-idx1-ubyte.gz  bytes.
Succesfully downloaded t10k-images-idx3-ubyte.gz  bytes.
Succesfully downloaded t10k-labels-idx1-ubyte.gz  bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Epoch 
Minibatch loss: , learning rate: 
Minibatch error: %
Validation error: %
Epoch 
Minibatch loss: , learning rate: 
Minibatch error: %
Validation error: %
...
...
           

附件

  1. 從源碼安裝,因為這個需要下載下傳那個protobuf的依賴包,可能不能下載下傳,需要單獨到這個連結把tensorflow下載下傳下來,然後參考4.2中的protobuf的編譯。

1.1 需要安裝bazel,也需要先安裝依賴

bazel 預設需要使用JDK1,如你使用JDK1,請下載下傳相應的安裝包。
安裝 Bazel 其他所需依賴: 
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
           

1.2 在git上下載下傳這個https://github.com/bazelbuild/bazel/releases對應機器的shell,然後運作

$ chmod +x PATH_TO_INSTALL.SH
$ ./PATH_TO_INSTALL.SH --user
           

1.3 安裝其它依賴:

# For Python 2.7:
$ sudo apt-get install python-numpy swig python-dev python-wheel
           

1.4 使用bazel編譯。

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer
$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
           

繼續閱讀