|
|
|
@ -5,7 +5,7 @@ import re
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
'fc', 'data', 'cross_entropy', 'conv2d', 'pool2d', 'embedding', 'concat',
|
|
|
|
|
'StaticRNN', 'cast', 'batch_norm'
|
|
|
|
|
'StaticRNN', 'cast', 'sequence_conv', 'sequence_pool'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -165,6 +165,18 @@ _create_op_func_('dropout')
|
|
|
|
|
_create_op_func_('reshape')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cast(x, data_type, program=None):
|
|
|
|
|
helper = LayerHelper('cast', **locals())
|
|
|
|
|
out = helper.create_tmp_variable(dtype=data_type)
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type='cast',
|
|
|
|
|
inputs={'X': [x]},
|
|
|
|
|
outputs={'Out': [out]},
|
|
|
|
|
attrs={'in_data_type': x.data_type,
|
|
|
|
|
'out_data_type': out.data_type})
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cast(x, data_type, program=None):
|
|
|
|
|
helper = LayerHelper('cast', **locals())
|
|
|
|
|
out = helper.create_tmp_variable(dtype=data_type)
|
|
|
|
@ -220,6 +232,46 @@ def square_error_cost(input, label, **kwargs):
|
|
|
|
|
return square_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sequence_conv(input,
|
|
|
|
|
num_filters,
|
|
|
|
|
name=None,
|
|
|
|
|
filter_size=3,
|
|
|
|
|
act=None,
|
|
|
|
|
stride=1,
|
|
|
|
|
padding=None,
|
|
|
|
|
bias_attr=None,
|
|
|
|
|
param_attr=None,
|
|
|
|
|
program=None,
|
|
|
|
|
init_program=None):
|
|
|
|
|
# FIXME(dzh) : want to unify the argument of python layer
|
|
|
|
|
# function. So we ignore some unecessary attributes.
|
|
|
|
|
# such as, padding_trainable, context_start.
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('sequence_conv', **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
|
|
|
|
|
filter_shape = [num_filters, filter_size]
|
|
|
|
|
filter = helper.create_parameter(
|
|
|
|
|
attr=helper.param_attr, shape=filter_shape, dtype=dtype)
|
|
|
|
|
pre_bias = helper.create_tmp_variable(dtype)
|
|
|
|
|
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type='sequence_conv',
|
|
|
|
|
inputs={
|
|
|
|
|
'X': [input],
|
|
|
|
|
'Filter': filter,
|
|
|
|
|
},
|
|
|
|
|
outputs={"Out": pre_bias},
|
|
|
|
|
attrs={
|
|
|
|
|
'context_stride': stride,
|
|
|
|
|
'context_start': 0,
|
|
|
|
|
'context_length': filter_size
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
pre_act = helper.append_bias_op(pre_bias)
|
|
|
|
|
return helper.append_activation(pre_act)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def conv2d(input,
|
|
|
|
|
num_filters,
|
|
|
|
|
name=None,
|
|
|
|
@ -272,6 +324,35 @@ def conv2d(input,
|
|
|
|
|
return helper.append_activation(pre_act)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sequence_pool(input,
|
|
|
|
|
pool_size,
|
|
|
|
|
pool_type,
|
|
|
|
|
pool_stride=1,
|
|
|
|
|
pool_padding=0,
|
|
|
|
|
global_pooling=False,
|
|
|
|
|
program=None,
|
|
|
|
|
init_program=None):
|
|
|
|
|
# FIXME(dzh) : want to unify the argument of python layer
|
|
|
|
|
# function. So we ignore some unecessary attributes
|
|
|
|
|
|
|
|
|
|
ENUM_POOL_TYPE = set(["max", "avg", "sqrt", "last", "first"])
|
|
|
|
|
if pool_type not in ENUM_POOL_TYPE:
|
|
|
|
|
raise ValueError("Unknown pool_type: '%s'. It can only be %s.",
|
|
|
|
|
str(pool_type), " ".join(ENUM_POOL_TYPE))
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('sequence_pool', **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
pool_out = helper.create_tmp_variable(dtype)
|
|
|
|
|
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="sequence_pool",
|
|
|
|
|
inputs={"X": [input]},
|
|
|
|
|
outputs={"Out": pool_out},
|
|
|
|
|
attrs={"strategy": pool_type})
|
|
|
|
|
|
|
|
|
|
return pool_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pool2d(input,
|
|
|
|
|
pool_size,
|
|
|
|
|
pool_type,
|
|
|
|
@ -291,7 +372,7 @@ def pool2d(input,
|
|
|
|
|
if isinstance(pool_padding, int):
|
|
|
|
|
pool_padding = [pool_padding, pool_padding]
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('conv2d', **locals())
|
|
|
|
|
helper = LayerHelper('pool2d', **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
pool_out = helper.create_tmp_variable(dtype)
|
|
|
|
|
|
|
|
|
|