Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

PR for recognize_digits #27

Merged
merged 7 commits into from
Jan 19, 2017
Merged

PR for recognize_digits #27

merged 7 commits into from
Jan 19, 2017

Conversation

dayhaha
Copy link
Contributor

@dayhaha dayhaha commented Dec 25, 2016

major content finished.
resolve #4

@Zrachel Zrachel assigned llxxxll, Zrachel, luotao1 and schinger and unassigned llxxxll and Zrachel Dec 27, 2016
入数据层,加上一个输出softmax层,也就是softmax回归。

####原理介绍:
通过`img = data_layer(name='pixel', size=data_size)`我们得到图片数据,而为了进行计算,该表示方法实际上将28X28的二
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模型概览里面尽量不放代码,如img = data_layer(name='pixel', size=data_size)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

$$ y_i=\begin{cases}1,&\text{如果图像的标签为i}\\0,&\text{如果图像的标签不为i}\end{cases} $$
比如说,如果图片的数字标签为3,则$y_i$组成的向量为[0,0,0,1,0,0,0,0,0,0]。输入层的数据x传到softmax层,在激活操作之
前,会乘以相应的权重,并加上偏置变量b,具体如下:
$$ net_j = \sum_j W_{i,j}x_j + b_i $$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net_i 吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

其中softmax函数的定义如下:
$$ softmax(x_i) = \frac{exp(x_i)}{\sum_j exp(x_j)} $$
以下为softmax回归的网络图:
<center> ![](./image/softmax_regression.png) </center>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此图能和上面的公式对应起来吗?图里面的x0等有角标的符号能否写成公式,如:$x_0$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不太好改,之后可以看能不能用更好的画图软件重新画一个。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图得和公式对应,画的不好看没关系,后面有设计师帮忙画。

这是一个多分类问题,在神经网络中,多分类问题一般用softmax函数来作为输出,因此最简单的神经网络分类器就是一层输
入数据层,加上一个输出softmax层,也就是softmax回归。

####原理介绍:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

公式需要更严谨一些

@luotao1
Copy link
Collaborator

luotao1 commented Dec 29, 2016

recognize_digits/data/raw_data/下的4个ubyte文件都不用上传,请删除。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conv_layer.png太大了,代码修改后请同步文档中的代码。

if "train" in filename:
n = 60000
else:
n = 10000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n =60000 if "train" in filename else 10000

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


o = open("./" + "test.list", "w")
o.write("./data/raw_data/t10k" + "\n")
o.close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件可以不需要。直接写train.list和test.list即可。或者在get_mnist_data.sh里面用echo命令写。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,21 @@
#!/usr/bin/env sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺copyright

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1 @@
./data/raw_data/t10k
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test.list和train.list两个文件是生成的,也不需要放入代码库。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

act=TanhActivation())
# second conv layer
#conv2 = img_conv_layer(input=pool1, filter_size=5, groups=1, num_filters=50, act=TanhActivation(), num_channels=1)
#pool2 = img_pool_layer(input=conv2, pool_size=2, stride=2, num_channels=50)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释掉的代码可以删去

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,34 @@
from paddle.trainer.PyDataProvider2 import *
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺copyright

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if "train" in filename:
n = 60000
else:
n = 10000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n = 60000 if "train" in filename else 10000

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#log=mlp_train.log
config=cnn_mnist.py
output=./cnn_mnist_model
log=cnn_train.log
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config, output, log不用单独列出,注释掉的代码请删去

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dayhaha
Copy link
Contributor Author

dayhaha commented Dec 30, 2016

主要内容已经完成,公式部分可能需要变得更加严谨一些。

Inception模块,以及深度学习为了防止过拟合,还会加入正则,BatchNorm等等方法。


MNIST数据库作为一个简单的计算机视觉数据集,包含一系列如下的手写数字:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 16 连上 line 21的第一句话“其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]”,放到背景介绍第二段(line9处),然后再加上介绍MNIST数据集哪年发布的;是由很多writers一起写的所以有不同patterns;经历了传统图像识别方法和基于神经网络的学习方法热潮,在http://yann.lecun.com/exdb/mnist/中可以看到各个时代的方法和结果;Lecun还基于这份数据集用LeNet5提出可以用于邮政编码的识别(website:http://yann.lecun.com/exdb/lenet/,Paper: Backpropagation applied to handwritten zip code recognition);

我语言组织不好,总之意思就是希望让背景介绍更有趣,读者能了解更多历史。
另外一定要放上源网页的参考:http://yann.lecun.com/exdb/mnist/,并简单解释下网页中的结果。(test error)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络,大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet等。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。

常常被作为机器学习入门的教程。


该数据库的提供者Yann LeCun,他早起在手写字符识别上做了很多研究,在研究过程中
Copy link
Collaborator

@Zrachel Zrachel Jan 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段话和下一段交换位置,变为第三段

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4,5,6行合并成一段。不然显示时,”则“与”有“,”因此“与”常常“,中间都有空格。下同。

本教程作为入门教程,希望给初学者讲述机器学习和Paddle的入门知识。在该教程中,我们会训练一个模型,该模型会扫描给定的图片,然后给出对应的标签。


我们意图并不是展示一个对于此类问题的完美的模型,而是会首先给出一个简单的模型,然后逐步深入的优化模型,让初学者感受复杂的模型是如何带来效
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段话改为:
本章中,我们希望通过简单的模型带大家入门利用神经网络进行图像识别,并逐步深入进行模型优化。

### Softmax回归(Softmax Regression)

####问题说明:
给予MNIST数据,我们希望训练一个分类器f,对于每张给定的图片数据x,预测结果为f(x),应该尽可能准确的接近真实label。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

给予-基于
label - 标签(label)。


####问题说明:
给予MNIST数据,我们希望训练一个分类器f,对于每张给定的图片数据x,预测结果为f(x),应该尽可能准确的接近真实label。
这是一个多分类问题,在神经网络中,多分类问题一般用softmax函数来作为输出,因此最简单的神经网络分类器就是一层输
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"因此"前后没有因果关系。
这句话改为:
神经网络中通常使用softmax函数计算多分类问题中,每一类的概率。为了熟悉softmax函数,我们定义最简单的多分类网络:将输入层经过一个线性映射得到的特征,直接通过softmax函数进行多分类。

后面的函数,都加函数引号, 如这个评注所示。



###数据提供
首先是用Paddle的PyDataProvider2来为模型导入数据,如 `src/mnist_provider.py` 中所示:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有paddle换成PaddlePaddle

'label': integer_value(10)},
cache=CacheType.CACHE_PASS_IN_MEM)
def process(settings, filename): # settings is not used currently.
# following code not shown here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有code都贴过啦吧,不用管篇幅长短

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zrachel 的意思是:config都要贴,其他脚本不用贴

module='mnist_provider',
obj='process')
```
- 首先配置数据,如上给出在训练过程中,训练(train_list)和测试数据集(test_list)的位置,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码说明都统一写在一段代码的前面。


### 多层感知器(Multilayer Perceptron, MLP)
####训练过程
将配置脚本 `./src/train.sh` 一下部分进行修改:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下部分

plot_log('softmax_train.log')
```
<p align="center">
<img src="image/softmax_train_log.png"><br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

给CNN也补上类似的training log图吧,另外这几张图的大小都缩小到原来的1/2~2/3吧。

