ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

【机器学习入门】#7 使用TensorFlow来识别手写数字 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

【机器学习入门】#7 使用TensorFlow来识别手写数字

Classifying Handwritten Digits with TF.Learn - Machine Learning Recipes #7

约什·戈登: 上期节目里我们使用
JOSH GORDON: Last episode we trained
TensorFlow for Poets来训练图像分类器
in Image Classifier using TensorFlow for Poets,
这一期 我们将使用TF.Learn来自己编写一个
and this time, we’ll write one using TF.Learn.
今天我们要解决的问题是
The problem we’ll start on today is
从MNIST数据集中分辨出手写的数字
classifying handwritten digits from the MNIST dataset,
为这个数据集编写一个简单的分类器
and writing a simple classifier for these is often
通常被认为是机器视觉领域的Hello World
considered the Hello World of computer vision.
MNIST是一个多层分类问题
Now MNIST is a multi-class classification problem.
对给定的一个数字的图片
Given an image of a digit, our job
可以预测出它是什么
will be to predict which one it is.
我为这期节目编写了一个IPython笔记本
I wrote an IPython notebook for this episode,
描述区里有链接
and you can find a link to it in the description.
为了让你配置环境更容易
And to make it easier for you to configure your environment,
我从用Docker安装TensorFlow
I’ll start with a quick screencast of installing
屏幕录像开始讲起
TensorFlow using Docker.
首先 这是我们将要讲的内容的主干
First, here’s an outline of what we’ll cover.
我会给你演示如何
I’ll show you how to download the dataset
下载数据集与可视化的图像
and visualize images.
接下来 我们会训练分类器 对它进行评估
Next, we’ll train a classifier, evaluate it,
然后对新图片进行预测
and use it to make predictions on new images.
然后我们来将分类器的学习和判权过程可视化
Then we’ll visualize the weights the classifier learns
来弄懂它的工作原理
to gain intuition for how it works under the hood.
我们先从安装TensorFlow开始
Let’s start by installing TensorFlow.
Docker的安装指南就在
You can find installation instructions
Getting Started页面里
for Docker linked from the Getting Started page
TensorFlow.org网站上有链接
on TensorFlow.org, and I’ll start this screencast
下面我假设你已经下载和安装好了Docker
assuming you’ve just finished downloading and installing
录屏从安装TensorFlow开始
Docker itself but haven’t started installing TensorFlow.
从全新安装的Docker开始 第一件要做的事
Starting from a fresh install of Docker, the first thing to do
就是打开Docker的快速开始终端
is open the Docker Quickstart terminal.
这个出现时 你会看见鲸鱼下面
And when this appears, you’ll see an IP address just
有个IP地址
below the whale.
拷贝下来
Copy it down.
后面会用上
We’ll need it later.
接下来 我们会启动一个Docker的容器
Next, we’ll launch a Docker container
容器里是TensorFlow的镜像
with a TensorFlow image.
Docker hub上提供了这个镜像
The image is hosted on Docker hub,
描述区有对应的链接
and there’s a link to that in the description.
镜像里包含了TensorFlow及其所有依赖
The image contains TensorFlow with all its dependencies
都配置好了 这是我们将用到的
properly configured, and here’s the command
下载和运行镜像的命令
we’ll use to download and launch the image.
但是首先 我们先选择需要的版本
But first, let’s choose the version we want.
这个页面里列出了所有的版本
The versions are on this page, and we’ll
我们要使用的是最新版
use the latest release.
现在我们可以拷贝粘贴这条命令到终端里
Now we can copy-paste the command into a terminal
在版本号的位置加上一个分号
and add a colon with the version number.
如果你是第一次运行这个镜像
If this is the first time you’ve run the image,
它会被自动下载下来
it’ll be downloaded automatically.
后续的运行里 它会被缓存到本地
And on subsequent runs, it’ll be cached locally.
默认镜像会自动运行
The image starts automatically, and by default, it
它会启动一个笔记本服务
runs a notebook server.
现在剩下需要我们做的就是打开一个浏览器
All that’s left for us to do is to open up a browser
输入刚才我们记录下的IP地址 端口是8888
and point it to the IP we jotted down earlier on port 8888.
现在运行在容器里的IPython笔记本已经搭建好了
And now we have an IPython notebook
我们可以在浏览器中
that we can experiment with in our browser served
做相关实验了
by the container.
本期节目用到的笔记本下载地址在描述区
You can find the notebook for this episode in the description
你可以通过网页来上传运行
and upload it through the UI.
OK
OK.
现在我们看代码
Now onto code.
这是我们用到的引用
Here are the imports we’ll use.
我使用了matplotlib来显示图片 并且 当然
I’ll use matplotlib to display images, and, of course,
我们还要使用TF.Learn来训练分类器
we’ll use TF.Learn to train the classifier.
所有这些都包含在镜像里一并安装上了
All of these are installed with the image.
接下来 我们来下载MNIST数据集
Next, we’ll download the MNIST dataset,
这里有一条命令就可以搞定
and we have a nice one liner for that.
数据集包含了几千条包含标签的
The dataset contains thousands of labeled images
手写数字图像
of handwritten digits.
它已经被分割为包含55000条数据的训练集
It’s pre-divided into train, which is 55,000,
以及10000条的测试数据集
and test, which is 10,000.
我们对它进行可视化展示来增强感性认识
Let’s visualize a few of these to get a feel.
这段代码显示了图像和它对应的标签
This code displays an image along with its label,
能看到我对图像进行了重塑
and you might notice I’m reshaping the image,
待会再解释这个
and I’ll explain why in a bit.
测试数据集的第一张图片是数字7
The first image from the testing set is a seven,
可以看到示例的索引和标签
and you can see the example index as well as the label.
这是第二张图片
Here’s the second image.
这两张手写的都还算清晰
Now both of these are clearly drawn,
但数据集里有一大堆
but there’s a variety of different handwriting
不同的手写示例
samples in this dataset.
这张图片辨认起来就相对困难
Here’s an image that’s harder to recognize.
这些图片的分辨率都不高
These images are low resolution, just 28
灰度的 尺寸是28×28像素
by 28 pixels in grayscale.
同时注意它们被恰当的分割了
Also note they’re properly segmented.
这意味着每张图片只会包含一个数字
That means each image contains exactly one digit.
现在我们来说下会用到的特征
Now let’s talk about the features we’ll use.
当处理图像时
When we’re working with images, we
我们会将其原始像素点作为特征
use the raw pixels as features.
这是因为从图像中抽取出
That’s because extracting useful features
纹理或形状这些特征是困难的
from images, like textures and shapes, is hard.
这里28×28的图像包含784个像素
Now a 28 by 28 image has 784 pixels,
因此我们有了784个特征
so we have 784 features.
这里我们要使用扁平的表达方式
And here, we’re using the flattened representation
来描述图像
of the image.
将图像扁平化的意思是 将二维数组
To flatten an image means to convert it from a 2D array
转化成一维数组 也就是将行拆散排成一排
to a 1D array by unstacking the rows and lining them up.
这就是我们刚刚我们看到的为何要
That’s why we had to reshape this array
重塑数组的原因
to display it earlier.
现在我们来初始化分类器
Now we can initialize the classifier,
这里我们会使用一个线性分类器
and here, we’ll use a linear classifier.
这里需要两个参数
We’ll provide two parameters.
第一个参数指的是我们有多少个类型
The first indicates how many classes we have,
这里是10个 每个数字一种
and there are 10, one for each type of digit.
第二个参数是分类器
The second informs the classifier
会需要用到的特征
about the features we’ll use.
现在我来快速画一下 从高阶展示
Now I’ll draw a quick diagram of a linear classifier
线性分类器的工作原理
to give you a high level preview of how it works under the hood.
你可以认为分类器
You could think of the classifier
就是在计算图像属于某个
as adding up the evidence that the image is
数字分类的证据
each type of digit.
顶部是输入节点 用x表示
The input nodes are on the top, represented by Xes,
输出节点在底部 用y表示
and the output nodes are on the bottom represented by Ys.
每个特征或者图像中的像素就是一个输入节点
We have one input node for each feature or pixel in the image
一个输出节点代表的是
and one output node for each digit
它可能所对应的数字图像
the image could represent.
这里 我们有784个输入和10个输出
Here, we have 784 inputs and 10 outputs.
我在屏幕上画了一些
I’ve just drawn a few of them, so everything
大小够填满屏幕的
fits on the screen.
现在输入和输出已经完全连接上了
Now the inputs and outputs are fully connected,
每条连线都会有个权重
and each of these edges has a weight.
当我们对图像进行分类时
When we classify an image, you can think of each pixel
每个像素都会经历一条个处理流程
as going on a journey.
首先 它会流动到对应的输入节点
First, it flows into its input node,
接下来 它沿着连线移动
and next, it travels along the edges.
它会乘上连线所拥有的权重
Along the way, it’s multiplied by the weight on the edge,
于是输出节点会收集
and the output nodes gather evidence
待分类的每个类型数字所具备的证据
that the image we’re classifying represents each type of digit.
这里 输出节点8收集到越多的证据
The more evidence we gather, say on the eight output,
这个图像就越有可能是数字8
the more likely it is the image is an eight.
要计算我们收集了多少证据
And to calculate how much evidence we have,
就是像素总和乘以
we sum the value of the pixel intensities multiplied
对应的权重
by the weights.
这样我们就可以预测出拥有最多证据的
Then we can predict that the image belongs to the output
就是对应的数字
node with the most evidence.
重点是权重
The important part is the weights,
设置的越合理
and by setting them properly, we can
就越可能获得准确的结果
get accurate classifications.
我们会从随机权重开始 然后逐渐调整到
We begin with random weights, then gradually adjust them
一些更好的值
towards better values.
这一过程在fit方法里实现
And this happens inside the fit method.
完成模型的训练后 我们就可以对它进行评估
Once we have a trained model, we can evaluate it.
使用evaluate方法
Using the evaluate method, we see
我们可以看到它对测试数据集的分类准确度能达到90%
that it correctly classifies about 90% of the test set.
我们也可以对单独的图片进行预测
We can also make predictions on individual images.
这张图片是正确的分类结果
Here’s one that it correctly classifies, and here’s
这是错误的分类结果
one that it gets wrong.
现在我来可视化演示一下分类器
Now I want to show you how to visualize the weights
是如何学习的
the classifier learns.
这里 正权重用红色表示
Here, positive weights are drawn in red,
负权重用蓝色表示
and negative weights are drawn in blue.
这些权重告诉我们什么?
So what do these weights tell us?
要理解这一点 我来展示四张都是1的图片
Well, to understand that, I’ll show four images of ones.
但它们局部不同
They’re all drawn slightly differently,
看下中心点的像素
but take a look at the middle pixel.
注意到没 每张图片上都有
Notice that it’s filled in on every image.
那就是说这个像素填充上
When that pixel is filled in, it’s
就是图像是1的证据
evidence that the image we’re looking at is a one,
因此这个点的权重很高
so we’d expect a high weight on that edge.
现在我们再看四个0
Now let’s take a look at four zeros.
注意 它们的中央点都是空的
Notice that the middle pixel is empty.
尽管绘制0有很多种方式
Although there’s lots of ways to draw zeros,
但如果中央像素是填充的
if that middle pixel is filled in,
就会是0的反证
it’s evidence against the image being a zero,
因此 这个点对0来说是负权重
so we’d expect a negative weight on the edge.
那么我们再来看下图像的权重
And looking at the images of the weights,
可以看出红色几乎绘了
we can almost see outlines of the digits drawn
每一类的描边
in red for each class.
这里可视化的原理是
We were able to visualize these, because we started
我们从784个像素开始 每个点有10个权重等级
with 784 pixels, and we learned 10 weights for each, one
每张图片代表一个数字
for each type of digit.
然后我们将权重值转成一个二维数组
We then reshape the weights into a 2D array.
OK
OK.
今天就到这里
That’s it for now.
当然 这个主题还有很多需要学习的内容
Of course, there’s lots more to learn about this,
我把最喜欢的内容相关链接都放到了描述区
and I put my favorite links in the description.
下次节目里 我们会介绍深度学习的实验
Coming up next time, we’ll experiment with deep learning,
今天这里介绍的内容会有更详细的讲解
and I’ll cover in more detail what we introduced here today.
感谢观看 下次再见
Thanks very much for watching, and I’ll see you then.
(音乐播放中)
[MUSIC PLAYING]

发表评论

译制信息
视频概述
听录译者

收集自网络

翻译译者

知易行难

审核员

【MR】拾月

视频来源

https://www.youtube.com/watch?v=Gj0iyo265bc

相关推荐