README.cn.md 23.0 KB
Newer Older
W
wangxuguang 已提交
1
# 情感分析
L
Luo Tao 已提交
2

L
Luo Tao 已提交
3
本教程源代码目录在[book/understand_sentiment](https://github.com/PaddlePaddle/book/tree/develop/06.understand_sentiment), 初次使用请参考PaddlePaddle[安装教程](https://github.com/PaddlePaddle/book/blob/develop/README.cn.md#运行这本书),更多内容请参考本教程的[视频课堂](http://bit.baidu.com/course/detail/id/177.html)
L
Luo Tao 已提交
4

W
wangxuguang 已提交
5
## 背景介绍
C
choijulie 已提交
6

W
wangxuguang 已提交
7
在自然语言处理中,情感分析一般是指判断一段文本所表达的情绪状态。其中,一段文本可以是一个句子,一个段落或一个文档。情绪状态可以是两类,如(正面,负面),(高兴,悲伤);也可以是三类,如(积极,消极,中性)等等。情感分析的应用场景十分广泛,如把用户在购物网站(亚马逊、天猫、淘宝等)、旅游网站、电影评论网站上发表的评论分成正面评论和负面评论;或为了分析用户对于某一产品的整体使用感受,抓取产品的用户评论并进行情感分析等等。表格1展示了对电影评论进行情感分析的例子:
W
wangxuguang 已提交
8 9 10 11 12

| 电影评论       | 类别  |
| --------     | -----  |
| 在冯小刚这几年的电影里,算最好的一部的了| 正面 |
| 很不好看,好像一个地方台的电视剧     | 负面 |
W
wangxuguang 已提交
13 14
| 圆方镜头全程炫技,色调背景美则美矣,但剧情拖沓,口音不伦不类,一直努力却始终无法入戏| 负面|
|剧情四星。但是圆镜视角加上婺源的风景整个非常有中国写意山水画的感觉,看得实在太舒服了。。|正面|
W
wangxuguang 已提交
15

16
<p align="center">表格 1 电影评论情感分析</p>
W
wangxuguang 已提交
17

W
wangxuguang 已提交
18
在自然语言处理中,情感分析属于典型的**文本分类**问题,即把需要进行情感分析的文本划分为其所属类别。文本分类涉及文本表示和分类方法两个问题。在深度学习的方法出现之前,主流的文本表示方法为词袋模型BOW(bag of words),话题模型等等;分类方法有SVM(support vector machine), LR(logistic regression)等等。  
W
wangxuguang 已提交
19

W
wangxuguang 已提交
20
对于一段文本,BOW表示会忽略其词顺序、语法和句法,将这段文本仅仅看做是一个词集合,因此BOW方法并不能充分表示文本的语义信息。例如,句子“这部电影糟糕透了”和“一个乏味,空洞,没有内涵的作品”在情感分析中具有很高的语义相似度,但是它们的BOW表示的相似度为0。又如,句子“一个空洞,没有内涵的作品”和“一个不空洞而且有内涵的作品”的BOW相似度很高,但实际上它们的意思很不一样。  
W
wangxuguang 已提交
21 22

本章我们所要介绍的深度学习模型克服了BOW表示的上述缺陷,它在考虑词顺序的基础上把文本映射到低维度的语义空间,并且以端对端(end to end)的方式进行文本表示及分类,其性能相对于传统方法有显著的提升\[[1](#参考文献)\]
C
choijulie 已提交
23

W
wangxuguang 已提交
24
## 模型概览
W
wangxuguang 已提交
25
本章所使用的文本表示模型为卷积神经网络(Convolutional Neural Networks)和循环神经网络(Recurrent Neural Networks)及其扩展。下面依次介绍这几个模型。
W
wangxuguang 已提交
26

C
choijulie 已提交
27
### 文本卷积神经网络简介(CNN)
W
wangxuguang 已提交
28

Y
Yu Yang 已提交
29
我们在[推荐系统](https://github.com/PaddlePaddle/book/tree/develop/05.recommender_system)一节介绍过应用于文本数据的卷积神经网络模型的计算过程,这里进行一个简单的回顾。
W
wangxuguang 已提交
30

C
choijulie 已提交
31
对卷积神经网络来说,首先使用卷积处理输入的词向量序列,产生一个特征图(feature map),对特征图采用时间维度上的最大池化(max pooling over time)操作得到此卷积核对应的整句话的特征,最后,将所有卷积核得到的特征拼接起来即为文本的定长向量表示,对于文本分类问题,将其连接至softmax即构建出完整的模型。在实际应用中,我们会使用多个卷积核来处理句子,窗口大小相同的卷积核堆叠起来形成一个矩阵,这样可以更高效的完成运算。另外,我们也可使用窗口大小不同的卷积核来处理句子,[推荐系统](https://github.com/PaddlePaddle/book/tree/develop/05.recommender_system)一节的图3作为示意画了四个卷积核,不同颜色表示不同大小的卷积核操作。
W
wangxuguang 已提交
32 33

对于一般的短文本分类问题,上文所述的简单的文本卷积网络即可达到很高的正确率\[[1](#参考文献)\]。若想得到更抽象更高级的文本特征表示,可以构建深层文本卷积神经网络\[[2](#参考文献),[3](#参考文献)\]
C
choijulie 已提交
34

W
wangxuguang 已提交
35
### 循环神经网络(RNN)
C
choijulie 已提交
36

W
wangxuguang 已提交
37
循环神经网络是一种能对序列数据进行精确建模的有力工具。实际上,循环神经网络的理论计算能力是图灵完备的\[[4](#参考文献)\]。自然语言是一种典型的序列数据(词序列),近年来,循环神经网络及其变体(如long short term memory\[[5](#参考文献)\]等)在自然语言处理的多个领域,如语言模型、句法解析、语义角色标注(或一般的序列标注)、语义表示、图文生成、对话、机器翻译等任务上均表现优异甚至成为目前效果最好的方法。
C
choijulie 已提交
38

39
<p align="center">
T
Tao Luo 已提交
40
<img src="image/rnn.png" width = "60%" align="center"/><br/>
C
choijulie 已提交
41
图1. 循环神经网络按时间展开的示意图
42
</p>
C
choijulie 已提交
43 44

循环神经网络按时间展开后如图1所示:在第$t$时刻,网络读入第$t$个输入$x_t$(向量表示)及前一时刻隐层的状态值$h_{t-1}$(向量表示,$h_0$一般初始化为$0$向量),计算得出本时刻隐层的状态值$h_t$,重复这一步骤直至读完所有输入。如果将循环神经网络所表示的函数记为$f$,则其公式可表示为:
W
wangxuguang 已提交
45

W
wangxuguang 已提交
46
$$h_t=f(x_t,h_{t-1})=\sigma(W_{xh}x_t+W_{hh}h_{h-1}+b_h)$$
W
wangxuguang 已提交
47

W
wangxuguang 已提交
48
其中$W_{xh}$是输入到隐层的矩阵参数,$W_{hh}$是隐层到隐层的矩阵参数,$b_h$为隐层的偏置向量(bias)参数,$\sigma$为$sigmoid$函数。  
C
choijulie 已提交
49

W
wangxuguang 已提交
50 51
在处理自然语言时,一般会先将词(one-hot表示)映射为其词向量(word embedding)表示,然后再作为循环神经网络每一时刻的输入$x_t$。此外,可以根据实际需要的不同在循环神经网络的隐层上连接其它层。如,可以把一个循环神经网络的隐层输出连接至下一个循环神经网络的输入构建深层(deep or stacked)循环神经网络,或者提取最后一个时刻的隐层状态作为句子表示进而使用分类模型等等。  

W
wangxuguang 已提交
52
### 长短期记忆网络(LSTM)
C
choijulie 已提交
53

W
wangxuguang 已提交
54
对于较长的序列数据,循环神经网络的训练过程中容易出现梯度消失或爆炸现象\[[6](#参考文献)\]。为了解决这一问题,Hochreiter S, Schmidhuber J. (1997)提出了LSTM(long short term memory\[[5](#参考文献)\])。  
W
wangxuguang 已提交
55

W
wangxuguang 已提交
56
相比于简单的循环神经网络,LSTM增加了记忆单元$c$、输入门$i$、遗忘门$f$及输出门$o$。这些门及记忆单元组合起来大大提升了循环神经网络处理长序列数据的能力。若将基于LSTM的循环神经网络表示的函数记为$F$,则其公式为:
W
wangxuguang 已提交
57

W
wangxuguang 已提交
58
$$ h_t=F(x_t,h_{t-1})$$
W
wangxuguang 已提交
59

W
wangxuguang 已提交
60
$F$由下列公式组合而成\[[7](#参考文献)\]
W
wangxuguang 已提交
61
\begin{align}
W
wangxuguang 已提交
62 63
i_t & = \sigma(W_{xi}x_t+W_{hi}h_{h-1}+W_{ci}c_{t-1}+b_i)\\\\
f_t & = \sigma(W_{xf}x_t+W_{hf}h_{h-1}+W_{cf}c_{t-1}+b_f)\\\\
W
wangxuguang 已提交
64
c_t & = f_t\odot c_{t-1}+i_t\odot tanh(W_{xc}x_t+W_{hc}h_{h-1}+b_c)\\\\
W
wangxuguang 已提交
65
o_t & = \sigma(W_{xo}x_t+W_{ho}h_{h-1}+W_{co}c_{t}+b_o)\\\\
W
wangxuguang 已提交
66 67
h_t & = o_t\odot tanh(c_t)\\\\
\end{align}
C
choijulie 已提交
68 69
其中,$i_t, f_t, c_t, o_t$分别表示输入门,遗忘门,记忆单元及输出门的向量值,带角标的$W$及$b$为模型参数,$tanh$为双曲正切函数,$\odot$表示逐元素(elementwise)的乘法操作。输入门控制着新输入进入记忆单元$c$的强度,遗忘门控制着记忆单元维持上一时刻值的强度,输出门控制着输出记忆单元的强度。三种门的计算方式类似,但有着完全不同的参数,它们各自以不同的方式控制着记忆单元$c$,如图2所示:

W
wangxuguang 已提交
70
<p align="center">
Y
Yu Yang 已提交
71
<img src="image/lstm.png" width = "65%" align="center"/><br/>
C
choijulie 已提交
72
图2. 时刻$t$的LSTM [7]
W
wangxuguang 已提交
73
</p>
C
choijulie 已提交
74

W
wangxuguang 已提交
75
LSTM通过给简单的循环神经网络增加记忆及控制门的方式,增强了其处理远距离依赖问题的能力。类似原理的改进还有Gated Recurrent Unit (GRU)\[[8](#参考文献)\],其设计更为简洁一些。**这些改进虽然各有不同,但是它们的宏观描述却与简单的循环神经网络一样(如图2所示),即隐状态依据当前输入及前一时刻的隐状态来改变,不断地循环这一过程直至输入处理完毕:**
W
wangxuguang 已提交
76

W
wangxuguang 已提交
77
$$ h_t=Recrurent(x_t,h_{t-1})$$
W
wangxuguang 已提交
78

W
wangxuguang 已提交
79
其中,$Recrurent$可以表示简单的循环神经网络、GRU或LSTM。
C
choijulie 已提交
80

W
wangxuguang 已提交
81
### 栈式双向LSTM(Stacked Bidirectional LSTM)
C
choijulie 已提交
82

W
wangxuguang 已提交
83 84
对于正常顺序的循环神经网络,$h_t$包含了$t$时刻之前的输入信息,也就是上文信息。同样,为了得到下文信息,我们可以使用反方向(将输入逆序处理)的循环神经网络。结合构建深层循环神经网络的方法(深层神经网络往往能得到更抽象和高级的特征表示),我们可以通过构建更加强有力的基于LSTM的栈式双向循环神经网络\[[9](#参考文献)\],来对时序数据进行建模。  

C
choijulie 已提交
85 86
如图3所示(以三层为例),奇数层LSTM正向,偶数层LSTM反向,高一层的LSTM使用低一层LSTM及之前所有层的信息作为输入,对最高层LSTM序列使用时间维度上的最大池化即可得到文本的定长向量表示(这一表示充分融合了文本的上下文信息,并且对文本进行了深层次抽象),最后我们将文本表示连接至softmax构建分类模型。

87
<p align="center">
T
Tao Luo 已提交
88
<img src="image/stacked_lstm.jpg" width=450><br/>
C
choijulie 已提交
89
图3. 栈式双向LSTM用于文本分类
90
</p>
W
wangxuguang 已提交
91

92
## 示例程序
C
choijulie 已提交
93

94
### 数据集介绍
C
choijulie 已提交
95

96 97 98 99 100 101 102 103 104
我们以[IMDB情感分析数据集](http://ai.stanford.edu/%7Eamaas/data/sentiment/)为例进行介绍。IMDB数据集的训练集和测试集分别包含25000个已标注过的电影评论。其中,负面评论的得分小于等于4,正面评论的得分大于等于7,满分10分。
```text
aclImdb
|- test
   |-- neg
   |-- pos
|- train
   |-- neg
   |-- pos
105
```
106
Paddle在`dataset/imdb.py`中提实现了imdb数据集的自动下载和读取,并提供了读取字典、训练数据、测试数据等API。
W
wangxuguang 已提交
107

C
choijulie 已提交
108
```python
109 110
import sys
import paddle.v2 as paddle
111
```
112
## 配置模型
C
choijulie 已提交
113 114 115

在该示例中,我们实现了两种文本分类算法,分别基于[推荐系统](https://github.com/PaddlePaddle/book/tree/develop/05.recommender_system)一节介绍过的文本卷积神经网络,以及[栈式双向LSTM](#栈式双向LSTM(Stacked Bidirectional LSTM))。

116
### 文本卷积神经网络
C
choijulie 已提交
117 118

```python
W
wangxuguang 已提交
119
def convolution_net(input_dim,
120 121
                    class_dim=2,
                    emb_dim=128,
C
choijulie 已提交
122 123
                    hid_dim=128,
                    is_predict=False):
124 125 126 127 128 129 130 131 132 133
    data = paddle.layer.data("word",
                             paddle.data_type.integer_value_sequence(input_dim))
    emb = paddle.layer.embedding(input=data, size=emb_dim)
    conv_3 = paddle.networks.sequence_conv_pool(
        input=emb, context_len=3, hidden_size=hid_dim)
    conv_4 = paddle.networks.sequence_conv_pool(
        input=emb, context_len=4, hidden_size=hid_dim)
    output = paddle.layer.fc(input=[conv_3, conv_4],
                             size=class_dim,
                             act=paddle.activation.Softmax())
C
choijulie 已提交
134 135 136 137 138 139
    if not is_predict:
        lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
        cost = paddle.layer.classification_cost(input=output, label=lbl)
        return cost
    else:
        return output
140 141
```
网络的输入`input_dim`表示的是词典的大小,`class_dim`表示类别数。这里,我们使用[`sequence_conv_pool`](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/trainer_config_helpers/networks.py) API实现了卷积和池化操作。
C
choijulie 已提交
142

143
### 栈式双向LSTM
C
choijulie 已提交
144 145

```python
W
wangxuguang 已提交
146
def stacked_lstm_net(input_dim,
147 148 149
                     class_dim=2,
                     emb_dim=128,
                     hid_dim=512,
C
choijulie 已提交
150 151
                     stacked_num=3,
                     is_predict=False):
152 153 154 155 156 157 158 159 160 161 162 163 164
    """
    A Wrapper for sentiment classification task.
    This network uses bi-directional recurrent network,
    consisting three LSTM layers. This configure is referred to
    the paper as following url, but use fewer layrs.
        http://www.aclweb.org/anthology/P15-1109

    input_dim: here is word dictionary dimension.
    class_dim: number of categories.
    emb_dim: dimension of word embedding.
    hid_dim: dimension of hidden layer.
    stacked_num: number of stacked lstm-hidden layer.
    """
W
wangxuguang 已提交
165 166
    assert stacked_num % 2 == 1

C
choijulie 已提交
167 168
    fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
    lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
169
    para_attr = [fc_para_attr, lstm_para_attr]
C
choijulie 已提交
170
    bias_attr = paddle.attr.Param(initial_std=0., l2_rate=0.)
171 172 173 174 175 176 177 178 179 180 181 182
    relu = paddle.activation.Relu()
    linear = paddle.activation.Linear()

    data = paddle.layer.data("word",
                             paddle.data_type.integer_value_sequence(input_dim))
    emb = paddle.layer.embedding(input=data, size=emb_dim)

    fc1 = paddle.layer.fc(input=emb,
                          size=hid_dim,
                          act=linear,
                          bias_attr=bias_attr)
    lstm1 = paddle.layer.lstmemory(
F
fengjiayi 已提交
183
        input=fc1, act=relu, bias_attr=bias_attr)
W
wangxuguang 已提交
184 185 186

    inputs = [fc1, lstm1]
    for i in range(2, stacked_num + 1):
187 188 189 190 191 192
        fc = paddle.layer.fc(input=inputs,
                             size=hid_dim,
                             act=linear,
                             param_attr=para_attr,
                             bias_attr=bias_attr)
        lstm = paddle.layer.lstmemory(
W
wangxuguang 已提交
193 194 195
            input=fc,
            reverse=(i % 2) == 0,
            act=relu,
F
fengjiayi 已提交
196
            bias_attr=bias_attr)
W
wangxuguang 已提交
197 198
        inputs = [fc, lstm]

C
choijulie 已提交
199 200
    fc_last = paddle.layer.pooling(input=inputs[0], pooling_type=paddle.pooling.Max())
    lstm_last = paddle.layer.pooling(input=inputs[1], pooling_type=paddle.pooling.Max())
201 202 203 204 205 206
    output = paddle.layer.fc(input=[fc_last, lstm_last],
                             size=class_dim,
                             act=paddle.activation.Softmax(),
                             bias_attr=bias_attr,
                             param_attr=para_attr)

C
choijulie 已提交
207 208 209 210 211 212
    if not is_predict:
        lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
        cost = paddle.layer.classification_cost(input=output, label=lbl)
        return cost
    else:
        return output
W
wangxuguang 已提交
213
```
214
网络的输入`stacked_num`表示的是LSTM的层数,需要是奇数,确保最高层LSTM正向。Paddle里面是通过一个fc和一个lstmemory来实现基于LSTM的循环神经网络。
C
choijulie 已提交
215

216
## 训练模型
C
choijulie 已提交
217 218

```python
219 220 221 222 223
if __name__ == '__main__':
    # init
    paddle.init(use_gpu=False)
```
启动paddle程序,use_gpu=False表示用CPU训练,如果系统支持GPU也可以修改成True使用GPU训练。
C
choijulie 已提交
224

225
### 训练数据
C
choijulie 已提交
226

227
使用Paddle提供的数据集`dataset.imdb`中的API来读取训练数据。
C
choijulie 已提交
228
```python
229 230 231 232 233 234
    print 'load dictionary...'
    word_dict = paddle.dataset.imdb.word_dict()
    dict_dim = len(word_dict)
    class_dim = 2
```
加载数据字典,这里通过`word_dict()`API可以直接构造字典。`class_dim`是指样本类别数,该示例中样本只有正负两类。
C
choijulie 已提交
235
```python
H
hedaoyuan 已提交
236
    train_reader = paddle.batch(
237
        paddle.reader.shuffle(
Y
Yang Yu 已提交
238
            paddle.dataset.imdb.train(word_dict), buf_size=1000),
239
        batch_size=100)
H
hedaoyuan 已提交
240
    test_reader = paddle.batch(
Y
Yang Yu 已提交
241
            paddle.dataset.imdb.test(word_dict),
242 243 244
        batch_size=100)
```
这里,`dataset.imdb.train()``dataset.imdb.test()`分别是`dataset.imdb`中的训练数据和测试数据API。`train_reader`在训练时使用,意义是将读取的训练数据进行shuffle后,组成一个batch数据。同理,`test_reader`是在测试的时候使用,将读取的测试数据组成一个batch。
C
choijulie 已提交
245 246
```python
    feeding={'word': 0, 'label': 1}
247
```
C
choijulie 已提交
248 249
`feeding`用来指定`train_reader``test_reader`返回的数据与模型配置中data_layer的对应关系。这里表示reader返回的第0列数据对应`word`层,第1列数据对应`label`层。

250
### 构造模型
C
choijulie 已提交
251 252

```python
253 254 255 256 257 258
    # Please choose the way to build the network
    # by uncommenting the corresponding line.
    cost = convolution_net(dict_dim, class_dim=class_dim)
    # cost = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
```
该示例中默认使用`convolution_net`网络,如果使用`stacked_lstm_net`网络,注释相应的行即可。其中cost是网络的优化目标,同时cost包含了整个网络的拓扑信息。
C
choijulie 已提交
259

260
### 网络参数
C
choijulie 已提交
261 262

```python
263 264 265 266
    # create parameters
    parameters = paddle.parameters.create(cost)
```
根据网络的拓扑构造网络参数。这里parameters是整个网络的参数集。
C
choijulie 已提交
267

268
### 优化算法
C
choijulie 已提交
269 270

```python
271 272 273 274 275 276 277
    # create optimizer
    adam_optimizer = paddle.optimizer.Adam(
        learning_rate=2e-3,
        regularization=paddle.optimizer.L2Regularization(rate=8e-4),
        model_average=paddle.optimizer.ModelAverage(average_window=0.5))
```
Paddle中提供了一系列优化算法的API,这里使用Adam优化算法。
C
choijulie 已提交
278

279
### 训练
C
choijulie 已提交
280 281 282

可以通过`paddle.trainer.SGD`构造一个sgd trainer,并调用`trainer.train`来训练模型。另外,通过给train函数传递一个`event_handler`来获取每个batch和每个pass结束的状态。
```python
283 284 285 286 287 288 289 290 291 292
    # End batch and end pass event handler
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 100 == 0:
                print "\nPass %d, Batch %d, Cost %f, %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics)
            else:
                sys.stdout.write('.')
                sys.stdout.flush()
        if isinstance(event, paddle.event.EndPass):
F
fengjiayi 已提交
293
            with open('./params_pass_%d.tar' % event.pass_id, 'w') as f:
294
                trainer.save_parameter_to_tar(f)
F
fengjiayi 已提交
295

C
choijulie 已提交
296
            result = trainer.test(reader=test_reader, feeding=feeding)
297
            print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
298
```
C
choijulie 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311
比如,构造如下一个`event_handler`可以在每100个batch结束后输出cost和error;在每个pass结束后调用`trainer.test`计算一遍测试集并获得当前模型在测试集上的error。
```python
    from paddle.v2.plot import Ploter

    train_title = "Train cost"
    cost_ploter = Ploter(train_title)
    step = 0
    def event_handler_plot(event):
        global step
        if isinstance(event, paddle.event.EndIteration):
            cost_ploter.append(train_title, step, event.cost)
            cost_ploter.plot()
            step += 1
312
```
C
choijulie 已提交
313 314
或者构造一个`event_handler_plot`画出cost曲线。
```python
315 316 317 318 319 320
    # create trainer
    trainer = paddle.trainer.SGD(cost=cost,
                                 parameters=parameters,
                                 update_equation=adam_optimizer)

    trainer.train(
H
hedaoyuan 已提交
321
        reader=train_reader,
322
        event_handler=event_handler,
C
choijulie 已提交
323
        feeding=feeding,
H
hedaoyuan 已提交
324
        num_passes=2)
325 326
```
程序运行之后的输出如下。
C
choijulie 已提交
327
```text
328 329 330 331 332
Pass 0, Batch 0, Cost 0.693721, {'classification_error_evaluator': 0.5546875}
...................................................................................................
Pass 0, Batch 100, Cost 0.294321, {'classification_error_evaluator': 0.1015625}
...............................................................................................
Test with Pass 0, {'classification_error_evaluator': 0.11432000249624252}
333 334
```

C
choijulie 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
## 应用模型

可以使用训练好的模型对电影评论进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
```python
    import numpy as np

    # Movie Reviews, from imdb test
    reviews = [
        'Read the book, forget the movie!',
        'This is a great movie.'
    ]
    reviews = [c.split() for c in reviews]

    UNK = word_dict['<unk>']
    input = []
    for c in reviews:
        input.append([[word_dict.get(words, UNK) for words in c]])

    # 0 stands for positive sample, 1 stands for negative sample
    label = {0:'pos', 1:'neg'}
    # Use the network used by trainer
    out = convolution_net(dict_dim, class_dim=class_dim, is_predict=True)
    # out = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3, is_predict=True)
    probs = paddle.infer(output_layer=out, parameters=parameters, input=input)

    labs = np.argsort(-probs)
    for idx, lab in enumerate(labs):
        print idx, "predicting probability is", probs[idx], "label is", label[lab[0]]
```


366
## 总结
C
choijulie 已提交
367

W
wangxuguang 已提交
368
本章我们以情感分析为例,介绍了使用深度学习的方法进行端对端的短文本分类,并且使用PaddlePaddle完成了全部相关实验。同时,我们简要介绍了两种文本处理模型:卷积神经网络和循环神经网络。在后续的章节中我们会看到这两种基本的深度学习模型在其它任务上的应用。
C
choijulie 已提交
369 370


W
wangxuguang 已提交
371
## 参考文献
W
wangxuguang 已提交
372 373 374 375 376 377 378 379 380
1. Kim Y. [Convolutional neural networks for sentence classification](http://arxiv.org/pdf/1408.5882)[J]. arXiv preprint arXiv:1408.5882, 2014.
2. Kalchbrenner N, Grefenstette E, Blunsom P. [A convolutional neural network for modelling sentences](http://arxiv.org/pdf/1404.2188.pdf?utm_medium=App.net&utm_source=PourOver)[J]. arXiv preprint arXiv:1404.2188, 2014.
3. Yann N. Dauphin, et al. [Language Modeling with Gated Convolutional Networks](https://arxiv.org/pdf/1612.08083v1.pdf)[J] arXiv preprint arXiv:1612.08083, 2016.
4. Siegelmann H T, Sontag E D. [On the computational power of neural nets](http://research.cs.queensu.ca/home/akl/cisc879/papers/SELECTED_PAPERS_FROM_VARIOUS_SOURCES/05070215382317071.pdf)[C]//Proceedings of the fifth annual workshop on Computational learning theory. ACM, 1992: 440-449.
5. Hochreiter S, Schmidhuber J. [Long short-term memory](http://web.eecs.utk.edu/~itamar/courses/ECE-692/Bobby_paper1.pdf)[J]. Neural computation, 1997, 9(8): 1735-1780.
6. Bengio Y, Simard P, Frasconi P. [Learning long-term dependencies with gradient descent is difficult](http://www-dsi.ing.unifi.it/~paolo/ps/tnn-94-gradient.pdf)[J]. IEEE transactions on neural networks, 1994, 5(2): 157-166.
7. Graves A. [Generating sequences with recurrent neural networks](http://arxiv.org/pdf/1308.0850)[J]. arXiv preprint arXiv:1308.0850, 2013.
8. Cho K, Van Merriënboer B, Gulcehre C, et al. [Learning phrase representations using RNN encoder-decoder for statistical machine translation](http://arxiv.org/pdf/1406.1078)[J]. arXiv preprint arXiv:1406.1078, 2014.
9. Zhou J, Xu W. [End-to-end learning of semantic role labeling using recurrent neural networks](http://www.aclweb.org/anthology/P/P15/P15-1109.pdf)[C]//Proceedings of the Annual Meeting of the Association for Computational Linguistics. 2015.
L
Luo Tao 已提交
381 382

<br/>
L
Luo Tao 已提交
383
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">本教程</span><a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a> 创作,采用 <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 署名-相同方式共享 4.0 国际 许可协议</a>进行许可。