图1. MNIST图片示例
</p>
其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]
本教程作为入门教程,希望给初学者讲述机器学习和Paddle的入门知识。在该教程中,我们会训练一个模型,该模型会扫描给定的图片,然后给出对应的标签。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paddle->PaddlePaddle

### Softmax回归(Softmax Regression)

####问题说明:
给予MNIST数据,我们希望训练一个分类器f,对于每张给定的图片数据x,预测结果为f(x),应该尽可能准确的接近真实label。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们希望训练一个分类器$f$,对于每张给定的图片数据$x$,预测结果为$f(x)$
尽量用公式表示。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下同

即:
$$ y_i = \frac{e^{net_i}}{\sum_j e^{net_j}} $$
神经网络的训练采用backpropagation的形式,其一般会定义一个损失函数(也称目标函数),训练的目的是为了减小目标函数的
值。在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy)。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全书中第一次介绍softmax,建议可以把其损失函数公式写出来

- ReLU激活函数:
$$ f(x) = max(0, x) $$

### 卷积神经网络(Convolutional Neural Network, CNN)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议增加这一节的公式描述(convolution,pooling)

@dayhaha
Copy link
Contributor Author

dayhaha commented Jan 4, 2017

之前不小心上传了多余的文件,此次提交只是删除多余文件。 根据review的修改在下一次commit会传上来。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 请按照中文教程撰写统一标准,重新组织:数据准备、模型配置说明、训练模型、 应用模型这几章。其中softmax,mlp和cnn的训练命令、评估和预测命令都类似,不用分开介绍。另外,这三个识别准确率的提升,要放在一起强调展示。
  2. 一段话不用分几行写,现在每行后面有空格,造成显示时出现空格。请调整成一行。
  3. 除了config和dataprovider需要全部贴出来外,其他脚本不用贴代码。
  4. src目录取消了,将其中的代码挪出来,并相应修改文档内容。

常常被作为机器学习入门的教程。


该数据库的提供者Yann LeCun,他早起在手写字符识别上做了很多研究,在研究过程中
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4,5,6行合并成一段。不然显示时,”则“与”有“,”因此“与”常常“,中间都有空格。下同。

Inception模块,以及深度学习为了防止过拟合,还会加入正则,BatchNorm等等方法。


MNIST数据库作为一个简单的计算机视觉数据集,包含一系列如下的手写数字:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络,大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet等。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。

图1. MNIST图片示例
</p>
其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]
本教程作为入门教程,希望给初学者讲述机器学习和PaddlePaddle的入门知识。在该教程中,我们会训练一个模型,该模型会扫描给定的图片,然后给出对应的标签。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 21行和22行中间空一行。
  2. ”本教程作为入门教程,希望给初学者讲述机器学习和PaddlePaddle的入门知识“这句话去掉,因为这里不讲机器学习,也不会讲PaddlePaddle的入门知识。
  3. 22行和25行连在一起,重新组织下。

果上提升的。


## 模型概览
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这章小标题改为:

Softmax回归

多层感知器(Multilayer Perceptron, MLP)

卷积神经网络(Convolutional Neural Network, CNN)

常见的激活函数

把问题说明挪到Softmax回归前面,就是模型概览需要一个综述的话。先介绍问题说明,然后说这章从最简单的softmax回归开始介绍、逐步深入。不然这里得看完全部的模型,才知道softmax,mlp,cnn之间的递进关系。另外,把激活函数专门拿出来介绍,不用放在mlp里面。

这是一个多分类问题,在神经网络中,多分类问题一般用softmax函数来作为输出,因此最简单的神经网络分类器就是一层输
入数据层,加上一个输出softmax层,也就是softmax回归。

####原理介绍:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有的原理介绍标题可以去掉了。

图8. softmax回归训练误差图<br/>
</p>

#### 评估过程
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应用模型

评估预训练的模型

预测命令与结果


```
I1218 18:12:41.720213 2464 Util.cpp:154] commandline: /usr/local/bin/../opt/paddle/bin/paddle_trainer --config=mlp_mnist.py --dot_period=10 --log_period=100 -- test_all_data_in_one_period=1 --use_gpu=0 --trainer_count=1 --num_passes=100 --save_dir=./mlp_mnist_model
......
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段日志删掉,换成有打印cost这种的

- -c 指定模型的结构
- -d 指定需要预测的数据源,这里用测试数据集进行预测
- -m 指定模型的参数,这里用之前训练效果最好的模型进行预测
```python
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

脚本不需要贴出来

```

## 总结
从上面的过程中,我们了解到了机器学习的基本知识,softmax回归、多层感知器和卷积神经网络都是最基础的机器学习模型,而复杂的神经网络
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

”机器学习的基本知识“可以去掉,因为没讲。

从上面的过程中,我们了解到了机器学习的基本知识,softmax回归、多层感知器和卷积神经网络都是最基础的机器学习模型,而复杂的神经网络
都是从这些衍生出来的,因此了解了这些基本的东西之后,对于我们之后的学习大有裨益。在这过程中,我们也观察到当我们将模型从简单的
Softmax回归到稍微复杂的卷积神经网络的时候,识别准确率有了大幅度的提升,而这与卷积层的加入是密切联系的,这启发我们在之后遇到新的
模型的时候,要认识到新模型相对于其他模型来说效果提升的关键之处。此外,在体验用PaddlePaddle搭建模型识别MNIS图片的同时
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MNIST少了一个T

@luotao1
Copy link
Collaborator

luotao1 commented Jan 4, 2017

请补充.gitignore文件

@dayhaha
Copy link
Contributor Author

dayhaha commented Jan 4, 2017

我在.gitignore里面把自身忽略了。。 那我改一下,把自身忽略删除吧。

@dayhaha
Copy link
Contributor Author

dayhaha commented Jan 4, 2017

根据以上意见进行了修改 @luotao1 @Zrachel @schinger ,还有以下三个部分待完善。

  1. 问题说明部分。
  2. 参考文献部分。
  3. 公式的补充和规范。

@dayhaha
Copy link
Contributor Author

dayhaha commented Jan 11, 2017

已根据review完成修改,其中 CNN的公式部分感觉内容较多,写少了无法确切的表达含义,就没有列出来,只给出了参考地址。

@dayhaha dayhaha changed the title first PR for recognize_digits PR for recognize_digits Jan 12, 2017
Copy link
Collaborator

@Zrachel Zrachel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About figure result

得到的结果类似如下:

```text
Best pass is 00047, error is 0.473053, which means predict get error as 0.343894
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请check代码看下是否应该是这样的?
Best pass is 00047, training set Avgcost is 0.473053, testing set Avgcost is 0.343894
The classification accuracy on 什么set is 89.49%

因为感觉看图,如果是error不太对啊。。。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩对,这里是应该改一下,确实容易引起歧义, 我把README和相应的代码改一下。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了模型概览和总结那部分还没看,其他的都看了,主要问题:

  1. 三个配置文件请合成一个:原因是三个配置文件的大部分都是一样的,而且以后notebook是用一份py来跑。
  2. 训练模型那章的结构要调整下,具体可以看comments,三个误差图中要加上测试误差的曲线。

