如何在 Ubuntu 16.04 上安装并使用 TensorFlow

1,688 阅读8分钟
原文链接: www.oschina.net

引言

TensorFlow 是由谷歌构建的用于训练神经网络的开源机器学习软件。TensorFlow 的神经网络以有状态数据流图的形式表示。图中的每个节点表示神经网络在多维数组上执行的操作。这些多维数组通常称为“张量(tensors)”,因此命名为“TensorFlow”。

TensorFlow 是一个深度学习软件系统。根据谷歌机器学习人工智能系统中的排名(RankBrain),TensorFlow 可以很好地用于信息检索。TensorFlow 可以执行图像识别,如谷歌的 Inception,以及人类语言的音频识别。它也有助于解决其他不特定于机器学习的问题,如偏微分方程。

TensorFlow 架构允许部署在桌面、服务器或移动设备中的多个 CPU 或 GPU 上。还有与 CUDA 集成的扩展,CUDA 是 Nvidia 的一个并行计算平台。它可让部署在 GPU 上的用户直接访问虚拟指令集和 GPU 的其他元素,这对于并行计算任务是必需的。

在本教程中,您将安装“只支持 CPU”的 TensorFlow 版本。这对于那些想要安装和使用 TensorFlow 的人来说是非常理想的,不需要依赖 Nvidia 显卡或者不需要运行关键性能的应用程序。

您可以通过以下几种方式安装 TensorFlow。每种方法都有不同的用例和开发环境:

  • Python 和 Virtualenv:在此方法中,您将安装 TensorFlow 并在 Python 虚拟环境中使用 TensorFlow 所需的所有包。这将把您的 TensorFlow 环境与同一台机器上的其他 Python 程序隔离开来。

  • Native pip:在此方法中,您将在系统全局中安装 TensorFlow。这是推荐给多用户系统上使用 TensorFlow 的那些人。这种安装方法不会在包环境中隔离 TensorFlow,并且可能会干扰其他 Python 安装或库。

  • Docker:Docker 是一个容器运行时环境,它将内容完全隔离在系统上预先存在的包中。在这个方法中,您使用包含 TensorFlow 及其所有依赖项的 Docker 容器。这种方法非常适合将 TensorFlow 合并到已经使用 Docker 的更大的应用程序体系结构中。但是这样做的话 Docker 镜像将相当大。

在本教程中,您将在 Python 虚拟环境 virtualenv 中安装 TensorFlow。该方法将 TensorFlow 安装隔离,并快速启动和运行。一旦完成安装,您将通过运行一个简单的 TensorFlow 程序来验证安装是否成功,然后使用 TensorFlow 来执行图像识别。

安装准备

在开始本教程之前,您需要准备以下内容:

步骤 1 — 安装 TensorFlow

在此步骤中,我们将创建一个虚拟环境并安装 TensorFlow。

首先,创建一个名为 tf-demo 的项目目录:

$ mkdir ~/tf-demo

导航到新创建的 tf-demo 目录下:

$ cd ~/tf-demo

然后创建一个名为 tensorflow-dev 的新虚拟环境。运行以下命令创建环境:

$ python3 -m venv tensorflow-dev

这将创建一个新的 tensorflow-dev 目录,该目录将包含环境被激活时需要安装的所有包。它还包括 pip 和一个独立版本的 Python。

现在激活你的虚拟环境:

$ source tensorflow-dev/bin/activate

一旦激活,你将会在你的终端看到如下内容:

(tensorflow-dev)username@hostname:~/tf-demo $

现在可以在虚拟环境中安装 TensorFlow。

运行以下命令安装和升级到 PyPi 中最新版本的 TensorFlow:

(tensorflow-dev) $ pip3 install --upgrade tensorflow

TensorFlow 将开始安装:

Output
Collecting tensorflow
  Downloading tensorflow-1.4.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
    100% |████████████████████████████████| 39.3MB 35kB/s
...
Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0

如果你希望随时关闭虚拟环境,使用下面的命令:

$ deactivate

如果要重新激活环境,导航至项目目录并运行 source tensorflow-dev/bin/activate.

现在,您已经安装了 TensorFlow,让我们确认一下 TensorFlow 是否安装成功。

步骤 2 — 验证安装是否成功

