AI机器学习: 在 macOS 上安装谷歌的TensorFlow, machine learning, 人工智能学习

在 macOS 上安装 TensorFlow

本指南将介绍如何在 macOS 上安装 TensorFlow。虽然这些指令可能也适用于其他 macOS 版本,但我们只在满足以下要求的计算机上对这些指令进行过测试(并仅支持这些指令):

  • macOS 10.12.6 (Sierra) 或更高版本

确定如何安装 TensorFlow

您必须选择安装 TensorFlow 的方式。目前可支持如下几种方式:

  • Virtualenv
  • “原生”pip
  • Docker
  • 从源代码安装(详情请参阅这篇单独的指南)。

我们建议采用 Virtualenv 安装方式。 Virtualenv 是一个与其他 Python 开发相互隔离的虚拟 Python 环境,它无法干扰同一计算机上的其他 Python 程序,也不会受其影响。在 Virtualenv 安装过程中,您不仅要安装 TensorFlow,还要安装 TensorFlow 需要的所有软件包。(这一过程其实很简单。)要开始使用 TensorFlow,您只需要“激活”虚拟环境。总而言之,Virtualenv 提供一种安全可靠的机制来安装和运行 TensorFlow。

原生 pip 会直接在您的系统上安装 TensorFlow,而不是通过任何容器或虚拟环境系统。由于原生 pip 安装并未被单独隔离,因此 pip 安装可能会干扰系统中其他基于 Python 的安装或受其影响。此外,您可能需要停用系统完整性保护 (SIP) 才能通过原生 pip 进行安装。但是,如果您熟悉 SIP、pip 和您的 Python 环境,原生 pip 安装就会相对容易执行一点。

Docker 会将 TensorFlow 安装与您计算机上之前就已存在的软件包完全隔离开来。Docker 容器包含了 TensorFlow 及其所有依赖项。请注意,Docker 映像可能会非常大(数百 MB)。如果您想要将 TensorFlow 整合到已在使用 Docker 的某个更大的应用架构中,则可以选择 Docker 安装。

在 Anaconda 中,您可以使用 conda 来创建一个虚拟环境。但是,在 Anaconda 内部,我们建议使用 pip install 命令来安装 TensorFlow,而不要使用 conda install 命令。

注意:conda 软件包是由社区提供支持的,并没有任何官方支持。也就是说,TensorFlow 团队既不测试也不维护 conda 软件包。若使用该软件包,您需要自行承担相关风险。

使用 Virtualenv 进行安装

如果 python3 和 pip3 遇到问题,也可以参阅下面文章:

设置python3为默认版本

绑定 pip3到 python3

请按照以下步骤使用 Virtualenv 安装 TensorFlow:

  1. 启动终端(即 shell)。您将在此 shell 中执行所有后续步骤。
  2. 通过发出以下命令安装 pip 和 Virtualenv:
    $ sudo easy_install pip 
    $ pip install --upgrade virtualenv
  3. 通过发出以下某种格式的命令创建 Virtualenv 环境:
    # for Python 2.7
    $ virtualenv --system-site-packages ~/tensorflow  
    
    # for Python 3.n
    $ virtualenv --system-site-packages -p python3 ~/tensorflow

    ~/tensorflow 用于指定 Virtualenv 树的顶层目录,但您也可以任何修改成其他目录。

  4. 通过发出下列其中一条命令激活 Virtualenv 环境:
    $ cd ~/tensorflow 
    
    # If using bash, sh, ksh, or zsh
    $ source ./bin/activate  
    
    # If using csh or tcsh 
    $ source ./bin/activate.csh

    执行上述 source 命令后,您的提示符应该会变成如下内容:

    (targetDirectory)$
  5. 确保安装 pip 8.1 或更高版本:
    (targetDirectory)$ easy_install -U pip
  6. 发出以下某个命令,将 TensorFlow 及其所需的所有软件包安装到活动 Virtualenv 环境中:
    # for Python 2.7
    (targetDirectory)$ pip install --upgrade tensorflow  
    
    # for Python 3.n
    (targetDirectory)$ pip3 install --upgrade tensorflow
  7. 可选。如果第 6 步失败了(通常是因为您所调用的 pip 版本低于 8.1),请通过发出以下格式的命令在活动 Virtualenv 环境中安装 TensorFlow:
    # Python 2.7 
    $ pip install --upgrade tfBinaryURL  
    
    # Python 3.n 
    $ pip3 install --upgrade tfBinaryURL

    其中 tfBinaryURL 表示 TensorFlow Python 软件包的网址。“tfBinaryURL”的具体值取决于操作系统和 Python 版本。可在此处查找适合您系统的 tfBinaryURL 值。例如,如果您要为装有 Python 2.7 的 macOS 安装 TensorFlow,则在活动 Virtualenv 中安装 TensorFlow 的命令如下所示:

     $ pip3 install --upgrade 
     https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl

