初入Tensorflow一览(1)

来源:互联网 发布:数据采集与处理期刊 编辑:程序博客网 时间:2024/06/05 10:35

1. 安装Tensorflow

环境: macOS Sierra 10.12.6

2. 安装方式

在tensorflow的官方指导下,利用virtualenv安装。

3. 安装步骤

3.1 安装virtualenv工具。

 $ sudo pip install --upgrade virtualenv 

3.2 为tensorflow创建环境目录

$ virtualenv --system-site-packages ~/tensorflow$ cd ~/tensorflow
注:上面的命令行都是根据tensorflow的官网教程安装的,忘记记录了,下面开始本人的记实操作步骤。

3.3 进入virtualenv环境并安装tensorflow

本人是在VPN环境下安装的,不知道非VPN环境是否被墙

[wai@waideMacBook ~/tensorflow]$cd bin/

[wai@waideMacBook ~/tensorflow/bin]$source activate

(tensorflow) [wai@waideMacBook ~/tensorflow/bin]$pip install --upgrade tensorflow

Collecting tensorflow

  Downloading tensorflow-1.3.0-cp27-cp27m-macosx_10_11_x86_64.whl (39.4MB)

    100% |████████████████████████████████| 39.4MB 20kB/s 

Collecting six>=1.10.0 (from tensorflow)

  Using cached six-1.10.0-py2.py3-none-any.whl


中间省去各种 Collecting...

Building wheels for collected packages: html5lib, markdown

  Running setup.py bdist_wheel for html5lib ... done

  Stored in directory: /Users/wai/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962

  Running setup.py bdist_wheel for markdown ... done

  Stored in directory: /Users/wai/Library/Caches/pip/wheels/bf/46/10/c93e17ae86ae3b3a919c7b39dad3b5ccf09aeb066419e5c1e5

Successfully built html5lib markdown

Installing collected packages: six, protobuf, backports.weakref, numpy, werkzeug, html5lib, markdown, bleach, tensorflow-tensorboard, funcsigs, pbr, mock, tensorflow

  Found existing installation: six 1.4.1

    Not uninstalling six at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/wai/tensorflow

  Found existing installation: numpy 1.8.0rc1

    Not uninstalling numpy at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/wai/tensorflow

Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 funcsigs-1.0.2 html5lib-0.9999999 markdown-2.6.9 mock-2.0.0 numpy-1.13.1 pbr-3.1.1 protobuf-3.4.0 six-1.10.0 tensorflow-1.3.0 tensorflow-tensorboard-0.1.6 werkzeug-0.12.2


4. 初用tensorflow,hello tensorflow!

[wai@waideMacBook ~/tensorflow/bin]$source activate

(tensorflow) [wai@waideMacBook ~/tensorflow/bin]$python

Python 2.7.10 (default, Feb  7 2017, 00:08:15) 

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf

>>> 

注:因为在virtualenv下安装,所以只能在该环境的python中import tensorflow。

>>> hello = tf.constant('Hello, TensorFlow!')

>>> sess = tf.Session()

2017-09-07 22:42:56.980323: 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.

2017-09-07 22:42:56.980373: 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.

2017-09-07 22:42:56.980392: 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.

2017-09-07 22:42:56.980410: 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.

>>> print(sess.run(hello))

Hello, TensorFlow!

果不其然,虽然都是按照官方指导来操作的,但是还是多多少少遇到一些WARN信息。上面总的意思就是说刚才安装的tensorflow没有采用这四个计算效率更高的库(SSE4.2/AVX/AVX2/FMA)来进行编译,原因为我仅仅是通过pip install来直接安装的。pip install这种安装方式其实就是把人家预编译的tensorflow下载下来然后放置到我电脑的python PATH中,而别人预编译的环境没有采用这四个库来进行编译。如果我想在使用tensorflow的过程中获得更快的计算效率,就得下载tensorflow的源码,通过build的方式来安装。这一步以后再做吧,先熟悉环境。暂且记录下来。之后搞定了再更新。

当然毕竟只是WARN,hello tensorflow是可以正常print的。如果要临时消除这些烦人的WARN,可以通过修改环境变量来屏蔽。
TF(tensorflow)有一个环境变量TF_CPP_MIN_LOG_LEVEL,默认为0:显示所有lOG,往上分别可以设置为:
1:屏蔽INFO级别LOG
2:屏蔽WARN级别LOG
3:屏蔽ERROR级别LOG

>>> import os

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

>>> sess = tf.Session()

>>> print(sess.run(hello))

Hello, TensorFlow!

WARN被屏蔽啦。不过后续的python脚本中开头都得带这个来屏蔽WARN,不知道其他有用的WARN会不会被屏蔽掉。
原创粉丝点击