为了验证 TensorFlow 是否安装成功,我们将在 TensorFlow 中使用非 root 用户权限运行一个的简单程序。我们将使用惯用的初学者示例"Hello, world!"作为一种验证形式来验证。我们将使用 Python 的交互式控制台(Python's interactive console)来创建这个程序,而不是创建一个 Python 文件。

要编写程序,请启动 Python 解释器:

(tensorflow-dev) $ python

您将会在终端看到如下的提示

>>>

这是 Python 解释器的提示符,这表明它已经准备好让您开始输入一些 Python 语句。

首先,输入这一行代码来导入 TensorFlow 包,并将其作为本地变量 tf。输入代码后按回车键:

>>> import tensorflow as tf

接下来,添加这行代码来设置信息"Hello, world!":

>>> hello = tf.constant("Hello, world!")

然后创建一个新的 TensorFlow 会话并将其分配给变量 sess

>>> sess = tf.Session()

注意:根据您的环境,您可能会看到如下输出:

Output
2017-06-18 16:22:45.956946: 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 CPU computations.
2017-06-18 16:22:45.957158: 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-06-18 16:22:45.957282: 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-06-18 16:22:45.957404: 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-06-18 16:22:45.957527: 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.

这告诉你,你有一个指令集(instruction set),它可以继续优化,以获得更好的 TensorFlow 性能。如果你看到这个,你可以放心地忽略它并继续下面的步骤。

最后,输入这一行代码,打印出在之前的代码行中构建的 hello TensorFlow 会话的结果:

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

您将在控制台中看到如下输出:

Output
Hello, world!

这表明一切都正常,您可以开始使用 TensorFlow 来做一些有趣的事情。

通过按下 CTRL+D 退出 Python 交互式控制台。

现在让我们使用 TensorFlow 的图像识别 API 来熟悉 TensorFlow 吧。

步骤3 — 使用 TensorFlow 进行图像识别

现在已经安装了 TensorFlow,通过运行一个简单的程序来验证一下,让我们看看 TensorFlow 的图像识别功能。

为了给图像分类,你需要训练一个模型。然后您需要编写一些代码来使用该模型。要了解更多有关这些概念的知识,您可以看一看机器学习的介绍 (An Introduction to Machine Learning)。

TensorFlow 提供了模型和示例的存储库,包括代码和用于对图像进行分类的训练模型。

使用 Git 将 TensorFlow 模型仓库从 GitHub 克隆到您的项目目录中:

(tensorflow-dev) $ git clone https://github.com/tensorflow/models.git

当 Git 将存储库检出新文件夹 models 时,您将看到以下输出:

Output
Cloning into 'models'...
remote: Counting objects: 8785, done.
remote: Total 8785 (delta 0), reused 0 (delta 0), pack-reused 8785
Receiving objects: 100% (8785/8785), 203.16 MiB | 24.16 MiB/s, done.
Resolving deltas: 100% (4942/4942), done.
Checking connectivity... done.

切换到 models/tutorials/image/imagenet 目录:

(tensorflow-dev) $ cd models/tutorials/image/imagenet

此目录包含 classify_image.py 文件,它使用 TensorFlow 来识别图像。这个程序在第一次运行时会从 tensorflow.org 下载一个经过训练的模型。下载该模型需要您的磁盘有 200MB 的可用空间。

在本例中,我们将对预处理好的熊猫图像进行分类。执行这条命令以运行图像分类器程序:

(tensorflow-dev) $ python classify_image.py

您将看到以下输出:

Output
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
custard apple (score = 0.00147)
earthstar (score = 0.00117)

您已经使用了 TensorFlow 的图像识别功能对您的第一个图像进行了分类。

如果您想要使用另一个图像,可以通过将 -- image_file 参数添加到 python3 classify_image.py 中来实现。对于这个参数,您将需要传入图像文件的绝对路径。

最后

您已经在 Python 虚拟环境中安装了 TensorFlow,并通过运行几个示例验证了 TensorFlow 的运行情况。现在您拥有了一些工具,它们可以让您探索其他项目,包括卷积神经网络和 Word Embeddings

TensorFlow 的开发者指南是个非常好的资源和手册。您还可以探索 Kaggle,这是一个检验机器学习概念实际应用的竞争平台,您与其他机器学习、数据科学和统计爱好者进行竞赛。他们有一个优秀的维基主页,你可以看到和分享一些解决方案,其中一些是在统计和机器学习技术的前沿思想。