美文网首页
TensorFlow 安装

TensorFlow 安装

作者: Moscow1147 | 来源:发表于2018-07-08 09:47 被阅读0次
    Grotta Palazzese 帕拉泽塞岩洞餐厅-波利尼亚诺

    TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。

    关于安装 TensorFlow 官方网站上提供了两种方法安装:

    • "native" pip
    • Anaconda

    第一种是直接在系统本地环境下 pip 安装。优点是用户能够直接在任何目录下直接运行 TensorFlow 程序。缺点是可能会影响到系统上其他基于 Python 安装的。

    第二种是使用 Anaconda 去安装。优点是会建立一个单独的虚拟环境,两个项目之间不会互相影响。缺点是 conda package 是由社区维护的,官方团队既不提供测试,也不提供维护,需要自担风险。建议是在 Anaconda 中使用 pip 安装。

    Native pip 安装仅 cpu 版本的:

    C:\> pip3 install --upgrade tensorflow
    

    Native pip 安装 gpu 版本的:

    C:\> pip3 install --upgrade tensorflow-gpu
    

    如果使用 Anaconda 方式安装,建议先了解一下 Anaconda 的使用方式,可以参考一下这篇文章:Anaconda多环境多版本python配置指导

    1. 创建一个名字为 tensorflow 的环境 Python 版本为3.5:

      这里一定要加上 python 版本选择,不然他会给你建立一个空的环境,当你激活这个环境执行命令时,她会调用你本机的命令去执行,如果执行 pip 安装,其实会安装到你本地,和这个环境没什么关系。

      C:> conda create -n tensorflow python=3.5 
      
    2. 激活 tensorflow :

      C:> activate tensorflow
       (tensorflow)C:>  # Your prompt should change 
      
    3. 通过 pip 安装 tensorflow :

      (tensorflow)C:> pip install --ignore-installed --upgrade tensorflow 
      

      安装 gpu 版本的 tensorflow :

      (tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu 
      

    接下来就可以测试是否安装成功了

    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow!')
    >>> sess = tf.Session()
    >>> print(sess.run(hello))
    

    如果输出Hello, TensorFlow!,那么恭喜安装成功。

    如果在第三条语句执行的时候出现了一些包含SSE2,SSE4,AVX,AVX2,FMA等的单词的时候,不要担心这不是错误。这是因为 TensorFlow 默认分发的是不使用 CPU 扩展来编译的版本,而你的 CPU 是支持使用这些的,使用这些扩展可以加快运行速度。

    解决办法有两个:

    1. 忽略这些信息

      可以使用 TF 的环境变量 TF_CPP_MIN_LOG_LEVEL 来设置

      • It defaults to 0, displaying all logs
      • To filter out INFO logs set it to 1
      • WARNINGS additionally, 2
      • and to additionally filter out ERROR logs set it to 3
      import os
      os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
      
    2. 使用源码重新编译

      关于源码安装可以参考 https://www.tensorflow.org/install/install_sources 。官方是不正式支持在 windows 上构建 TensorFlow 的,但是也是可以使用 Bazel 和 CMake 去编译的,有两个链接Bazel on WindowsTensorFlow CMake build。我认为在 Windows 上还是使用 CMake 比较好,看了看前提条件以及步骤,决定不去踩坑了,我需要钱搞一台机器了(其实我 GPU 挺好的,可惜是 AMD 的)。因为见过一句话 :hence will be faster on a CPU that supports AVX and FMA (up to 300%)。无论真假,跟 GPU 相比远远不及是肯定的。如果有 GPU 支持的话,非常耗时的操作会由 GPU 来执行,根本不用关心是否需要使用这些扩展。

    我是通过 Anaconda pip 安装的,这里列一下我出现的问题。

    第一个:

    Collecting setuptools (from protobuf>=3.3.0->tensorflow)
      Could not find a version that satisfies the requirement setuptools (from protobuf>=3.3.0->tensorflow) (from versions: )
    No matching distribution found for setuptools (from protobuf>=3.3.0->tensorflow)
    

    其实我第一次安装是直接 pip 安装的,特别顺利什么问题都没有,一次安装成功,到这里居然不行了。通过查找资料发现是因为自带的setuptools 版本较低,通过命令升级一下就可以:

    pip install setuptools --ignore-installed
    

    还有一个鬼才知道的问题:

    在第一次pip install --ignore-installed --upgrade tensorflow运行的时候,在Collecting protobuf>=3.3.0 (from tensorflow)这个步骤上什么问题都没有。可是解决掉 setuptools 问题后,再一次pip的时候出现了这样的错误信息:

    Could not find a version that satisfies the requirement protobuf>=3.3.0 (from tensorflow) (from versions: )
    No matching distribution found for protobuf>=3.3.0 (from tensorflow)
    

    下意识告诉我卸掉 protobuf (我也不知道为啥),然后我就去做了,pip 和 conda 都告诉我没有安装。接着我就又pip,然后就莫名地好了。

    相关文章

      网友评论

          本文标题:TensorFlow 安装

          本文链接:https://www.haomeiwen.com/subject/shanuftx.html