<td> linear classifier (1-layer NN) </td>
<td> none </td>
<td> 12.0 </td>
<td> [LeCun et al. 1998] (http://yann.lecun.com/exdb/publis/index.html#lecun-98) </td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

42行改为:[LeCun et al. 1998](http://yann.lecun.com/exdb/publis/index.html#lecun-98) ,就是去掉中间的空格,这样后面的链接不会显示。另外这个表格中都是LeCun et al. 1998,是不是粘贴错了

# 手写字符识别教程

## 背景介绍
当我们学习编程的时候,编写的第一个程序一般是实现打印"Hello World"。而就像编程语言有"Hello World",机器学习则有 [MNIST](http://yann.lecun.com/exdb/mnist/) 数据库。MNIST数据库是手写字符,属于机器学习中典型的图像分类问题,而由于其问题简单,数据集完备,因此常常被作为机器学习入门的教程。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当我们学习编程的时候,编写的第一个程序一般是实现打印"Hello World"。而机器学习(或深度学习)的入门教程,一般都是MNIST 数据库上的手写识别问题。原因是手写识别问题属于典型的图像分类问题,比较简单,同时MNIST数据集也很完备。

<img src="image/mnist_example_image.png"><br/>
图1. MNIST图片示例
</p>
其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]。该数据有60,000条数据的训练集,10,000条数据的测试集。其是更大的数据集NIST的一个子集。MNIST数据集中的数字经过了大小归一化和图片居中的处理。 MNIST数据集从NIST的Special Database 3和Special Database 1构建而来,NIST最初利用SD-3作为训练集,而SD-1作为测试集。然而,SD-3比SD-1更干净,更容易识别,因为SD-3是Census Bureau(人口调查局)的员工进行标注的,而SD-1来自高中生的标注。要训练出合理的模型,需要消除数据集的偏差,因此Yann LeCun等人构建了MNIST数据库,从SD-1和SD-3中各取一半作为MNIST的训练集和测试集,其中训练集来自250位标注员的手写,并且保证了训练集和测试集手写标注员也不相同。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MNIST数据库作为一个简单的计算机视觉数据集,包含一系列如图1所示的手写数字图片和对应的标签。图片是28X28的像素矩阵,而标签则对应着0~9的10个数字。每张图片都经过了大小归一化和居中处理。(这里图1可以找设计师p一下,把标签也弄进去。 @Zrachel

MNIST数据集是从NIST(NIST要有链接)的Special Database 3(SD-3)和Special Database 1(SD-1)构建而来。由于SD-3是由美国人口调查局的员工进行标注,SD-1是由美国高中生进行标注,因此SD-3比SD-1更干净也更容易识别。Yann LeCun等人从SD-1和SD-3中各取一半作为MNIST的训练集(60000条数据)和测试集(10000条数据),其中训练集来自250位不同的标注员,而且训练集和测试集的标注员也完全不相同。

你再顺一下。

## 背景介绍
当我们学习编程的时候,编写的第一个程序一般是实现打印"Hello World"。而就像编程语言有"Hello World",机器学习则有 [MNIST](http://yann.lecun.com/exdb/mnist/) 数据库。MNIST数据库是手写字符,属于机器学习中典型的图像分类问题,而由于其问题简单,数据集完备,因此常常被作为机器学习入门的教程。

MNIST数据库作为一个简单的计算机视觉数据集,包含一系列如下的手写数字:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第6行和11行合并,统一放在图的前面。


MNIST数据库作为一个简单的计算机视觉数据集,包含一系列如下的手写数字:
<p align="center">
<img src="image/mnist_example_image.png"><br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图缩小,<img src="image/flowers.png" width="400" ><br/> ,具体数值你调一下


```bash
python predict.py -c softmax_mnist.py -d ../data/raw_data/ -m softmax_mnist_model/pass-00047
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 526行放在522行前面
  2. ../data/raw_data 应该是data/raw_data吧,现在脚本都在根目录下了。

[[ 1.00000000e+00 1.60381094e-28 1.60381094e-28 1.60381094e-28
1.60381094e-28 1.60381094e-28 1.60381094e-28 1.60381094e-28
1.60381094e-28 1.60381094e-28]]
0 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要修改下脚本,537行打印出来的两个0前面加上“预测的结果和实际的标签”的英文描述。
Predict number: 0
Actual number: 0
534行前面也写一个:各个数字的生成概率 (Generating probability of each digit)
@Zrachel

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意

python predict.py -c softmax_mnist.py -d ../data/raw_data/ -m softmax_mnist_model/pass-00047
```

根据提示,输入需要预测的图片的序号,则分类器能够给出预测的结果和实际的标签。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

根据提示,输入需要预测的图片序号,则分类器能够给出各个数字的生成概率、预测的结果(取最大生成概率对应的数字)和实际的标签。

0 0
```

上面,给出的向量是神经网络输出的十维向量,可以理解为分类为各个数字的概率,实际预测的类即为向量中值最大的一项。如上,代表该分类器接近100%的认为该图片上面的数字为0,而实际标签给出的类也确实如此。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从结果看出,该分类器接近100%地认为第3张图片上面的数字为0,而实际标签给出的类也确实如此。

regularization=L2Regularization(0.0005 * 128))
```

### 模型结构
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把三个配置合成一个,

def softmax()
def mlp()
def lenet5()

data_size = 1 * 28 * 28
label_size = 10
img = data_layer(name='pixel', size=data_size)

predict=softmax(img)
#predict=mlp(img)
#predict=lenet5(img)

if not is_predict:
 +    lbl = data_layer(name="label", size=label_size)
 +    outputs(classification_cost(input=predict, label=lbl))
 +else:
 +    outputs(predict)

这些都是一样的。所以只要把不同的地方写成三个def函数。函数名字可以取的更好一点。
介绍的时候,先整体说一下,然后分别介绍三个def函数。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@luotao1
Copy link
Collaborator

luotao1 commented Jan 13, 2017

README贴出来的代码里面,将现在的英文注释全改成中文注释。源代码里面可以不用改。

@dayhaha
Copy link
Contributor Author

dayhaha commented Jan 13, 2017

卷积过程比较复杂,我看有几个比较好的展示形式,我们要详细展示这样一个过程么? @luotao1 @Zrachel
mxnet
2017-01-13 3 22 14

standford cs231n 动态图
2017-01-13 3 25 12
不过Markdown好像不支持动态图吧?

Copy link
Collaborator

@Zrachel Zrachel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

背景介绍 修改建议

其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]。该数据有60,000条数据的训练集,10,000条数据的测试集。其是更大的数据集NIST的一个子集。MNIST数据集中的数字经过了大小归一化和图片居中的处理。 MNIST数据集从NIST的Special Database 3和Special Database 1构建而来,NIST最初利用SD-3作为训练集,而SD-1作为测试集。然而,SD-3比SD-1更干净,更容易识别,因为SD-3是Census Bureau(人口调查局)的员工进行标注的,而SD-1来自高中生的标注。要训练出合理的模型,需要消除数据集的偏差,因此Yann LeCun等人构建了MNIST数据库,从SD-1和SD-3中各取一半作为MNIST的训练集和测试集,其中训练集来自250位标注员的手写,并且保证了训练集和测试集手写标注员也不相同。


该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络,大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet等。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一处"卷积神经网络"->卷积神经网络(Convolutional Neural Network)

其同时包含每张图片的标签,标签告诉我们图像中包含的数字是什么。例如,上面的图片中对应的标签为[3, 4, 7, 0, 4, 1, 1, 4, 3, 1]。该数据有60,000条数据的训练集,10,000条数据的测试集。其是更大的数据集NIST的一个子集。MNIST数据集中的数字经过了大小归一化和图片居中的处理。 MNIST数据集从NIST的Special Database 3和Special Database 1构建而来,NIST最初利用SD-3作为训练集,而SD-1作为测试集。然而,SD-3比SD-1更干净,更容易识别,因为SD-3是Census Bureau(人口调查局)的员工进行标注的,而SD-1来自高中生的标注。要训练出合理的模型,需要消除数据集的偏差,因此Yann LeCun等人构建了MNIST数据库,从SD-1和SD-3中各取一半作为MNIST的训练集和测试集,其中训练集来自250位标注员的手写,并且保证了训练集和测试集手写标注员也不相同。


该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络,大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet等。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。
->
,人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。

PS:背景介绍不要这么高深


该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络,大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet等。现在的卷积神经网络核心与LeNet类似,但它们在网络结构上要复杂很多,有了更多的组织技巧,如GoogLeNet的Inception模块、以及为了防止过拟合的正则和BatchNorm等等方法。

事实上,许多人对MNIST手写字符的识别做了研究,除了采用不同的分类器之外,人们还考虑用许多预处理的方法(如deskewing(去除歪曲),noise removal(去噪),blurring(模糊)等等)来提高识别的准确率。 Yann LeCunn关于[MNIST数据库] (http://yann.lecun.com/exdb/mnist/) 的网站中介绍多种分类器加上多种预处理方法对于该数据库识别效果的表格。
Copy link
Collaborator

@Zrachel Zrachel Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 16~line 97删掉,改成:

自从该数据集发布以来,有很多算法在MNIST上进行实验。从1998年,LeCun分别用单层线性分类器、多层感知器(multilayer perceptron, MLP)和 多层卷积神经网络LeNet进行实验,使得测试集上的误差不断下降(从???下降到???)。此后,科学家们分别基于K近邻(K-Nearest Neighbors)算法、支持向量机(SVM)、神经网络和Boosting方法在该数据集上做了大量实验(http://yann.lecun.com/exdb/mnist), 并采用多种预处理方法(如deskewing(去除歪曲),noise removal(去噪),blurring(模糊)等等)来提高识别的准确率。

其中括号都用中文括号

@luotao1 你觉得呢?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段修改我觉得可以。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模型概览和总结已全部看完了。



## 总结
从上面的过程中,我们了解到了softmax回归、多层感知器和卷积神经网络都是最基础的机器学习模型,而复杂的神经网络都是从这些衍生出来的,因此了解了这些基本的东西之后,对于我们之后的学习大有裨益。在这过程中,我们也观察到当我们将模型从简单的Softmax回归到稍微复杂的卷积神经网络的时候,识别准确率有了大幅度的提升,而这与卷积层的加入是密切联系的,这启发我们在之后遇到新的模型的时候,要认识到新模型相对于其他模型来说效果提升的关键之处。此外,在体验用PaddlePaddle搭建模型识别MNIST图片的同时我们了解了PaddlePaddle模型搭建的过程,从dataprovider的编写,到网络层的构建,到最后的训练和预测,是用PaddlePaddle实现自定义模型的一个基本流程。对这个流程熟悉以后,我们可以用自己的数据,定义自己的网络模型,来完成自己的训练和预测任务。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本章的softmax回归、多层感知器和卷积神经网络是最基础的深度学习模型,后续章节中复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的Softmax回归到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层(XXX,请补充)。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本章还介绍了PaddlePaddle模型搭建的基本流程,从dataprovider的编写、网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务。

注:图中权重用黑线表示,偏置用红线表示,+1代表偏置参数的系数为1
</p>

神经网络模型的构建包括网络配置在内,通常有以下几个步骤:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

137-145行的第一句话(对于以上步骤4,神经网络的训练采用 backpropagation 的形式,其一般会定义一个损失函数(也称目标函数),训练的目的是为了减小目标函数的值。),全部可以删掉了。 @Zrachel 会放到第一章 新手入门中。


## 模型概览

###问题说明:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

104行改成 ### 分类问题介绍 (后面冒号不要)

## 模型概览

###问题说明:
基于MNIST数据,我们希望训练一个分类器 $f$,对于每张给定的图片数据 $X$ ,预测结果为 $ Y = f(x) $ ,应该尽可能准确的接近真实标签L。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把112行和114行的第一句话拿到这里(语句重新组织)。在分类问题说明这一节,需要说明y和x到底是什么。因为y和x对三个分类器都是一样的,所以后面三个小节都不用再描述了。


在softmax回归中,我们采用了最简单的两层神经网络,分别为输入的datalayer层和输出的 `softmax` 层,模型比较简单,意味着其拟合能力有限。因此,为了达到更好的识别效果,我们可以考虑在输入层和输出层中间加上若干个隐藏层。

在该网络层中,我们有输入X($x_i(i=0,1,2,...,n-1)$),输出标签Y($y_i(i=0,1,2,..9)$),为了表示方便,以下我们都直接用向量计算来表示。经过第一层网络,我们可以得到:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在softmax回归中,我们采用了最简单的两层神经网络,分别为输入的data层和输出的 softmax层。由于这个模型很简单,其拟合能力也有限,因此为了达到更好的识别效果,我们可以考虑在输入层和输出层的中间加上若干个隐藏层。


### 卷积神经网络(Convolutional Neural Network, CNN)

#### 卷积层
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

卷积层等新的更详细描述再看,现在189-193这部分公式看的还是很费劲

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<img src="image/cnn.png"><br/>
图6. 卷积神经网络结构<br/>
</p>
上面的网络是典型的LeNet结构,从中我们可以看出,从输入的二维图像,经过两次卷积->池化,然后再经过全连接,最后的输出层是 `softmax` 分类层。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 为什么这里讲LeNet-5呢,不讲LeNet-1,可以稍微解释下。
  2. “上面的网络是典型的LeNet结构”,为什么呢?这段可以换一种表达:
    LeNet网络的典型结构是:输入的二维图像,首先经过两次卷积层到池化层,再经过全连接层,最后使用softmax分类作为输出层。

</p>
上面的网络是典型的LeNet结构,从中我们可以看出,从输入的二维图像,经过两次卷积->池化,然后再经过全连接,最后的输出层是 `softmax` 分类层。

更详细的关于卷积神经网络的具体知识可以参考 [cs231n] ( http://cs231n.github.io/convolutional-networks/ )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

216放到214行后面。参考cs231n,换一个说法。读者不知道cs231n是什么

<img src="image/Max_pooling.png"><br/>
图5. 池化层图片<br/>
</p>
卷积神经网络另外一个重要的概念就是池化层,这是非线性下采样的一种形式。有许多种非线性函数能够完成下采样的功能,而其中用的最多的就是最大池化。如上所示,其用特定大小不重叠的矩形框将输入层分成不同的区域,然后对于每一个矩形框内的数,取最大值来代表这个区域,从而得到输出层。池化层的主要作用是减少网络的参数,从而能够减小计算量,并且能够控制过拟合。通常情况下在卷积层的后面都会加上一个池化层。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

池化是非线性下采样(这里要简单介绍非线性下采样,或给链接么 @Zrachel )的一种形式,主要作用是通过减少网络的参数来减小计算量,并且能够在一定程度上控制过拟合(原来写的太绝对)。通常在卷积层的后面都会加上一个池化层。

池化包括最大池化、平均池化等。其中最大池化是用不重叠的矩形框将输入层分成不同的区域,对于每一个矩形框内的数取最大值作为输出层,如图5所示。


池化包括最大池化、平均池化等等。

例如,对于图上的矩阵,用二维矩阵X(X的维度为$4\times4$)表示,采用$F=2$的$2\times2$的过滤器(filter)进行最大池化,步长Stride设置为$S=2$,由于$F=S$,该池化为不重叠池化。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

204-206行可以去掉了。那个图很容易能看出最大池化。206行如果写了不重叠池化,还得介绍重叠池化。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

背景介绍、数据准备、参考文献三部分意见。

  1. 参考文献要在文中有引用
  2. 数据准备里的数据格式说明不需要放

## 背景介绍
当我们学习编程的时候,编写的第一个程序一般是实现打印"Hello World"。而机器学习(或深度学习)的入门教程,一般都是 [MNIST](http://yann.lecun.com/exdb/mnist/) 数据库上的手写识别问题。原因是手写识别属于典型的图像分类问题,比较简单,同时MNIST数据集也很完备。

MNIST数据集作为一个简单的计算机视觉数据集,包含一系列如图1所示的手写数字图片和对应的标签。图片是28x28的像素矩阵,而标签则对应着0~9的10个数字。每张图片都经过了大小归一化和居中处理。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. ,标签则对应着0~9的10个数字
  2. 13行和14行合并,一起放到6行的后面,图1放在14行后面。



该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络(Convolutional Neural Network),大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann
LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet、ResNet等(我们将在下一章中介绍这三个模型),人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 17,18行合并。
  2. 17行开始改成:Yann LeCun早先在手写字符识别上做了很多研究
  3. 我们将在下一章中介绍这三个模型-》请参见图像分类教程

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的链接就放图像分类在github上的链接?

该数据库的提供者Yann LeCun,早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络(Convolutional Neural Network),大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann
LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet、ResNet等(我们将在下一章中介绍这三个模型),人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。

自从该数据集发布以来,有很多算法在MNIST上进行实验。1998年,LeCun分别用单层线性分类器、多层感知器(Multilayer Perceptron, MLP)和多层卷积神经网络LeNet进行实验,使得测试集上的误差不断下降(从12%下降到0.7%)。此后,科学家们分别基于K近邻(K-Nearest Neighbors)算法、支持向量机(SVM)、神经网络和Boosting方法在该数据集上做了大量实验,并采用多种预处理方法(如去除歪曲(deskewing),去噪(noise removal),模糊(blurring)等等)来提高识别的准确率。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 第一句改成:有很多算法在MNIST数据集上进行实验。
  2. 科学家们又基于K近邻(K-Nearest Neighbors)算法、支持向量机(SVM)、神经网络和Boosting方法等做了大量实验,并采用多种预处理方法(如去除歪曲、去噪、模糊等)来提高识别的准确率。


### 数据介绍与下载

我们首先下载 [MNIST](http://yann.lecun.com/exdb/mnist/) 数据库,该数据库是手写字符识别常用的数据库。执行以下命令,进行下载并解压缩,并将训练集和测试集的地址分别写入train.list和test.list两个文件,供PaddlePaddle读取。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

144行第一句话不用了,在前文已经知道MNIST是常用数据库了。

执行以下命令,下载MNIST数据库并解压缩,然后将训练集和测试集的地址分别写入train.listtest.list两个文件,供PaddlePaddle读取。

3. Simard, Patrice Y., David Steinkraus, and John C. Platt. "Best practices for convolutional neural networks applied to visual document analysis." ICDAR. Vol. 3. 2003.
4. Rosenblatt F. The perceptron: a probabilistic model for information storage and organization in the brain[J]. Psychological review, 1958, 65(6): 386.
5. Bishop C M. Pattern recognition[J]. Machine Learning, 2006, 128.
6. http://cs231n.github.io/convolutional-networks/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

纯链接的参考文献,比如1/6/7,不用列出来了,在文章中对应的地方加上链接即可。


magic number的第四个字节则是代表储存向量或矩阵的维度:1代表向量,2代表矩阵......

数据是以C array的方式进行存储的,即最后一维的索引变量变化最快。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

159-200行的数据格式说明不需要了 @Zrachel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦。。 好吧。。


数据是以C array的方式进行存储的,即最后一维的索引变量变化最快。

用户可以通过以下脚本随机绘制10张图片
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用户可以通过以下脚本随机绘制10张图片(可参考图1):

labels = np.fromfile(l, 'ubyte', count=n).astype("int") # 以无符号字节为单位一个一个的读取数据

for i in xrange(n):
yield {"pixel": images[i, :], 'label': labels[i]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以在注释里简单说一下magic,n,rows,cols是什么,
# 读取开头的四个参数,分别是XXX、XXX、行数和列数

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dayhaha dayhaha force-pushed the develop branch 8 times, most recently from c12c8d6 to bf75b38 Compare January 18, 2017 08:36
Copy link
Collaborator

@Zrachel Zrachel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before MLP


这是一个分类问题,基于MNIST数据,我们希望训练一个分类器 $f$。输入为MNIST数据库的图片, $28\times28$ 的二维图像,为了进行计算,我们一般将上将 $28\times28$ 的二维图像转化为 $n(n=784)$ 维的向量,因此我们采用$x_i(i=0,1,2,...,n-1)$(向量表示为$X$)来表示输入的图片数据。对于每张给定的图片数据 $X$,我们采用$ y_i (i=0,1,2,..9)$(向量表示为 $Y$来表示预测的输出,预测结果为 $ Y = f(X) $。然后,我们用 $label_i$ (向量表示为$L$)来代表标签,则预测结果 $Y$ 应该尽可能准确的接近真实标签 $L$。

$Y$和$L$具体含义为:比如说,$y_i$组成的向量$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维分别代表图像数字预测为0~9的概率;而此时$label_i$组成的向量$L$可能为[0,1,0,0,0,0,0,0,0,0],其代表标签为1,即输入$X$代表图片的数字为1。则$Y$和$L$尽可能接近的意思是$Y$中概率最大的一维为$L$中对应的标签,并且概率越大则代表越接近。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$Y$和$L$具体含义为:比如说,$y_i$组成的向量$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维分别代表图像数字预测为0~9的概率;
->
$Y$和$L$具体含义为:比如说,预测结果$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维$y_i$分别代表图像数字预测为第$i$类的概率($i\in[0,9]$);


$Y$和$L$具体含义为:比如说,$y_i$组成的向量$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维分别代表图像数字预测为0~9的概率;而此时$label_i$组成的向量$L$可能为[0,1,0,0,0,0,0,0,0,0],其代表标签为1,即输入$X$代表图片的数字为1。则$Y$和$L$尽可能接近的意思是$Y$中概率最大的一维为$L$中对应的标签,并且概率越大则代表越接近。

下面我们一一介绍本章中使用的三个分类器Softmax回归、多层感知器、卷积神经网络。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

三个分类器->三个基本图像分类网络


这是一个分类问题,基于MNIST数据,我们希望训练一个分类器 $f$。输入为MNIST数据库的图片, $28\times28$ 的二维图像,为了进行计算,我们一般将上将 $28\times28$ 的二维图像转化为 $n(n=784)$ 维的向量,因此我们采用$x_i(i=0,1,2,...,n-1)$(向量表示为$X$)来表示输入的图片数据。对于每张给定的图片数据 $X$,我们采用$ y_i (i=0,1,2,..9)$(向量表示为 $Y$来表示预测的输出,预测结果为 $ Y = f(X) $。然后,我们用 $label_i$ (向量表示为$L$)来代表标签,则预测结果 $Y$ 应该尽可能准确的接近真实标签 $L$。

$Y$和$L$具体含义为:比如说,$y_i$组成的向量$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维分别代表图像数字预测为0~9的概率;而此时$label_i$组成的向量$L$可能为[0,1,0,0,0,0,0,0,0,0],其代表标签为1,即输入$X$代表图片的数字为1。则$Y$和$L$尽可能接近的意思是$Y$中概率最大的一维为$L$中对应的标签,并且概率越大则代表越接近。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

而图像的真实标签$L$由$label_i$表示表示,其中只有一维为1,其他为0,如[0,1,0,0,0,0,0,0,0,0],其代表标签为第二类,即标签为0到9中的1。

图2. softmax回归网络结构图<br/>
</p>

神经网络的训练采用 `backpropagation` 的形式,其一般会定义一个损失函数(也称目标函数),训练的目的是为了减小目标函数的值。在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy),其形式如下:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

神经网络一般会定义一个损失函数(也称目标函数),训练的过程是通过改变网络参数来最小化这个损失函数,此过程可以采用backpropagation 算法。在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy),其形式如下:


$$ CE(label, y) = -\sum_i label_ilog(y_i) $$

上面公式为softmax输出层的交叉熵代价损失函数,CE为cross entropy的简称,y是预测每一类的概率,label是标签。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y->$y_i$
label->$label_i$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个的话, 后面不是有一个\sum_i么,CE的参数是两个向量。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只是后面的文字改一下? $y_i$是预测每一类的概率,$label_i$是标签。 公式里面的不用吧?

Copy link
Collaborator

@Zrachel Zrachel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全文的“线性映射”全改成“全连接层”,不要有“softmax层”的概念,变成”以softmax为激活函数的全连接层“


在softmax回归中,我们采用了最简单的两层神经网络,分别为输入的data层和输出的softmax 层。由于这个模型比较简单,其拟合能力有限。因此,为了达到更好的识别效果,我们可以考虑在输入层和输出层中间加上若干个隐藏层。\[[15-16](#参考文献)\]

在该网络层中,我们有输入X($x_i(i=0,1,2,...,n-1)$),输出标签Y($y_i(i=0,1,2,..9)$),为了表示方便,以下我们都直接用向量计算来表示。经过第一层网络,我们可以得到:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X, Y上下文统一,要么都用latex,要么都不用。。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Softmax回归模型采用了最简单的两层神经网络,即只有输入层和输出层,因此其拟合能力有限。为了达到更好的识别效果,我们考虑在输入层和输出层中间加上若干个隐藏层。[15-16]

  1. 经过第一个隐藏层,可以得到 $$ H_1 = \phi(W_1X + b_1) $$,其中$\phi$代表激活函数,常见的有$sigmoid$、$tanh$或$ReLU$等函数。
  2. 经过第二个隐藏层,可以得到 $$ H_2 = \phi(W_2H_1 + b_2) $$。
  3. 最后,再经过输出层,得到的$Y=softmax(W_3H_2 + b_3)$,即为最后的分类结果向量。

图2. softmax回归网络结构图<br/>
</p>

神经网络一般会定义一个损失函数(也称目标函数),训练的过程是通过改变网络参数来最小化这个损失函数,此过程可以采用`backpropagation`算法。在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy),其形式如下:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

神经网络一般会定义一个损失函数(也称目标函数),训练的过程是通过改变网络参数来最小化这个损失函数,此过程可以采用backpropagation算法。

刚看这句第一章有了。。。。还是删了吧

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy),公式如下:

#### 卷积层
<p align="center">
<img src="image/conv_layer.png"><br/>
图4. 卷积层图片<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是用原图吧。。。这图不能动,就和原图没区别了。原图已经重绘过了。描述还是按原图的走。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除模型概览外,其他部分已review完

<p align="center">
<img src="image/mnist_example_image.png" width="400"><br/>
图1. MNIST图片示例
</p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把第4行和第6行合并,中间不要空格,图1放到第6行后面吧。

背景这部分看的太纠结了,辛苦修改!


Yann LeCun早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络(Convolutional Neural Network),大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet、ResNet等(请参见[图像分类](https://github.com/PaddlePaddle/book/tree/develop/image_classification) ),人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。

有很多算法在MNIST上进行实验。1998年,LeCun分别用单层线性分类器、多层感知器(Multilayer Perceptron, MLP)和多层卷积神经网络LeNet进行实验,使得测试集上的误差不断下降(从12%下降到0.7%)。此后,科学家们又基于K近邻(K-Nearest Neighbors)算法、支持向量机(SVM)、神经网络和Boosting方法等做了大量实验,并采用多种预处理方法(如去除歪曲、去噪、模糊等)来提高识别的准确率。\[[2-13](#参考文献)\]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

17行参考文献不要用2-13,显得太笼统了,能否在行内标的更具体一点呢。


本章的softmax回归、多层感知器和卷积神经网络是最基础的深度学习模型,后续章节中复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的softmax回归到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层局部连接和共享权重的特性。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本章还介绍了PaddlePaddle模型搭建的基本流程,从dataprovider的编写、网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务。

## 参考文献
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考文献中,1,14,17,18,19,20都是网站,请不要在参考文献中列出。直接在文中对应位置加网站链接即可。


- tanh激活函数: $ f(x) = tanh(x) = \frac{e^x-e^{-x}}{e^x+e^{-x}} $

实际上,$tanh$ 函数只是规模变化的 $sigmoid$ 函数,将 $sigmoid$函数值放大2倍之后再向下平移1个单位:$ tanh(x) = 2sigmoid(2x) - 1 $
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

124行缩进

  • tanh激活函数: $ f(x) = tanh(x) = \frac{e^x-e^{-x}}{e^x+e^{-x}} $

    实际上,$tanh$ 函数是将 $sigmoid$函数值放大2倍之后再向下平移1个单位:$ tanh(x) = 2sigmoid(2x) - 1 $


- ReLU激活函数: $ f(x) = max(0, x) $

更详细的介绍请参考\[[19](#参考文献)\]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更详细的介绍请参考维基百科激活函数

The classification accuracy is 94.95%
```

从训练日志中我们可以看出,最终训练的准确率为94.95%,相比于softmax回归模型有了显著的提升(这是因为softmax回归模型较为简单,无法拟合更为复杂的数据,而加入了隐藏层之后的多层感知器则具有更强的拟合能力)。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从评估结果可以看到,最终训练的准确率为94.95%,相比于softmax回归模型有了显著的提升。原因是softmax回归模型较为简单,无法拟合更为复杂的数据,而加入了隐藏层之后的多层感知器则具有更强的拟合能力。

The classification accuracy is 99.20%
```

从评估模型的结果可以看到,卷积神经网络的最好分类准确率达到惊人的99.20%。由此可以看到,对于图像问题而言,卷积神经网络能够比一般的全连接网络达到更好的识别效果,而这与卷积层具有局部连接和共享权重的特性是分不开的。同时,从图9中可以看到,卷积神经网络在很早的时候就能达到很好的效果,说明其收敛速度非常快。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从评估结果可以看到,XXX。说明对于图像问题而言,XXX。


```bash
python predict.py -c softmax_mnist.py -d data/raw_data/ -m softmax_mnist_model/pass-00047
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码放在392行前面

python predict.py -c softmax_mnist.py -d data/raw_data/ -m softmax_mnist_model/pass-00047
```

根据提示,输入需要预测的图片序号,则分类器能够给出各个数字的生成概率、预测的结果(取最大生成概率对应的数字)和实际的标签。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

400行去掉“则”


## 总结

本章的softmax回归、多层感知器和卷积神经网络是最基础的深度学习模型,后续章节中复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的softmax回归到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层局部连接和共享权重的特性。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本章还介绍了PaddlePaddle模型搭建的基本流程,从dataprovider的编写、网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 从最简单的softmax回归变换到稍复杂的卷积神经网络时,XXX,原因是卷积层具有XXX。
  2. 本章都换成本教程
  3. 最后加一个“了”,预测任务了。

Copy link
Collaborator

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模型概览部分:

  1. softmax公式的loss公式 @Zrachel 帮忙看下。
  2. 卷积层这段描述不用修改, @qingqing01 在帮忙修改
  3. 卷积的两个特征,也请 @qingqing01@Zrachel 提点意见

其余部分都根据comment进行修改吧。

</p>
池化是非线性下采样的一种形式,主要作用是通过减少网络的参数来减小计算量,并且能够在一定程度上控制过拟合。通常在卷积层的后面会加上一个池化层。

池化包括最大池化、平均池化等。其中最大池化是用不重叠的矩形框将输入层分成不同的区域,对于每个矩形框的数取最大值作为输出层,如图5所示。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 通常在卷积层的后面都会加上一个池化层。
  2. 去掉101行。这两句都很短,中间不用空一行。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要加个都呢? 好像有的卷积层后面没有加池化层?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦,意思是前面一句话不要?。

<img src="image/cnn.png"><br/>
图6. 卷积神经网络结构<br/>
</p>
LeNet-5网络的典型结构是:输入的二维图像,首先经过两次卷积层到池化层,再经过全连接层,最后使用softmax分类作为输出层。\[[20](#参考文献)\]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

109-111行:

LeNet-5是一个最简单的卷积神经网络。图6显示了其结构:输入的二维图像,先经过两次卷积层到池化层,再经过全连接层,最后使用softmax分类作为输出层[20]。卷积的如下三个特性,决定了LeNet-5能比同样使用全连接层的多层感知器更好地识别图像[17]:

#### LeNet-5网络
<p align="center">
<img src="image/cnn.png"><br/>
图6. 卷积神经网络结构<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图6. LeNet-5卷积神经网络结构


相比于全连接层的多层感知器来说,卷积神经网络有以下特性,这三个特性决定了卷积能够更好的识别图像。\[[17](#参考文献)\]

- 神经元的三维特性: 卷积层的神经元在宽度、高度和深度上进行了组织排列。每一层的神经元仅仅与前一层的一块小区域连接,这块小区域被称为感受野。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感受野(加英文)

- 局部连接:CNN通过在相邻层的神经元之间实施局部连接模式来利用空间局部相关性。这样的结构保证了学习后的过滤器能够对于局部的输入特征有最强的响应。堆叠许多这样的层导致非线性“过滤器”变得越来越“全局”。这允许网络首先创建输入的小部分的良好表示,然后从它们组合较大区域的表示。
- 共享权重:在CNN中,每个滤波器在整个视野中重复扫描。 这些复制单元共享相同的参数化(权重向量和偏差)并形成特征图。 这意味着给定卷积层中的所有神经元检测完全相同的特征。 以这种方式的复制单元允许不管它们在视野中的位置都能检测到特征,从而构成平移不变性的性质。

更详细的关于卷积神经网络的具体知识可以参考斯坦福大学公开课 [cs231n]( http://cs231n.github.io/convolutional-networks/ )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更详细的关于卷积神经网络的具体知识可以参考斯坦福大学公开课图像分类教程。


神经网络一般会定义一个损失函数(也称目标函数),训练的过程是通过改变网络参数来最小化这个损失函数,此过程可以采用`backpropagation`算法。在分类问题中,我们一般采用交叉熵代价损失函数(cross entropy),其形式如下:

$$ CE(label, y) = -\sum_i label_ilog(y_i) $$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

51行里面的CE就用cross_entropy(label,y),这样也不用说CE是简称了。


对于有 $N$ 个类别的多分类问题,指定 $N$ 个输出节点,$N$ 维输入特征经过softmax将归一化为 $N$ 个[0,1]范围内的实数值,分别表示该样本属于这 $N$ 个类别的概率。此处的 $y_i$ 即对应该图片为数字 $i$ 的预测概率。

图2为softmax回归的网络图,图中权重用黑线表示,偏置用红线表示,+1代表偏置参数的系数为1。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

43行和图2放在本小节的末尾,因为下面一个小节也是把图放在最后的。

43行的标点小修下:
图2为softmax回归的网络图,图中权重用黑线表示、偏置用红线表示、+1代表偏置参数的系数为1。


### Softmax回归(Softmax Regression)

神经网络中通常使用softmax函数计算多分类问题中每一类的概率。为了熟悉softmax函数,我们定义最简单的多分类网络:将输入层经过一个线性映射得到的特征,直接通过softmax 函数进行多分类。\[[14](#参考文献)\]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最简单的Softmax回归模型是先将XXX(这里不要用线性映射,按照 @Zrachel 提过的意见改),然后直接通过softmax函数进行多分类[14]。


$$ y_i = softmax(\sum_j W_{i,j}x_j + b_i) $$

其中 $ softmax(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}} $
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

公式 @Zrachel 看下:这里用x_i, y_i,下一个小节中的公式都没有下标,直接是X和Y。所以要统一。

公式变了之后,35行和41行也要对应改变。

本章中,我们从简单的模型Softmax回归开始,带大家入门手写字符识别,并逐步进行模型优化。


## 模型概览
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基于MNIST数据训练一个分类器,在介绍本教程使用的三个基本图像分类网络前,我们先给出一些定义:

  • $X$是输入:MNIST图片是$28\times28$ 的二维图像,为了进行计算,我们将其转化为$784$维向量,即$X=\left ( x_0, x_1, \dots, x_783 \right )$。
  • $Y$是输出:MNIST图片的输出是10类数字(0-9),因此$Y=\left ( y_0, y_1, \dots, y_9 \right )$,每一维$y_i$代表图片分类为第$i$类数字的概率。
  • $L$是图片的真实标签:$L=\left ( l_0, l_1, \dots, l_9 \right )$也是10维,但只有一维为1,其他都为0。

@dayhaha dayhaha force-pushed the develop branch 2 times, most recently from 6796d1c to 21cf8bb Compare January 19, 2017 08:15
Yann LeCun早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络(Convolutional Neural Network),大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet、ResNet等(请参见[图像分类](https://github.com/PaddlePaddle/book/tree/develop/image_classification) ),人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。

有很多算法在MNIST上进行实验。1998年,LeCun分别用单层线性分类器、多层感知器(Multilayer Perceptron, MLP)和多层卷积神经网络LeNet进行实验,使得测试集上的误差不断下降(从12%下降到0.7%)。此后,科学家们又基于K近邻(K-Nearest Neighbors)算法、支持向量机(SVM)、神经网络和Boosting方法等做了大量实验,并采用多种预处理方法(如去除歪曲、去噪、模糊等)来提高识别的准确率。\[[2-13](#参考文献)\]
有很多算法在MNIST上进行实验。1998年,LeCun分别用单层线性分类器、多层感知器(Multilayer Perceptron, MLP)和多层卷积神经网络LeNet进行实验,使得测试集上的误差不断下降(从12%下降到0.7%)。\[[1](#参考文献)\]此后,科学家们又基于K近邻(K-Nearest Neighbors)算法\[[2](#参考文献)\]、支持向量机(SVM)\[[3](#参考文献)\]、神经网络\[[4-7](#参考文献)\]和Boosting方法\[[8](#参考文献)\]等做了大量实验,并采用多种预处理方法(如去除歪曲、去噪、模糊等)来提高识别的准确率。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考文献[1]的位置对么,应该放在句号前面。

这是一个分类问题,基于MNIST数据,我们希望训练一个分类器 $f$。输入为MNIST数据库的图片, $28\times28$ 的二维图像,为了进行计算,我们一般将上将 $28\times28$ 的二维图像转化为 $n(n=784)$ 维的向量,因此我们采用$x_i(i=0,1,2,...,n-1)$(向量表示为$X$)来表示输入的图片数据。对于每张给定的图片数据 $X$,我们采用$ y_i (i=0,1,2,..9)$(向量表示为 $Y$来表示预测的输出,预测结果为 $ Y = f(X) $。然后,我们用 $label_i$ (向量表示为$L$)来代表标签,则预测结果 $Y$ 应该尽可能准确的接近真实标签 $L$。
基于MNIST数据训练一个分类器,在介绍本教程使用的三个基本图像分类网络前,我们先给出一些定义:
- $X$是输入:MNIST图片是$28\times28$ 的二维图像,为了进行计算,我们将其转化为$784$维向量,即$X=\left ( x_0, x_1, \dots, x_{783} \right )$。
- $Y$是输出:MNIST图片的输出是10类数字(0-9),因此$Y=\left ( y_0, y_1, \dots, y_9 \right )$,每一维$y_i$代表图片分类为第$i$类数字的概率。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$Y$是输出:分类器的输出是10类数字(0-9),即$Y=\left ( y_0, y_1, \dots, y_9 \right )$,每一维$y_i$代表图片分类为第$i$类数字的概率。

$Y$和$L$具体含义为:比如说,$y_i$组成的向量$Y$为[0.2,0.3,0,1,0,0.1,0.1,0.1,0.1,0],每一维$y_i$分别代表图像数字预测为第$i$类的概率($i\in[0,9]$);而图像的真实标签$L$由$label_i$表示,其中只有一维为1,其他为0,如[0,1,0,0,0,0,0,0,0,0],其代表标签为第二类,即标签为0到9中的1。则$Y$和$L$尽可能接近的意思是$Y$中概率最大的一维为$L$中对应的标签,并且概率越大则代表越接近。

下面我们一一介绍本章中使用的三个基本图像分类网络Softmax回归、多层感知器、卷积神经网络。
下面我们一一介绍本教程中使用的三个基本图像分类网络Softmax回归、多层感知器、卷积神经网络。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

27行去掉,因为22行已经讲了要介绍三个图像分类网络。


<p align="center">
<img src="image/mnist_example_image.png" width="400"><br/>
图1. MNIST图片示例
</p>

MNIST数据集是从 [NIST](https://www.nist.gov/srd/nist-special-database-19) 的Special Database 3(SD-3)和Special Database 1(SD-1)构建而来。由于SD-3是由美国人口调查局的员工进行标注,SD-1是由美国高中生进行标注,因此SD-3比SD-1更干净也更容易识别。Yann LeCun等人从SD-1和SD-3中各取一半作为MNIST的训练集(60000条数据)和测试集(10000条数据),其中训练集来自250位不同的标注员,此外还保证了训练集和测试集的标注员是不完全相同的。

Yann LeCun早先在手写字符识别上做了很多研究,并在研究过程中提出了卷积神经网络(Convolutional Neural Network),大幅度地提高了手写字符的识别能力,也因此成为了深度学习领域的奠基人之一。如今的深度学习领域,卷积神经网络占据了至关重要的地位,从最早Yann LeCun提出的简单LeNet,到如今ImageNet大赛上的优胜模型VGGNet、GoogLeNet、ResNet等(请参见[图像分类](https://github.com/PaddlePaddle/book/tree/develop/image_classification) ),人们在图像分类领域,利用卷积神经网络得到了一系列惊人的结果。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请参见[图像分类]教程,加上教程两字。



### Softmax回归(Softmax Regression)

神经网络中通常使用softmax函数计算多分类问题中每一类的概率。为了熟悉softmax函数,我们定义最简单的多分类网络:将输入层经过一个线性映射得到的特征,直接通过softmax 函数进行多分类。\[[14](#参考文献)\]
最简单的Softmax回归模型是先将输入层经过一个全连接层得到的特征,然后直接通过softmax 函数进行多分类。\[[9](#参考文献)\]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考文献的位置要放在句号前面。


1. 经过第一个隐藏层,可以得到 $ H_1 = \phi(W_1X + b_1) $,其中$\phi$代表激活函数,常见的有$sigmoid$、$tanh$或$ReLU$等函数。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$sigmoid$、$tanh$或$ReLU$-》sigmoid、tanh或ReLU。这里不用公式


- tanh激活函数: $ f(x) = tanh(x) = \frac{e^x-e^{-x}}{e^x+e^{-x}} $

实际上,$tanh$ 函数只是规模变化的 $sigmoid$ 函数,将 $sigmoid$函数值放大2倍之后再向下平移1个单位:$ tanh(x) = 2sigmoid(2x) - 1 $
实际上,$tanh$ 函数只是规模变化的 $sigmoid$ 函数,将 $sigmoid$函数值放大2倍之后再向下平移1个单位:$ tanh(x) = 2sigmoid(2x) - 1 $
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tanh函数只是规模变化的sigmoid函数,将sigmoid函数

去掉公式格式


- ReLU激活函数: $ f(x) = max(0, x) $

更详细的介绍请参考\[[19](#参考文献)\]
更详细的介绍请参考[维基百科激活函数](https://en.wikipedia.org/wiki/Activation_function)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

108行加句号。


```python
def convolutional_neural_network(img):
# first conv layer
# 第一个 卷积-池化 层
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

卷积-池化 前后空格去掉。265行也是。

fc1 = fc_layer(input=conv_pool_2, size=128, act=TanhActivation())
# The softmax layer, note that the hidden size should be 10,
# which is the number of unique digits
# 以softmax为激活函数的全连接输出层,输出层的大小必须为数字的个数10
predict = fc_layer(input=fc1, size=10, act=SoftmaxActivation())
return predict
```

## 训练命令及日志
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 训练模型
### 训练命令及日志
### softmax回归的训练结果
...

这部分的标题#不对。

@luotao1 luotao1 merged commit 2506ff1 into PaddlePaddle:develop Jan 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

识别数字
6 participants