Skip to content

Commit f4710cf

Browse files
authored
"add sequence conv layer" (#5117)
* "add sequence conv layer" * "add sequence layer" * add networks * "fix based comment" * Update layers.py
1 parent d3cc7ac commit f4710cf

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

python/paddle/v2/framework/layers.py

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
__all__ = [
77
'fc', 'data', 'cross_entropy', 'conv2d', 'pool2d', 'embedding', 'concat',
8-
'StaticRNN', 'cast', 'batch_norm'
8+
'StaticRNN', 'cast', 'sequence_conv', 'sequence_pool'
99
]
1010

1111

@@ -165,6 +165,18 @@ def func(**kwargs):
165165
_create_op_func_('reshape')
166166

167167

168+
def cast(x, data_type, program=None):
169+
helper = LayerHelper('cast', **locals())
170+
out = helper.create_tmp_variable(dtype=data_type)
171+
helper.append_op(
172+
type='cast',
173+
inputs={'X': [x]},
174+
outputs={'Out': [out]},
175+
attrs={'in_data_type': x.data_type,
176+
'out_data_type': out.data_type})
177+
return out
178+
179+
168180
def cast(x, data_type, program=None):
169181
helper = LayerHelper('cast', **locals())
170182
out = helper.create_tmp_variable(dtype=data_type)
@@ -220,6 +232,46 @@ def square_error_cost(input, label, **kwargs):
220232
return square_out
221233

222234

235+
def sequence_conv(input,
236+
num_filters,
237+
name=None,
238+
filter_size=3,
239+
act=None,
240+
stride=1,
241+
padding=None,
242+
bias_attr=None,
243+
param_attr=None,
244+
program=None,
245+
init_program=None):
246+
# FIXME(dzh) : want to unify the argument of python layer
247+
# function. So we ignore some unecessary attributes.
248+
# such as, padding_trainable, context_start.
249+
250+
helper = LayerHelper('sequence_conv', **locals())
251+
dtype = helper.input_dtype()
252+
253+
filter_shape = [num_filters, filter_size]
254+
filter = helper.create_parameter(
255+
attr=helper.param_attr, shape=filter_shape, dtype=dtype)
256+
pre_bias = helper.create_tmp_variable(dtype)
257+
258+
helper.append_op(
259+
type='sequence_conv',
260+
inputs={
261+
'X': [input],
262+
'Filter': filter,
263+
},
264+
outputs={"Out": pre_bias},
265+
attrs={
266+
'context_stride': stride,
267+
'context_start': 0,
268+
'context_length': filter_size
269+
})
270+
271+
pre_act = helper.append_bias_op(pre_bias)
272+
return helper.append_activation(pre_act)
273+
274+
223275
def conv2d(input,
224276
num_filters,
225277
name=None,
@@ -272,6 +324,35 @@ def conv2d(input,
272324
return helper.append_activation(pre_act)
273325

274326

327+
def sequence_pool(input,
328+
pool_size,
329+
pool_type,
330+
pool_stride=1,
331+
pool_padding=0,
332+
global_pooling=False,
333+
program=None,
334+
init_program=None):
335+
# FIXME(dzh) : want to unify the argument of python layer
336+
# function. So we ignore some unecessary attributes
337+
338+
ENUM_POOL_TYPE = set(["max", "avg", "sqrt", "last", "first"])
339+
if pool_type not in ENUM_POOL_TYPE:
340+
raise ValueError("Unknown pool_type: '%s'. It can only be %s.",
341+
str(pool_type), " ".join(ENUM_POOL_TYPE))
342+
343+
helper = LayerHelper('sequence_pool', **locals())
344+
dtype = helper.input_dtype()
345+
pool_out = helper.create_tmp_variable(dtype)
346+
347+
helper.append_op(
348+
type="sequence_pool",
349+
inputs={"X": [input]},
350+
outputs={"Out": pool_out},
351+
attrs={"strategy": pool_type})
352+
353+
return pool_out
354+
355+
275356
def pool2d(input,
276357
pool_size,
277358
pool_type,
@@ -291,7 +372,7 @@ def pool2d(input,
291372
if isinstance(pool_padding, int):
292373
pool_padding = [pool_padding, pool_padding]
293374

294-
helper = LayerHelper('conv2d', **locals())
375+
helper = LayerHelper('pool2d', **locals())
295376
dtype = helper.input_dtype()
296377
pool_out = helper.create_tmp_variable(dtype)
297378

python/paddle/v2/framework/nets.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import paddle.v2.framework.layers as layers
22

3+
__all__ = ["simple_img_conv_pool", "sequence_conv_pool"]
4+
35

46
def simple_img_conv_pool(input,
5-
filter_size,
67
num_filters,
8+
filter_size,
79
pool_size,
810
pool_stride,
911
act,
@@ -94,3 +96,29 @@ def __extend_list__(obj):
9496
program=program,
9597
init_program=init_program)
9698
return pool_out
99+
100+
101+
def sequence_conv_pool(input,
102+
num_filters,
103+
filter_size,
104+
pool_size,
105+
pool_stride,
106+
act,
107+
program=None,
108+
init_program=None):
109+
conv_out = layers.sequence_conv(
110+
input=input,
111+
num_filters=num_filters,
112+
filter_size=filter_size,
113+
act=act,
114+
program=program,
115+
init_program=init_program)
116+
117+
pool_out = layers.sequence_pool(
118+
input=conv_out,
119+
pool_size=pool_size,
120+
pool_type='max',
121+
pool_stride=pool_stride,
122+
program=program,
123+
init_program=init_program)
124+
return pool_out

0 commit comments

Comments
 (0)