Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
f4b5cc83
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
f4b5cc83
编写于
6月 05, 2018
作者:
T
Tao Luo
提交者:
GitHub
6月 05, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #532 from weixing02/fix
Fix 04 & 06
上级
3cac9f3f
97855b58
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
19 addition
and
26 deletion
+19
-26
04.word2vec/README.cn.md
04.word2vec/README.cn.md
+1
-1
04.word2vec/README.md
04.word2vec/README.md
+1
-1
04.word2vec/train.py
04.word2vec/train.py
+1
-1
06.understand_sentiment/README.cn.md
06.understand_sentiment/README.cn.md
+14
-21
06.understand_sentiment/README.md
06.understand_sentiment/README.md
+2
-2
未找到文件。
04.word2vec/README.cn.md
浏览文件 @
f4b5cc83
...
...
@@ -250,7 +250,7 @@ def wordemb(inlayer):
-
定义输入层接受的数据类型以及名字。
```
python
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
3
)
# 初始化PaddlePaddle
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
# 初始化PaddlePaddle
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
dict_size
=
len
(
word_dict
)
# 每个输入层都接受整形数据,这些数据的范围是[0, dict_size)
...
...
04.word2vec/README.md
浏览文件 @
f4b5cc83
...
...
@@ -266,7 +266,7 @@ def wordemb(inlayer):
-
Define name and type for input to data layer.
```
python
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
3
)
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
dict_size
=
len
(
word_dict
)
# Every layer takes integer value of range [0, dict_size)
...
...
04.word2vec/train.py
浏览文件 @
f4b5cc83
...
...
@@ -41,7 +41,7 @@ def load_dict_and_embedding():
def
main
():
paddle
.
init
(
use_gpu
=
with_gpu
,
trainer_count
=
3
)
paddle
.
init
(
use_gpu
=
with_gpu
,
trainer_count
=
1
)
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
dict_size
=
len
(
word_dict
)
# Every layer takes integer value of range [0, dict_size)
...
...
06.understand_sentiment/README.cn.md
浏览文件 @
f4b5cc83
...
...
@@ -129,12 +129,9 @@ def convolution_net(input_dim,
output
=
paddle
.
layer
.
fc
(
input
=
[
conv_3
,
conv_4
],
size
=
class_dim
,
act
=
paddle
.
activation
.
Softmax
())
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
lbl
=
paddle
.
layer
.
data
(
"label"
,
paddle
.
data_type
.
integer_value
(
2
))
cost
=
paddle
.
layer
.
classification_cost
(
input
=
output
,
label
=
lbl
)
return
cost
,
output
```
网络的输入
`input_dim`
表示的是词典的大小,
`class_dim`
表示类别数。这里,我们使用
[
`sequence_conv_pool`
](
https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/trainer_config_helpers/networks.py
)
API实现了卷积和池化操作。
...
...
@@ -202,12 +199,9 @@ def stacked_lstm_net(input_dim,
bias_attr
=
bias_attr
,
param_attr
=
para_attr
)
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
lbl
=
paddle
.
layer
.
data
(
"label"
,
paddle
.
data_type
.
integer_value
(
2
))
cost
=
paddle
.
layer
.
classification_cost
(
input
=
output
,
label
=
lbl
)
return
cost
,
output
```
网络的输入
`stacked_num`
表示的是LSTM的层数,需要是奇数,确保最高层LSTM正向。Paddle里面是通过一个fc和一个lstmemory来实现基于LSTM的循环神经网络。
...
...
@@ -233,10 +227,10 @@ if __name__ == '__main__':
```
python
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
lambda
:
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
batch_size
=
100
)
test_reader
=
paddle
.
batch
(
lambda
:
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
100
)
```
这里,
`dataset.imdb.train()`
和
`dataset.imdb.test()`
分别是
`dataset.imdb`
中的训练数据和测试数据API。
`train_reader`
在训练时使用,意义是将读取的训练数据进行shuffle后,组成一个batch数据。同理,
`test_reader`
是在测试的时候使用,将读取的测试数据组成一个batch。
...
...
@@ -249,9 +243,10 @@ if __name__ == '__main__':
```
python
# 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)
# option 1
[
cost
,
output
]
=
convolution_net
(
dict_dim
,
class_dim
=
class_dim
)
# option 2
# [cost, output] = stacked_lstm_net(dict_dim, class_dim=class_dim, stacked_num=3)
```
该示例中默认使用
`convolution_net`
网络,如果使用
`stacked_lstm_net`
网络,注释相应的行即可。其中cost是网络的优化目标,同时cost包含了整个网络的拓扑信息。
...
...
@@ -350,10 +345,8 @@ Test with Pass 0, {'classification_error_evaluator': 0.11432000249624252}
# 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
)
probs
=
paddle
.
infer
(
output_layer
=
output
,
parameters
=
parameters
,
input
=
input
)
labs
=
np
.
argsort
(
-
probs
)
for
idx
,
lab
in
enumerate
(
labs
):
...
...
06.understand_sentiment/README.md
浏览文件 @
f4b5cc83
...
...
@@ -282,11 +282,11 @@ trainer = paddle.trainer.SGD(cost=cost,
```
python
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
lambda
:
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
paddle
.
dataset
.
imdb
.
train
(
word_dict
),
buf_size
=
1000
),
batch_size
=
100
)
test_reader
=
paddle
.
batch
(
lambda
:
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
100
)
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
100
)
```
`feeding`
is devoted to specifying the correspondence between each yield record and
`paddle.layer.data`
. For instance, the first column of data generated by
`paddle.dataset.imdb.train()`
corresponds to
`word`
feature.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录