安装TensorFlow从来就不是个技术活,但你可能还是要花不少的时间,遇到不少的坑,把心得写一写,免得在一个坑上踩两次。

参考网址

确定安装哪个TensorFlow

  • TensorFlow仅支持CPU支持。如果您的系统没有NVIDIACUDA®GPU,则应安装此版本。请注意,具有CPU支持的TensorFlow通常比具有GPU支持的TensorFlow更容易安装。因此,即使您有NVIDIA CUDA GPU,我们建议先安装此版本作为诊断步骤,以防您在安装具有GPU支持的TensorFlow时出现问题。
  • TensorFlow支持GPU。TensorFlow程序通常在GPU上比在CPU上运行得更快。因此,如果您的系统具有NVIDIA CUDA GPU满足以下所示的先决条件,并且需要运行性能关键型应用程序,则应最终安装此版本。
    由于本人当前用的是黑苹果,没有独显,所以安装的是cpu版本的Tensorfly,基于python3.6

    确定如何安装TensorFlow

    Tensorflow支持多种安装机制。选择如下

  • virtualenv

  • “native” pip
  • Docker
  • Anaconda

在这里我选择了Anaconda作为安装环境。步骤如下:

  1. 按照 Anaconda下载网站 上的说明下载并安装Anaconda。图形界面或者是命令行版本的都没问题.
    这是个大大大坑
    看似最简单的一步,我执行第二步一直是’command not found: conda’,究竟是怎么肥四!学过Linux的小伙伴应该清楚,这是因为conda没在环境变量里头。只需要在终端配置文件中加入export PATH=~/anaconda3/bin:$PATH即可。可是让人感到不解的是,教程里可没有这一步啊,理应在安装Anaconda时他会自动加入环境变量的。迷糊了很久我发现了蛛丝马迹:本人终端用的是zsh,系统默认的是bash。于是乎,我打开了bash的配置文件,果不其然!在里头发现了环境变量╮( ̄▽ ̄)╭。# added by Anaconda3 installer export PATH="/Users/wangxiaobin/conda3_command_line/bin:$PATH"所以说,得出的最终结论是Anaconda3 installer默认配置bash的环境变量是,而非正在使用的zsh。

  2. 创建一个tensorflow 通过调用以下命令命名的conda环境

1
$ conda create -n tensorflow
  1. 通过发出以下命令激活conda环境:
    1
    2
    3
    $ source activate tensorflow ##激活
    (tensorflow)$ #你的提示符应该改变
    $ source deactivate tensorflow ##退出激活

为了方便激活,可以设置别名alias,如alias tensorfly = 'source activate tensorflow'

  1. 在公共环境中安装TensorFlow(python2.x使用pip):
    1
    (tensorflow)$ pip3 install --upgrade --ignore-installed tensorflow

这里又是个大大大坑
报错:
Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow
换个TensorFlow Python包的URL,因为是google源,可能会不稳定导致连接超时。

1
(tensorflow)$ pip3 install --upgrade --ignore-installed https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl

报错:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Users/wangxiaobin/env/tensorflow/lib/python3.7/site-packages/six.py' Consider using the–useroption or check the permissions.这次好办,有解决的提示。

1
(tensorflow)$ pip3 install --upgrade --ignore-installed https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl --user

  1. 测试TensorFlow
    1
    2
    3
    4
    5
    6
    7
    8
    9
    (tensorflow)$ python3
    #进入python3环境
    >>> import tensorflow as tf
    >>> a=tf.constant([1.0,2.0])
    >>> b=tf.constant([2.0,3.0])
    >>> sess=tf.Session()
    2018-07-23 16:30:00.791342: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    >>> print(sess.run(a+b))
    [3. 5.]

测试成功!

Comments

⬆︎TOP