TensorFlow 安裝及問題解決

TensorFlow 爲 Google 開源的新一代深度學習框架,與之前使用的Caffe和Torch基於層的設計理念有所不同,是基於張量流圖的設計思路,這也是今後深度學習框架的一大發展趨勢。

本文默認您的linux計算機已經安裝有Python下面介紹一下安裝與安裝遇到的問題及解決方法。將包含下列內容:

  • CUDA-7.5 與 cuDNN v.5 安裝

  • pip安裝

  • TensorFlow 安裝

  • 安裝成功測試

  • 安裝錯誤及解決


(一) CUDA-7.5 與 cuDNN v.5 安裝

按照NVIDIA官網,分別安裝CUDA和 cuDNN ,注意兩個版本與計算機顯卡都要相對應,下載之前可能需要註冊一下,很方便。


(二) pip 安裝

在命令行下輸入如下安裝命令:

$ sudo apt-get install python-pip python-dev
  • 1

(三) TensorFlow 安裝

這部分如果順利的話一兩行代碼即可搞定,不順利的話,就要看人品了。

1. 環境清理 
注意:如果你之前在計算機上裝過TensorFlow,那麼你需要先卸載掉之前的TensorFlow和protobuf,具體如下:

$ pip uninstall tensorflow
$ pip uninstall protobuf
  • 1
  • 2

2. 選擇合適版本的二進制安裝文件鏈接

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py3-none-any.whl
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3. 下載並安裝TensorFlow

根據自己計算機安裝的Python版本(如何查看本機Python版本,很簡單,請參考這篇文章),選擇在命令行下相應的命令:

# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL

# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_URL
  • 1
  • 2
  • 3
  • 4
  • 5

至此安裝完成,如果安裝過程報錯,該如何解決?又如何測試成功安裝?見下面部分。


(四) 安裝成功測試

$ python
...
>>> 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(32)
>>> print(sess.run(a + b))
42
>>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

恭喜你,安裝成功!


(五) 安裝問題及解決

我主要遇到以下問題:


1. pip 問題

  • 問題: 
    在運行安裝TensorFlow命令行時,報出了關於pip的錯誤。

  • 解決: 
    升級pip,用如下命令行:

$ sudo pip install --upgrade pip 
  • 1

2. 聯網問題

# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL

# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_URL
  • 1
  • 2
  • 3
  • 4
  • 5
  • 解決: 
    爲了方便沒有翻牆的好寶寶,我下載了[Linux 64-bit, GPU enabled, Python 2.7]的whl類型安裝包。下載地址:

    其他版本可以在留言中留下郵箱,我下載後發給您。


3. 安裝後測試出現問題

  • 問題: 
    當測試代碼輸入以下命令時:
>>> import tensorflow as tf
  • 1

   出現了以下問題:“ImportError: cannot import name pywrap_tensorflow”。 
test error

  • 解決:

    首先,我上網查了下,有人這麼說:“python誤以爲tensorflow目錄中的tensorflow就是要導入的模塊。解決辦法:不要在tensorflow中運行python或者ipython。”

    然而我並沒有在tensorflow的目錄中運行,懷疑和之前我打算按另一種源文件安裝的方法Installing from sources來安裝TensorFlow,故下載了它的源文件,於是乎我就把它刪了。然後卸載重裝了TensorFlow。卸載方法:

$ sudo pip uninstall tensorflow
$ pip uninstall protobuf
  • 1
  • 2

   重裝之後,就測試OK了。

原文轉載:http://blog.csdn.net/jasonzzj/article/details/52583891

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