美文网首页机器学习
Tensorflow Mac 安装教程

Tensorflow Mac 安装教程

作者: 杂货铺学徒 | 来源:发表于2018-04-25 13:33 被阅读77次

    macOS:10.13.4

    Tensorflow安装教程在官网或者中文社区也有。也可以参照Tensorflow的GitHub

    过程比较简单,还是简单说下我安装的过程

    1.python环境

    Mac自带,注意下python版本,我的电脑是2.7.5

    2.安装pip

    pip的版本需要在8.0以上,若是之前安装过pip,版本低于8.0,则需要先卸载pip重新安装

    卸载pip命令:

    $ sudo pip uninstall pip
    

    安装pip命令:

    $ sudo easy_install pip
    $ pip --version
    

    我原来是1.5.6,现在为10.0.1

    若pip版本低,会报刺眼的红色警告!!!
    类似:

      Could not find any downloads that satisfy the requirement tensorflow
    
    

    3.安装VirtualEnv

    我们推荐使用 VirtualEnv 创建一个隔离的容器, 来安装 TensorFlow. 这是可选的, 但是这样做能使排查安装问题变得更容易.

    首先, 安装所有必备工具:

    $ sudo easy_install pip  # 如果还没有安装 pip
    $ sudo pip install --upgrade virtualenv
    

    接下来, 建立一个全新的 virtualenv 环境. 为了将环境建在 ~/tensorflow 目录下, 执行:

    $ virtualenv --system-site-packages ~/tensorflow
    $ cd ~/tensorflow
    

    然后, 激活 virtualenv:

    $ source bin/activate  # 如果使用 bash
    $ source bin/activate.csh  # 如果使用 csh
    (tensorflow)$  # 终端提示符应该发生变化
    

    4.安装Tensorflow

    在 virtualenv 内, 安装 TensorFlow:

    python版本

    对应不同python版本有不同安装包(下面tensorflow版本为1.0.0,现在截止目前最新的是1.8.0rc0版本):

    # Mac OS X, CPU only, Python 2.7:
    $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.0-py2-none-any.whl
     
    # Mac OS X, GPU enabled, Python 2.7:
    $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-1.0.0-py2-none-any.whl
    
    # Mac OS X, CPU only, Python 3.4 or 3.5:
    $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.0-py3-none-any.whl
     
    # Mac OS X, GPU enabled, Python 3.4 or 3.5:
    $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-1.0.0-py3-none-any.whl
    
    (tensorflow)$ pip install --upgrade $TF_BINARY_URL
    

    这里有可能会遇到黄色警告,可以不理会依然安装成功。

    当使用完 TensorFlow

    (tensorflow)$ deactivate  # 停用 virtualenv
    
    $  # 你的命令提示符会恢复原样
    

    5.尝试你的第一个 TensorFlow 程序

    ~/tensorflow下新建hello.py文件

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    
    import tensorflow as tf
    
    hello = tf.constant("hello,tensorflow");
    sess = tf.Session()
    print sess.run(hello)
    
    a = tf.constant(10)
    b = tf.constant(32)
    print sess.run(a+b)
    
    matrix1 = tf.constant([[3.,3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1,matrix2)
    result = sess.run(product)
    print result
    
    sess.close()
    

    然后执行

    $ python hello.py 
    

    正常情况下会显示

    (tensorflow) bogon:tensorflow huadong$ python hello.py 
    hello,tensorflow
    42
    [[12.]]
    (tensorflow) bogon:tensorflow huadong$ 
    

    若出现下面的警告:

    2018-04-25 09:43:46.898056: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
    2018-04-25 09:43:46.898104: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
    2018-04-25 09:43:46.898120: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
    2018-04-25 09:43:46.898133: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
    

    解决方法参考这里
    在python文件中增加

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    

    上面文件已经添加

    相关文章

      网友评论

        本文标题:Tensorflow Mac 安装教程

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