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。该方法将 TensorFlow 安装隔离,并快速启动和运行。一旦完成安装,您将通过运行一个简单的 TensorFlow 程序来验证安装是否成功,然后使用 TensorFlow 来执行图像识别。
在开始本教程之前,您需要准备以下内容:
在此步骤中,我们将创建一个虚拟环境并安装 TensorFlow。
首先,创建一个名为 tf-demo 的项目目录:
- {$78}nbsp;mkdir ~/tf-demo
导航到新创建的 tf-demo 目录下:
- {$75}nbsp;cd ~/tf-demo
然后创建一个名为 tensorflow-dev 的新虚拟环境。运行以下命令创建环境:
- {$72}nbsp;python3 -m venv tensorflow-dev
这将创建一个新的 tensorflow-dev 目录,该目录将包含环境被激活时需要安装的所有包。它还包括 pip 和一个独立版本的 Python。
现在激活你的虚拟环境:
- {$68}nbsp;source tensorflow-dev/bin/activate
一旦激活,你将会在你的终端看到如下内容:
- (tensorflow - dev) username@hostname: ~ / tf - demo $
现在可以在虚拟环境中安装 TensorFlow。
运行以下命令安装和升级到 PyPi 中最新版本的 TensorFlow:
- (tensorflow-dev){$63}nbsp;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.3MB35kB/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
如果你希望随时关闭虚拟环境,使用下面的命令:
- {$58}nbsp;deactivate
如果要重新激活环境,导航至项目目录并运行
.
- source tensorflow - dev / bin / activate
现在,您已经安装了 TensorFlow,让我们确认一下 TensorFlow 是否安装成功。
为了验证 TensorFlow 是否安装成功,我们将在 TensorFlow 中使用非 root 用户权限运行一个的简单程序。我们将使用惯用的初学者示例 "Hello, world!" 作为一种验证形式来验证。我们将使用 Python 的交互式控制台 ( Python's interactive console ) 来创建这个程序,而不是创建一个 Python 文件。
要编写程序,请启动 Python 解释器:
- (tensorflow-dev){$52}nbsp;python
您将会在终端看到如下的提示
>>>
这是 Python 解释器的提示符,这表明它已经准备好让您开始输入一些 Python 语句。
首先,输入这一行代码来导入 TensorFlow 包,并将其作为本地变量 tf。输入代码后按回车键:
- >>> import tensorflow as tf
接下来,添加这行代码来设置信息 "Hello, world!":
- >>> hello = tf.constant("Hello, world!")
然后创建一个新的 TensorFlow 会话并将其分配给变量 sess:
- >>> sess = tf.Session()
注意:根据您的环境,您可能会看到如下输出:
- Output
- 2017-06-1816:22:45.956946: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow 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-1816:22:45.957282: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow 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-1816:22:45.957527: W tensorflow/core/platform/cpu_feature_guard.cc:45]TheTensorFlow 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 吧。
现在已经安装了 TensorFlow,通过运行一个简单的程序来验证一下,让我们看看 TensorFlow 的图像识别功能。
为了给图像分类,你需要训练一个模型。然后您需要编写一些代码来使用该模型。要了解更多有关这些概念的知识,您可以看一看机器学习的介绍 ( An Introduction to Machine Learning )。
TensorFlow 提供了 模型和示例的存储库 ,包括代码和用于对图像进行分类的训练模型。
使用 Git 将 TensorFlow 模型仓库从 GitHub 克隆到您的项目目录中:
- (tensorflow-dev){$24}nbsp;git clone https://github.com/tensorflow/models.git
当 Git 将存储库检出新文件夹 models 时,您将看到以下输出:
- Output Cloninginto'models'...remote: Counting objects: 8785,
- done.remote: Total8785 (delta 0),
- reused 0 (delta 0),
- pack - reused 8785 Receiving objects: 100 % (8785 / 8785),
- 203.16MiB | 24.16MiB / s,
- done.Resolving deltas: 100 % (4942 / 4942),
- done.Checking connectivity...done.
切换到
目录:
- models / tutorials / image / imagenet
- (tensorflow-dev){$18}nbsp;cd models/tutorials/image/imagenet
此目录包含 classify_image.py 文件,它使用 TensorFlow 来识别图像。这个程序在第一次运行时会从 tensorflow.org 下载一个经过训练的模型。下载该模型需要您的磁盘有 200MB 的可用空间。
在本例中,我们将对 预处理好的熊猫图像 进行分类。执行这条命令以运行图像分类器程序:
- (tensorflow-dev){$12}nbsp;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 ,这是一个检验机器学习概念实际应用的竞争平台,您与其他机器学习、数据科学和统计爱好者进行竞赛。他们有一个优秀的 维基主页 ,你可以看到和分享一些解决方案,其中一些是在统计和机器学习技术的前沿思想。
来源: https://juejin.im/entry/5a4dfc376fb9a044fc452923