如果您遇到任何安装问题,请参阅常见的安装问题

后续步骤

安装好 TensorFlow 后,请验证您的安装以确认安装的软件能否正常运行。

请注意,每次在新的 shell 中使用 TensorFlow 时,您都必须激活 Virtualenv 环境。如果 Virtualenv 环境当前未处于活动状态(即提示符不是 (<i>targetDirectory</i>)),请调用以下某个命令:

$ cd targetDirectory 

# If using bash, sh, ksh, or zsh
$ source ./bin/activate  

# If using csh or tcsh 
$ source ./bin/activate.csh

您的提示符将变成如下所示,这表示您的 tensorflow 环境已处于活动状态:

(targetDirectory)$

当 Virtualenv 环境处于活动状态时,您就可以从该 shell 运行 TensorFlow 程序了。

用完 TensorFlow 后,可以通过发出以下命令来停用此环境:

(targetDirectory)$ deactivate

提示符将恢复为您的默认提示符(由 PS1 所定义)。

卸载 TensorFlow

如果您想卸载 TensorFlow,只需移除您之前创建的树即可。例如:

$ rm -r ~/tensorflow

验证您的安装

要验证您的 TensorFlow 安装,请执行以下操作:

  1. 确保您的环境已准备好运行 TensorFlow 程序。
  2. 运行一个简短的 TensorFlow 程序。

准备您的环境

如果您是在原生 pip、Virtualenv 或 Anaconda 上进行安装,请执行以下操作:

  1. 启动终端。
  2. 如果您是通过 Virtualenv 或 Anaconda 进行安装,请激活您的容器。
  3. 如果您安装了 TensorFlow 源代码,请导航到未包含 TensorFlow 源代码的任何目录。

如果您是通过 Docker 进行安装,请启动一个运行 bash 的 Docker 容器。例如:

$ docker run -it tensorflow/tensorflow bash

运行一个简短的 TensorFlow 程序

从 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,请参阅 TensorFlow 使用入门

常见的安装问题

我们借助 Stack Overflow 来记录 TensorFlow 安装问题及其补救措施。下表包含了 Stack Overflow 上一些常见安装问题解答的链接。如果您遇到了下表中未列出的错误消息或其他安装问题,请在 Stack Overflow 上进行搜索。如果 Stack Overflow 中没有显示相关错误消息,请在 Stack Overflow 上提一个新的问题,并指定 tensorflow 标签。

Stack Overflow 链接 错误消息
42006320
ImportError: Traceback (most recent call last):
File “…/tensorflow/core/framework/graph_pb2.py”, line 6,
in from google.protobuf import descriptor as _descriptor
ImportError: 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 pip install 操作过程中,系统返回了如下错误:

OSError: [Errno 1] Operation not permitted

33622842 import tensorflow 语句触发了如下错误:

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 pip install 命令触发了以下错误:

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 a Terminal 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 软件包的网址

部分安装方式需要您提供 TensorFlow Python 软件包的网址。您所指定的值取决于您的 Python 版本。

Python 2.7

https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py2-none-any.whl

Python 3.4、3.5 或 3.6

https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl

 

 

本文:AI机器学习: 在 macOS 上安装谷歌的TensorFlow, machine learning, 人工智能学习

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.