-
说明
本文说明了在Linux操作系统上,以Fedora系统为例,安装Tensorlow环境的步骤。
2017年9月,TensorFlow已经开放到1.3.0版本,支持多种操作系统。接下来我们就用1.3.0版本来介绍TensorFlow的环境准备过程。
-
安装virtualenv
virtualenv是Python的沙箱工具,用于创建独立的Python环境。为了不来回修改各种环境变量,这里用virtualenv为TensorFlow创建一套“隔离”的Python环境。
首先,在root用户下用pip安装virtualenv:
#pip install virtualenv –-upgrade
注:如果是非root用户,需要加参数--user。
安装好后创建一个工作目录,这里直接在home目录下创建了一个tensorflow文件夹:
#virtualenv --system-site-packages /home/tensorflow
然后进入该目录,激活沙箱:
#cd /home/tensorflow/
#source bin/activate
(tensorflow)#
最后,在virtualenv里安装TensorFlow:
(tensorflow)# pip --default-timeout=100 install -U tensorflow
默认安装所需要的依赖,直至安装完成。
-
运行TensorFlow
测试一个简单的例子:
(tensorflow) # python
[GCC 7.1.120170622 (Red Hat 7.1.1-3)] on linux2
Type "help","copyright", "credits" or "license" formore information.
>>>importtensorflow as tf
>>>hello = tf.constant('Hello,TensorFlow!')
>>>sess = tf.Session()
2017-09-1914:43:17.486410: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use SSE4.1 instructions,but these are available on your machine and could speed up CPUcomputations.
2017-09-1914:43:17.486458: 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 CPUcomputations.
2017-09-1914:43:17.486473: W tensorflow/core/platform/cpu_feature_guard.cc:45]The TensorFlow library wasn't compiled to use AVX instructions, butthese are available on your machine and could speed up CPUcomputations.
>>>print sess.run(hello)
Hello,TensorFlow!
>>>
TensorFlow环境已经安装成功了。
注:每次运行TensorFlow程序时,都需要进入tensorflow目录,然后执行source/bin/activate命令来激活沙箱。
4.TensorBoard
TensorBoard是TensorFlow自带的一个强大的可视化工具,也是一个Web应用程序套件。接下来通过一个示例来查看。
首先,在GitHub代码仓库中将1.3.0版本的TensorFlow源代码下载下来。
下载地址:https://github.com/tensorflow/tensorflow/tree/v1.3.0
在Tags中选择1.3.0(最新版本)将跳转到1.3.0版本的代码仓库,下载解压之后即获得源代码。
这里,我们运行手写数字识别的入门例子。如下:
(tensorflow)#pythontensorflow-1.3.0/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py
等程序跑完后,用以下命令打开TensorBoard面板:
(tensorflow)#tensorboard –logdir=/tmp/tensorflow/mnist/logs/mnist_with_summaries
TensorBoard0.1.6 at http://localhost:6006 (Press CTRL+C to quit)
然后我们就可以在浏览器中打开http://localhost:6006,就可以看到TensorBoard面板了。