Skip to content

Commit 981eccb

Browse files
authored
Merge pull request #1549 from hedaoyuan/sentiment_new_api
Use the sequence_conv_pool define inside the networks.py
2 parents c8ccd4f + 84ce706 commit 981eccb

File tree

1 file changed

+57
-138
lines changed

1 file changed

+57
-138
lines changed

demo/sentiment/train_v2.py

Lines changed: 57 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,40 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import sys
2-
from os.path import join as join_path
316
import paddle.trainer_config_helpers.attrs as attrs
417
from paddle.trainer_config_helpers.poolings import MaxPooling
5-
import paddle.v2.layer as layer
6-
import paddle.v2.activation as activation
7-
import paddle.v2.data_type as data_type
8-
import paddle.v2.dataset.imdb as imdb
918
import paddle.v2 as paddle
1019

1120

12-
def sequence_conv_pool(input,
13-
input_size,
14-
context_len,
15-
hidden_size,
16-
name=None,
17-
context_start=None,
18-
pool_type=None,
19-
context_proj_layer_name=None,
20-
context_proj_param_attr=False,
21-
fc_layer_name=None,
22-
fc_param_attr=None,
23-
fc_bias_attr=None,
24-
fc_act=None,
25-
pool_bias_attr=None,
26-
fc_attr=None,
27-
context_attr=None,
28-
pool_attr=None):
29-
"""
30-
Text convolution pooling layers helper.
31-
32-
Text input => Context Projection => FC Layer => Pooling => Output.
33-
34-
:param name: name of output layer(pooling layer name)
35-
:type name: basestring
36-
:param input: name of input layer
37-
:type input: LayerOutput
38-
:param context_len: context projection length. See
39-
context_projection's document.
40-
:type context_len: int
41-
:param hidden_size: FC Layer size.
42-
:type hidden_size: int
43-
:param context_start: context projection length. See
44-
context_projection's context_start.
45-
:type context_start: int or None
46-
:param pool_type: pooling layer type. See pooling_layer's document.
47-
:type pool_type: BasePoolingType.
48-
:param context_proj_layer_name: context projection layer name.
49-
None if user don't care.
50-
:type context_proj_layer_name: basestring
51-
:param context_proj_param_attr: context projection parameter attribute.
52-
None if user don't care.
53-
:type context_proj_param_attr: ParameterAttribute or None.
54-
:param fc_layer_name: fc layer name. None if user don't care.
55-
:type fc_layer_name: basestring
56-
:param fc_param_attr: fc layer parameter attribute. None if user don't care.
57-
:type fc_param_attr: ParameterAttribute or None
58-
:param fc_bias_attr: fc bias parameter attribute. False if no bias,
59-
None if user don't care.
60-
:type fc_bias_attr: ParameterAttribute or None
61-
:param fc_act: fc layer activation type. None means tanh
62-
:type fc_act: BaseActivation
63-
:param pool_bias_attr: pooling layer bias attr. None if don't care.
64-
False if no bias.
65-
:type pool_bias_attr: ParameterAttribute or None.
66-
:param fc_attr: fc layer extra attribute.
67-
:type fc_attr: ExtraLayerAttribute
68-
:param context_attr: context projection layer extra attribute.
69-
:type context_attr: ExtraLayerAttribute
70-
:param pool_attr: pooling layer extra attribute.
71-
:type pool_attr: ExtraLayerAttribute
72-
:return: output layer name.
73-
:rtype: LayerOutput
74-
"""
75-
# Set Default Value to param
76-
context_proj_layer_name = "%s_conv_proj" % name \
77-
if context_proj_layer_name is None else context_proj_layer_name
78-
79-
with layer.mixed(
80-
name=context_proj_layer_name,
81-
size=input_size * context_len,
82-
act=activation.Linear(),
83-
layer_attr=context_attr) as m:
84-
m += layer.context_projection(
85-
input=input,
86-
context_len=context_len,
87-
context_start=context_start,
88-
padding_attr=context_proj_param_attr)
89-
90-
fc_layer_name = "%s_conv_fc" % name \
91-
if fc_layer_name is None else fc_layer_name
92-
fl = layer.fc(name=fc_layer_name,
93-
input=m,
94-
size=hidden_size,
95-
act=fc_act,
96-
layer_attr=fc_attr,
97-
param_attr=fc_param_attr,
98-
bias_attr=fc_bias_attr)
99-
100-
return layer.pooling(
101-
name=name,
102-
input=fl,
103-
pooling_type=pool_type,
104-
bias_attr=pool_bias_attr,
105-
layer_attr=pool_attr)
106-
107-
10821
def convolution_net(input_dim,
10922
class_dim=2,
11023
emb_dim=128,
11124
hid_dim=128,
11225
is_predict=False):
113-
data = layer.data("word", data_type.integer_value_sequence(input_dim))
114-
emb = layer.embedding(input=data, size=emb_dim)
115-
conv_3 = sequence_conv_pool(
116-
input=emb, input_size=emb_dim, context_len=3, hidden_size=hid_dim)
117-
conv_4 = sequence_conv_pool(
118-
input=emb, input_size=emb_dim, context_len=4, hidden_size=hid_dim)
119-
output = layer.fc(input=[conv_3, conv_4],
120-
size=class_dim,
121-
act=activation.Softmax())
122-
lbl = layer.data("label", data_type.integer_value(2))
123-
cost = layer.classification_cost(input=output, label=lbl)
26+
data = paddle.layer.data("word",
27+
paddle.data_type.integer_value_sequence(input_dim))
28+
emb = paddle.layer.embedding(input=data, size=emb_dim)
29+
conv_3 = paddle.networks.sequence_conv_pool(
30+
input=emb, context_len=3, hidden_size=hid_dim)
31+
conv_4 = paddle.networks.sequence_conv_pool(
32+
input=emb, context_len=4, hidden_size=hid_dim)
33+
output = paddle.layer.fc(input=[conv_3, conv_4],
34+
size=class_dim,
35+
act=paddle.activation.Softmax())
36+
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
37+
cost = paddle.layer.classification_cost(input=output, label=lbl)
12438
return cost
12539

