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

介绍

TensorFlow是一个开源的机器学习软件,由谷歌开发,以训练神经网络。TensorFlow的神经网络以 stateful dataflow graphs的形式表达。图表中的每个节点代表了神经网络在多维数组中执行的操作。这些多维数组通常被称为tensors,因此名称为TensorFlow。

TensorFlow是一个深度学习软件系统(https://en.wikipedia.org/wiki/Deep_learning)。TensorFlow对于信息检索工作很好,如Google在其机器学习人工智能系统(RankBrain中的搜索排名中所示。TensorFlow可以执行图像识别,如Google的Inception(https://arxiv.org/abs/1409.4842)所示,以及人类语言音频识别。

TensorFlow 架构允许在桌面、服务器或移动设备中部署多个 CPU 或 GPU. 还提供与 Nvidia 的一种平行计算平台 CUDA的集成扩展。

在本教程中,您将安装TensorFlow的仅支持CPU版本. 此安装对于想要安装和使用TensorFlow的人来说是理想的,但他们没有Nvidia显卡或不需要运行性能关键的应用程序。

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

  • Python 和 Virtualenv : 在这种方法中,你会安装 TensorFlow 和在 Python 虚拟环境中使用 TensorFlow 所需的所有套件。这将你的 TensorFlow 环境与同一台机器上的其他 Python 程序隔离。
  • Native pip :在这种方法中,你会在你的系统上安装 TensorFlow 全球范围内。这对于希望在多用户系统上让 TensorFlow 可供所有人使用的人来说是非常有用的。 这种安装方法不会在包含的环境中隔离 TensorFlow 并可能干扰其他 Python 安装或库。
  • Docker :Docker 是一个集装箱运行环境,并且完全将其内容从系统上现有的包隔离出来

在本教程中,您将安装TensorFlow在Python虚拟环境中使用virtualenv。这种方法将TensorFlow安装隔离,并使事情快速运行.一旦完成安装,您将通过运行一个短的TensorFlow程序验证您的安装,然后使用TensorFlow进行图像识别。

前提条件

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

第1步:安装TensorFlow

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

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

1mkdir ~/tf-demo

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

1cd ~/tf-demo

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

1python3 -m venv tensorflow-dev

这将创建一个新的tensorflow-dev目录,该目录将包含您在启用此环境时安装的所有包,还包括pip和Python的独立版本。

现在启用您的虚拟环境:

1source tensorflow-dev/bin/activate

一旦激活,您将在您的终端中看到类似的东西:

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

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

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

1pip3 install --upgrade tensorflow

TensorFlow 将安装:

1[secondary_label Output]
2Collecting tensorflow
3  Downloading tensorflow-1.4.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
4    100% |████████████████████████████████| 39.3MB 35kB/s
5
6...
7
8Successfully 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

<$>[注] 如果您想随时禁用虚拟环境,该命令是:

1deactivate

若要稍后重新激活环境,请导航到您的项目目录并运行 source tensorflow-dev/bin/activate

现在,你已经安装了TensorFlow,让我们确保TensorFlow安装工作。

步骤2 - 验证安装

为了验证TensorFlow的安装,我们将作为非根用户在TensorFlow中运行一个简单的程序,我们将使用典型的初学者的例子你好,世界!作为验证的一种形式。

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

1python

您将看到下面的提示在您的终端中出现

1>>>

这是 Python 解释器的提示,它表明它已经为您准备好开始输入一些 Python 陈述。

首先,键入此行以导入 TensorFlow 包,并将其作为本地变量 tf 提供。

1import tensorflow as tf

接下来,添加此代码行以设置你好,世界!的消息:

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

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

1sess = tf.Session()

<$>[注] :根据您的环境,您可能会看到以下输出:

1[secondary_label Output]
22017-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.
32017-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.
42017-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.
52017-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.
62017-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.

這告訴你,你有一個 指令集,它有可能被優化為使用 TensorFlow 更好的性能. 如果你看到這一點,你可以安全地忽略它並繼續。

最后,输入此代码行以打印您在以前的代码行中构建的你好 TensorFlow 会话的结果:

1print(sess.run(hello))

您将在您的控制台中看到此输出:

1[secondary_label Output]
2Hello, world!

这表明一切都起作用,你可以开始使用TensorFlow来做一些更有趣的事情。

通过按CTRL+D离开 Python 交互式控制台。

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

步骤 3 — 使用 TensorFlow 用于图像识别

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

为了分类一个图像,你需要训练一个模型,然后你需要写一些代码来使用该模型. 要了解更多关于这些概念,你可以看看 机器学习的介绍

TensorFlow提供一个模型和示例库(https://github.com/tensorflow/models),包括代码和训练有素的图像分类模型。

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

1git clone https://github.com/tensorflow/models.git

您将看到以下输出,因为Git将存储库检查到一个名为模型的新文件夹:

1[secondary_label Output]
2Cloning into 'models'...
3remote: Counting objects: 8785, done.
4remote: Total 8785 (delta 0), reused 0 (delta 0), pack-reused 8785
5Receiving objects: 100% (8785/8785), 203.16 MiB | 24.16 MiB/s, done.
6Resolving deltas: 100% (4942/4942), done.
7Checking connectivity... done.

转到模型 / 教程 / 图像 / 图像网目录:

1cd models/tutorials/image/imagenet

该目录包含使用 TensorFlow 识别图像的 classify_image.py 文件. 该程序在首次运行时从 `tensorflow.org 下载了一种训练有素的模型. 下载此模型需要您有 200 MB 的可用磁盘空间。

在本示例中,我们将分类 预先提供的 Panda 图像

1python classify_image.py

你会看到类似于此的输出:

1[secondary_label Output]
2giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
3indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
4lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
5custard apple (score = 0.00147)
6earthstar (score = 0.00117)

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

如果您想使用另一个图像,您可以通过将 -- image_file 参数添加到您的 python3 classify_image.py 命令中来做到这一点。

结论

您已经在Python虚拟环境中安装了TensorFlow,并通过运行几个示例验证了TensorFlow的功能,您现在拥有工具,使您能够探索其他主题,包括 Convolutional Neural NetworksWord Embeddings

TensorFlow 的 程序员指南是 TensorFlow 开发的伟大资源和参考文献,您还可以探索 Kaggle,机器学习概念的实际应用的竞争环境,这将使您与其他机器学习、数据科学和统计爱好者对抗。

Published At
Categories with 技术
comments powered by Disqus