Ubuntu 上安裝 TensorFlow

Ubuntu 上安裝 TensorFlow

Google官方建議採用 Virtualenv 安裝方式。 Virtualenv 是一個與其他 Python 開發相互隔離的虛擬 Python 環境,它無法干擾同一計算機上的其他 Python 程序,也不會受其影響。總而言之,Virtualenv 提供一種安全可靠的機制來安裝和運行 TensorFlow。

1.安裝 pip 和 Virtualenv

sudo apt-get install python-pip python-dev python-virtualenv # for Python 2.7sudo apt-get install python3-pip python3-dev python-virtualenv # for Python 3.n

2. 創建 Virtualenv 環境

$ virtualenv --system-site-packages targetDirectory # for Python 2.7$ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n

targetDirectory 用於指定 Virtualenv 樹的頂層目錄。我們的說明中假定 targetDirectory為 ~/tensorflow,但您可以選擇任何目錄。

3.激活 Virtualenv 環境

$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh$ source ~/tensorflow/bin/activate.csh # csh or tcsh

執行上述 source 命令後,您的提示符應該會變成如下內容:

(tensorflow)$ 

4.安裝 pip 8.1 或更高版本

(tensorflow)$ easy_install -U pip

5.在處於活動狀態的 Virtualenv 環境中安裝 TensorFlow

(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7(tensorflow)$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade tensorflow # for Python 3.n(tensorflow)$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade tensorflow-gpu # for Python 2.7 and GPU(tensorflow)$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade tensorflow-gpu # for Python 3.n and GPU

6.(可選)如果第 5 步執行失敗(通常是因為您所調用的 pip 版本低於 8.1,需要手動在活動 Virtualenv 環境中安裝 TensorFlow:

(tensorflow)$ pip install --upgrade tfBinaryURL # Python 2.7(tensorflow)$ pip3 install --upgrade tfBinaryURL # Python 3.n 

其中 tfBinaryURL 表示TensorFlow Python 軟件包的URL地址。tfBinaryURL 的具體值取決於操作系統、Python 版本 和 GPU 支持。例如,如果您要為裝有 Python 3.4 的 Linux 安裝僅支持 CPU 的 TensorFlow,可發出用以下命令:

(tensorflow)$ pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.6.0-cp34-cp34m-linux_x86_64.whl

安裝測試

從 shell 中調用 Python,如下所示:

$ python

在 Python 交互式 shell 中輸入以下幾行簡短的程序代碼:

# Pythonimport tensorflow as tfhello = tf.constant('Hello, TensorFlow!')sess = tf.Session()print(sess.run(hello))

如果系統輸出以下內容,就說明您可以開始編寫 TensorFlow 程序了:

Hello, TensorFlow!


分享到:


相關文章: