這個文檔說明了如何在 Mac OS X 上安裝 TensorFlow。python
注意:從 1.2 版本開始,在 Mac OS X 上 TensorFlow 再也不支持 GPU。
你能夠選擇一種方式安裝 TensorFlow,支持下面的幾種選擇:git
咱們建議使用 virtualenv 安裝。virtualenv 是一個和其它 Python 項目開發隔離的虛擬 Python 環境,在同一臺機器上不會干擾也不會被其它程序影響。virtualenv 安裝過程當中,你不單單安裝了 TensorFlow 還有它的全部依賴包。(事實上這很簡單)要開始使用 TensorFlow,你須要「啓動」 virtualenv 環境。總而言之,virtualenv 提供了一個安全可靠的 TensorFlow 安裝和運行機制。github
本地 pip 安裝 TensorFlow 不通過任何容器或者虛擬環境系統直接裝到了系統上,因爲本地 pip 安裝沒被關閉,pip 安裝會干擾或者影響系統上其它有 Python 依賴的安裝。並且,若是要經過本地 pip 安裝,你須要禁用系統完整性保護(SIP)。然而,若是你瞭解 SIP,pip 和 你的 Python 環境,本地 pip 安裝相對容易執行。docker
Docker 可以使 TensorFlow 的安裝徹底脫離於機器上的其它已存在的包,Docker 容器包括 TensorFlow 和它的全部依賴。注意 Docker 鏡像可能很大(幾百 M)。若是你已將 TensorFlow 集成到使用了 Docker 的大型應用架構中能夠選擇 Docker 安裝。shell
選擇 Anaconda,你能夠使用 conda 建立一個虛擬環境,咱們建議使用 pip install
命令而不是 coda install
命令安裝 TensorFlow。macos
注意:coda 包是社區而不是官方支持,也就是說,TensorFlow 團隊既不測試也不維護 conda 包,若是使用風險本身承擔。
按照如下步驟安裝 TensorFlow:小程序
打開終端(一個 shell),你將在這個終端中執行隨後的步驟c#
經過如下命令安裝 pip 和 virtualenv:windows
$ sudo easy_install pip $ sudo pip install --upgrade virtualenv
執行如下任一命令建立虛擬環境:api
$ virtualenv --system-site-packages targetDirectory # for Python 2.7 $ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n
targetDirectory 因虛擬環境根路徑而異,咱們的命令假使 targetDirectory 是 ~/tensorflow
,但你能夠選擇任一目錄。
執行任一命令激活虛擬環境:
$ source ~/tensorflow/bin/activate # If using bash, sh, ksh, or zsh $ source ~/tensorflow/bin/activate.csh # If using csh or tcsh
上面的 source 命令應該將提示符改爲了下面這樣:
(tensorflow)$
若是已經安裝了 pip 8.1 或者更新的版本,執行如下任一命令在激活的虛擬環境中安裝 TensorFlow 及其全部依賴:
$ pip install --upgrade tensorflow # for Python 2.7 $ pip3 install --upgrade tensorflow # for Python 3.n
若是前面的命令執行成功了,跳過步驟 6;若是失敗了,再執行步驟 6。
可選,若是步驟 5 失敗了(通常是由於你使用了低於 8.1 版本的 pip),執行如下任一命令在激活的虛擬環境中安裝 TensorFlow:
$ pip install --upgrade tfBinaryURL # Python 2.7 $ pip3 install --upgrade tfBinaryURL # Python 3.n
tfBinaryURL 是 Tensorflow 包的 URL,準確的 tfBinaryURL 值因操做系統和 Python 版本而異,在這裏找到和你係統相關的 tfBinaryURL 值。例如,你要在 Mac OS X 上安裝 Python 2.7 對應的 Tensorflow 版本,在虛擬環境中安裝 Tensorflow 就執行下面的命令:
$ pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl
若是安裝過程當中遇到麻煩,參考常見安裝問題。
安裝完成後,驗證你的安裝是否工做正常。
注意,每打開一個新的 shell 使用 TensorFlow 都必須激活虛擬環境。若是當前虛擬環境沒有被激活(也就是提示符不是 tensorflow),執行如下任一命令:
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh $ source ~/tensorflow/bin/activate.csh # csh or tcsh
你的提示符變成下面這樣說明 tensorflow 環境已經激活:
(tensorflow)$
當虛擬環境激活後,你能夠在這個 shell 中運行 TensorFlow 程序。若是你再也不使用 TensorFlow,能夠經過下面命令退出環境:
(tensorflow)$ deactivate
提示符將會恢復到默認的(在 PS1 中定義的)。
若是你想卸載 TensorFlow,簡單地移除你建立的目錄。例如:
$ rm -r ~/tensorflow
咱們已經將 TensorFlow 二進制文件上傳到了 PyPI,所以你能夠經過 pip 安裝, REQUIRED_PACKAGES section of setup.py 文件列出了 pip 將要安裝或升級的包。
要安裝 TensorFlow,你的系統必須依據安裝瞭如下任一 Python 版本:
若是你的系統尚未安裝符合以上版本的 Python,如今安裝。
安裝 Python,你可能須要禁用系統完整性保護(SIP)來得到從 Mac App Store 外安裝軟件的許可。
Pip 安裝和管理 Python寫的軟件包,若是你要使用本地 pip 安裝,系統上必須安裝下面的任一 pip 版本:
pip
, for Python 2.7pip3
, for Python 3.n.pip 或者 pip3 可能在你安裝 Python 的時候已經安裝了,執行如下任一命令確認系統上是否安裝了 pip 或 pip3:
$ pip -V # for Python 2.7 $ pip3 -V # for Python 3.n
咱們強烈建議使用 pip 或者 pip3 爲 8.1 或者更新的版本安裝 TensorFlow,若是沒有安裝,執行如下任一命令安裝或更新:
$ sudo easy_install --upgrade pip $ sudo easy_install --upgrade six
假設你的 Mac 上已經裝好了必備的程序,按照如下步驟執行:
執行如下任一命令安裝 TensorFlow:
$ pip install tensorflow # Python 2.7; CPU support $ pip3 install tensorflow # Python 3.n; CPU support
若是上面的命令執行完成,如今能夠驗證你的安裝了。
(可選的) 若是步驟 1 失敗了,執行下面的命令安裝最新版本 TensorFlow:
$ sudo pip install --upgrade tfBinaryURL # Python 2.7 $ sudo pip3 install --upgrade tfBinaryURL # Python 3.n
tfBinaryURL 是 Tensorflow 包的 URL,準確的 tfBinaryURL 值因操做系統和 Python 版本而異,在這裏找到和你係統相關的 tfBinaryURL 值。例如,你要在 Mac OS X 上安裝 Python 2.7 對應的 Tensorflow 版本,在虛擬環境中安裝 Tensorflow 就執行下面的命令:
$ sudo pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl
若是以上命令運行失敗,參考 安裝問題:
安裝完成後,驗證你的安裝是否工做正常。
若是要卸載 TensorFlow,執行下面的命令:
$ pip uninstall tensorflow $ pip3 uninstall tensorflow
按照如下步驟使用 Docker 安裝 TensorFlow:
本節剩下部分解釋如何啓動 Docker 容器。
要啓動包含 TensorFlow 鏡像的 Docker 容器,執行如下命令:
$ docker run -it -p hostPort:containerPort TensorFlowImage
where:
8888
。若是你想在鏡像中運行 TensorBoard,再添加一個-p
參數,hostPort 和 containerPort 都設置爲 6006。gcr.io/tensorflow/tensorflow
: TensorFlow 二進制鏡像,gcr.io/tensorflow/tensorflow:latest-devel
: TensorFlow 二進制鏡像加源碼。gcr.io
是 Goole 的容器註冊表(?),注意部分 TensorFlow 也能夠從 dockerhub 獲取。
例如,下面的命令能夠在 Docker 容器中啓動一個 TensorFlow CPU 鏡像,而後你能夠在鏡像的 shell 中運行 TensorFlow 程序:
$ docker run -it gcr.io/tensorflow/tensorflow bash
如下命令也能夠在 Docker 容器中啓動一個 TensorFlow CPU 鏡像,然而,在這個 Docker 鏡像中,你能夠在 Jupyter notebook 中運行 TensorFlow 程序:
$ docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow
Docker 將會先下載 TensorFlow 鏡像而後啓動它。
如今能夠驗證你的安裝了。
Anaconda 安裝只是社區而非官方支持
按照如下步驟在 Anaconda 環境中安裝 TensorFlow:
按照 Anaconda 下載站點 說明下載安裝 Anaconda
執行如下命令建立名爲 tensorflow
的 conda 環境:
$ conda create -n tensorflow
執行如下命令激活 conda 環境:
$ source activate tensorflow (tensorflow)$ # Your prompt should change
執行如下命令在你的 conda 環境中安裝 TensorFlow:
(tensorflow)$ pip install --ignore-installed --upgrade TF_PYTHON_URL
TF_PYTHON_URL 是 TensorFlow Python 包 的 URL,例如,如下命令是安裝 Python 2.7 CPU-only 版本的 TensorFlow:
(tensorflow)$ pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl
要驗證你的 TensorFlow 安裝,操做如下步驟:
若是你使用本地 pip, virtualenv 或者 Anaconda 安裝,操做如下步驟:
若是經過 Docker 安裝,啓動一個運行 bash 的 Docker 容器,例如:
$ docker run -it gcr.io/tensorflow/tensorflow bash
在一個 shell 中執行 Python:
$ python
在 python 交互式 shell 中輸入如下小程序:
# Python import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))
若是系統輸出如下內容,你能夠開始寫 TensorFlow 程序了:
Hello, TensorFlow!
若是你不熟悉 TensorFlow,參考 Getting Started with TensorFlow。
若是系統輸出錯誤信息而不是歡迎語,參考 常見安裝問題。
咱們依據 Stack Overflow 記錄 TensorFlow 安裝問題和相應的解決方法。下面的表格包括 Stack Overflow 常見的安裝問題回覆連接,若是你遇到的錯誤信息或者其它安裝問題不在表格中,請在 Stack Overflow 上搜索。若是 Stack Overflow 上沒有你搜索的錯誤信息,提一個新問題而且打上 tensorflow
標籤。
Stack Overflow Link | Error Message |
---|---|
42006320 | ImportError: Traceback (most recent call last):File ".../tensorflow/core/framework/graph_pb2.py", line 6, in from google.protobuf import descriptor as _descriptorImportError: cannot import name 'descriptor' |
33623453 | IOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py' |
35190574 | SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed |
42009190 | Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow Found existing installation: setuptools 1.1.6 Uninstalling setuptools-1.1.6: Exception: ... [Errno 1] Operation not permitted: '/tmp/pip-a1DXRT-uninstall/.../lib/python/_markerlib' |
33622019 | ImportError: No module named copyreg |
37810228 | During a pip install operation, the system returns:OSError: [Errno 1] Operation not permitted |
33622842 | An import tensorflow statement triggers an error such as the following:Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 4, in from tensorflow.python import * ... File "/usr/local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_shape_pb2.py", line 22, in serialized_pb=_b('\n,tensorflow/core/framework/tensor_shape.proto\x12\ntensorflow\"d\n\x10TensorShapeProto\x12-\n\x03\x64im\x18\x02 \x03(\x0b\x32 .tensorflow.TensorShapeProto.Dim\x1a!\n\x03\x44im\x12\x0c\n\x04size\x18\x01 \x01(\x03\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3') TypeError: __init__() got an unexpected keyword argument 'syntax' |
42075397 | A pip install command triggers the following error:...You have not agreed to the Xcode license agreements, please run'xcodebuild -license' (for user-level acceptance) or'sudo xcodebuild -license' (for system-wide acceptance) from within aTerminal window to review and agree to the Xcode license agreements.... File "numpy/core/setup.py", line 653, in get_mathlib_info raise RuntimeError("Broken toolchain: cannot link a simple C program")RuntimeError: Broken toolchain: cannot link a simple C program |
一些安裝方法須要 TensorFlow Python 包的 URL,值與三個方面有關(?):
本節記錄了 Mac OS 安裝相關的值
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py3-none-any.whl
若是你沒有遇到 protobuf pip 包相關的問題能夠跳過本節。
注意:若是你的 TensorFlow 運行很慢,多是和 protobuf pip 包有關的問題。
TensorFlow pip 包依賴 protobuf pip 3.1 版本的包,從 PyPI 下載的 protobuf pip 包(在調用 pip install protobuf
時)是一個僅包含 Python 的庫,其中包含執行速度比 C++ 實現慢10 ~ 50 倍的原始序列化/反序列化的Python 實現。 Protobuf 還支持包含基於快速 C++ 的原語解析的 Python 包的二進制擴展,此擴展在標準的僅Python 專用 pip 包中不可用,咱們爲 protobuf 建立了一個包含二進制擴展名的自定義二進制 pip 包。要安裝自定義二進制 protobuf pip 包,請調用如下命令之一:
for Python 2.7:
$ pip install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp27-none-macosx_10_11_x86_64.whl
for Python 3.n:
$ pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp35-none-macosx_10_11_x86_64.whl
安裝這些 protobuf 包將會覆蓋已安裝的包,注意二進制 pip 包已經支持大於 64M 的 protobufs,修復了以下報錯:
[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A protocol message was rejected because it was too big (more than 67108864 bytes). To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
上次更新日期:六月 30, 2017
原文連接:http://www.tensorflownews.com/2017/08/07/install-tensorflow-on-mac/
做者:https://github.com/Enachan