12640

@@ -152,41 +66,45 @@ def stacked_lstm_net(input_dim,
15266
lstm_para_attr = attrs.ParameterAttribute(initial_std=0., learning_rate=1.)
15367
para_attr = [fc_para_attr, lstm_para_attr]
15468
bias_attr = attrs.ParameterAttribute(initial_std=0., l2_rate=0.)
155-
relu = activation.Relu()
156-
linear = activation.Linear()
157-
158-
data = layer.data("word", data_type.integer_value_sequence(input_dim))
159-
emb = layer.embedding(input=data, size=emb_dim)
160-
161-
fc1 = layer.fc(input=emb, size=hid_dim, act=linear, bias_attr=bias_attr)
162-
lstm1 = layer.lstmemory(
69+
relu = paddle.activation.Relu()
70+
linear = paddle.activation.Linear()
71+
72+
data = paddle.layer.data("word",
73+
paddle.data_type.integer_value_sequence(input_dim))
74+
emb = paddle.layer.embedding(input=data, size=emb_dim)
75+
76+
fc1 = paddle.layer.fc(input=emb,
77+
size=hid_dim,
78+
act=linear,
79+
bias_attr=bias_attr)
80+
lstm1 = paddle.layer.lstmemory(
16381
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
16482

16583
inputs = [fc1, lstm1]
16684
for i in range(2, stacked_num + 1):
167-
fc = layer.fc(input=inputs,
168-
size=hid_dim,
169-
act=linear,
170-
param_attr=para_attr,
171-
bias_attr=bias_attr)
172-
lstm = layer.lstmemory(
85+
fc = paddle.layer.fc(input=inputs,
86+
size=hid_dim,
87+
act=linear,
88+
param_attr=para_attr,
89+
bias_attr=bias_attr)
90+
lstm = paddle.layer.lstmemory(
17391
input=fc,
17492
reverse=(i % 2) == 0,
17593
act=relu,
17694
bias_attr=bias_attr,
17795
layer_attr=layer_attr)
17896
inputs = [fc, lstm]
17997

180-
fc_last = layer.pooling(input=inputs[0], pooling_type=MaxPooling())
181-
lstm_last = layer.pooling(input=inputs[1], pooling_type=MaxPooling())
182-
output = layer.fc(input=[fc_last, lstm_last],
183-
size=class_dim,
184-
act=activation.Softmax(),
185-
bias_attr=bias_attr,
186-
param_attr=para_attr)
98+
fc_last = paddle.layer.pooling(input=inputs[0], pooling_type=MaxPooling())
99+
lstm_last = paddle.layer.pooling(input=inputs[1], pooling_type=MaxPooling())
100+
output = paddle.layer.fc(input=[fc_last, lstm_last],
101+
size=class_dim,
102+
act=paddle.activation.Softmax(),
103+
bias_attr=bias_attr,
104+
param_attr=para_attr)
187105

188-
lbl = layer.data("label", data_type.integer_value(2))
189-
cost = layer.classification_cost(input=output, label=lbl)
106+
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
107+
cost = paddle.layer.classification_cost(input=output, label=lbl)
190108
return cost
191109

192110

@@ -196,7 +114,7 @@ def stacked_lstm_net(input_dim,
196114

197115
# network config
198116
print 'load dictionary...'
199-
word_dict = imdb.word_dict()
117+
word_dict = paddle.dataset.imdb.word_dict()
200118
dict_dim = len(word_dict)
201119
class_dim = 2
202120

@@ -226,7 +144,8 @@ def event_handler(event):
226144
if isinstance(event, paddle.event.EndPass):
227145
result = trainer.test(
228146
reader=paddle.reader.batched(
229-
lambda: imdb.test(word_dict), batch_size=128),
147+
lambda: paddle.dataset.imdb.test(word_dict),
148+
batch_size=128),
230149
reader_dict={'word': 0,
231150
'label': 1})
232151
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
@@ -239,7 +158,7 @@ def event_handler(event):
239158
trainer.train(
240159
reader=paddle.reader.batched(
241160
paddle.reader.shuffle(
242-
lambda: imdb.train(word_dict), buf_size=1000),
161+
lambda: paddle.dataset.imdb.train(word_dict), buf_size=1000),
243162
batch_size=100),
244163
event_handler=event_handler,
245164
reader_dict={'word': 0,

0 commit comments

Comments
 (0)