From 06fd61f8b514dcc218831402e3bcdbf8c82ef719 Mon Sep 17 00:00:00 2001 From: jinyuKing <2943829328@qq.com> Date: Mon, 27 Apr 2020 10:11:58 +0000 Subject: [PATCH 1/4] update text.py --- hapi/text/text.py | 98 ++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/hapi/text/text.py b/hapi/text/text.py index 1228d9f..5ff5d1c 100644 --- a/hapi/text/text.py +++ b/hapi/text/text.py @@ -19,7 +19,6 @@ import os import six import sys - if six.PY2: reload(sys) sys.setdefaultencoding('utf8') @@ -50,8 +49,8 @@ 'BeamSearchDecoder', 'MultiHeadAttention', 'FFN', 'TransformerEncoderLayer', 'TransformerEncoder', 'TransformerDecoderLayer', 'TransformerDecoder', 'TransformerBeamSearchDecoder', 'Linear_chain_crf', - 'Crf_decoding', 'SequenceTagging', 'GRUEncoderLayer', 'CNNEncoder', - 'BOWEncoder', 'SimpleConvPoolLayer', 'GRUEncoder', 'DynamicGRU', 'LSTMEncoder' + 'Crf_decoding', 'SequenceTagging', 'GRUEncoderLayer', 'SimCNNEncoder', + 'SimBOWEncoder', 'SimpleConvPoolLayer', 'SimGRUEncoder', 'DynamicGRU', 'SimLSTMEncoder' ] @@ -89,12 +88,12 @@ def get_initial_states(self, batch_ref = flatten(batch_ref)[0] def _is_shape_sequence(seq): - if sys.version_info < (3,): + if sys.version_info < (3, ): integer_types = ( int, - long,) + long, ) else: - integer_types = (int,) + integer_types = (int, ) """For shape, list/tuple of integer is the finest-grained objection""" if (isinstance(seq, list) or isinstance(seq, tuple)): if reduce( @@ -249,8 +248,8 @@ def __init__(self, self.use_customized_weight = False for _weights in [ - forget_gate_weights, input_gate_weights, output_gate_weights, - cell_weights + forget_gate_weights, input_gate_weights, output_gate_weights, + cell_weights ]: for _key in _weights: if _weights[_key] is not None: @@ -275,7 +274,7 @@ def __init__(self, is_bias=True) else: if "w" in forget_gate_weights and forget_gate_weights[ - "w"] is not None: + "w"] is not None: self.fg_w = forget_gate_weights["w"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -289,7 +288,7 @@ def __init__(self, dtype=self._dtype) if "h" in forget_gate_weights and forget_gate_weights[ - "h"] is not None: + "h"] is not None: self.fg_h = forget_gate_weights["h"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -303,7 +302,7 @@ def __init__(self, dtype=self._dtype) if "b" in forget_gate_weights and forget_gate_weights[ - "b"] is not None: + "b"] is not None: self.fg_b = forget_gate_weights["b"] else: if self._bias_attr is not None and self._bias_attr.name is not None: @@ -318,7 +317,7 @@ def __init__(self, is_bias=True) if "w" in input_gate_weights and input_gate_weights[ - "w"] is not None: + "w"] is not None: self.ig_w = input_gate_weights["w"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -333,7 +332,7 @@ def __init__(self, dtype=self._dtype) if "h" in input_gate_weights and input_gate_weights[ - "h"] is not None: + "h"] is not None: self.ig_h = input_gate_weights["h"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -348,7 +347,7 @@ def __init__(self, dtype=self._dtype) if "b" in input_gate_weights and input_gate_weights[ - "b"] is not None: + "b"] is not None: self.ig_b = input_gate_weights["b"] else: if self._bias_attr is not None and self._bias_attr.name is not None: @@ -363,7 +362,7 @@ def __init__(self, is_bias=True) if "w" in output_gate_weights and output_gate_weights[ - "w"] is not None: + "w"] is not None: self.og_w = output_gate_weights["w"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -377,7 +376,7 @@ def __init__(self, dtype=self._dtype) if "h" in output_gate_weights and output_gate_weights[ - "h"] is not None: + "h"] is not None: self.og_h = output_gate_weights["h"] else: if self._param_attr is not None and self._param_attr.name is not None: @@ -392,7 +391,7 @@ def __init__(self, dtype=self._dtype) if "b" in output_gate_weights and output_gate_weights[ - "b"] is not None: + "b"] is not None: self.og_b = output_gate_weights["b"] else: if self._bias_attr is not None and self._bias_attr.name is not None: @@ -547,7 +546,7 @@ def __init__(self, self.use_customized_weight = False for _weights in [ - update_gate_weights, reset_gate_weights, cell_weights + update_gate_weights, reset_gate_weights, cell_weights ]: for _key in _weights: if _weights[_key] is not None: @@ -603,7 +602,7 @@ def __init__(self, # create the parameters of gates in gru if "w" in update_gate_weights and update_gate_weights[ - "w"] is not None: + "w"] is not None: self.ug_w = update_gate_weights["w"] else: if gate_param_attr is not None and gate_param_attr.name is not None: @@ -617,7 +616,7 @@ def __init__(self, dtype=self._dtype) if "h" in update_gate_weights and update_gate_weights[ - "h"] is not None: + "h"] is not None: self.ug_h = update_gate_weights["h"] else: if gate_param_attr is not None and gate_param_attr.name is not None: @@ -631,7 +630,7 @@ def __init__(self, dtype=self._dtype) if "b" in update_gate_weights and update_gate_weights[ - "b"] is not None: + "b"] is not None: self.ug_b = update_gate_weights["b"] else: if gate_bias_attr is not None and gate_bias_attr.name is not None: @@ -647,7 +646,7 @@ def __init__(self, # reset gate parameters if "w" in reset_gate_weights and reset_gate_weights[ - "w"] is not None: + "w"] is not None: self.rg_w = reset_gate_weights["w"] else: if gate_param_attr is not None and gate_param_attr.name is not None: @@ -661,7 +660,7 @@ def __init__(self, dtype=self._dtype) if "h" in reset_gate_weights and reset_gate_weights[ - "h"] is not None: + "h"] is not None: self.rg_h = reset_gate_weights["h"] else: if gate_param_attr is not None and gate_param_attr.name is not None: @@ -675,7 +674,7 @@ def __init__(self, dtype=self._dtype) if "b" in reset_gate_weights and reset_gate_weights[ - "b"] is not None: + "b"] is not None: self.rg_b = reused_params["b"] else: if gate_bias_attr is not None and gate_bias_attr.name is not None: @@ -803,7 +802,7 @@ def _maybe_copy(state, new_state, step_mask): new_state = fluid.layers.elementwise_mul( new_state, step_mask, axis=0) - fluid.layers.elementwise_mul( - state, (step_mask - 1), axis=0) + state, (step_mask - 1), axis=0) return new_state flat_inputs = flatten(inputs) @@ -849,8 +848,8 @@ def _maybe_copy(state, new_state, step_mask): outputs = map_structure( lambda x: ArrayWrapper(x), step_outputs) if i == 0 else map_structure( - lambda x, x_array: x_array.append(x), step_outputs, - outputs) + lambda x, x_array: x_array.append(x), step_outputs, + outputs) final_outputs = map_structure( lambda x: fluid.layers.stack(x.array, @@ -919,7 +918,7 @@ def _maybe_copy(state, new_state, step_mask): step_mask.stop_gradient = True new_state = layers.elementwise_mul( state, step_mask, axis=0) - layers.elementwise_mul( - new_state, (step_mask - 1), axis=0) + new_state, (step_mask - 1), axis=0) if convert_dtype(state_dtype) in ["bool"]: new_state = layers.cast(new_state, dtype=state_dtype) return new_state @@ -961,8 +960,8 @@ def _maybe_copy(state, new_state, step_mask): outputs = map_structure( lambda x: ArrayWrapper(x), step_outputs) if step_idx == 0 else map_structure( - lambda x, x_array: x_array.append(x), step_outputs, - outputs) + lambda x, x_array: x_array.append(x), step_outputs, + outputs) inputs, states, finished, sequence_lengths = ( next_inputs, next_states, next_finished, next_sequence_lengths) @@ -991,7 +990,7 @@ def _maybe_copy(state, new_state, step_mask): return (final_outputs, final_states, sequence_lengths) if self.return_length else ( - final_outputs, final_states) + final_outputs, final_states) else: return fluid.layers.dynamic_decode( self.decoder, @@ -1042,7 +1041,7 @@ def _merge_batch_beams_with_var_dim(self, x): x = layers.reshape( x, [0] * (len(x.shape) - var_dim_in_state ) + [self.batch_size * self.beam_size] + - [int(size) for size in x.shape[-var_dim_in_state + 2:]]) + [int(size) for size in x.shape[-var_dim_in_state + 2:]]) x = layers.transpose( x, list(range((len(x.shape) + 1 - var_dim_in_state), len(x.shape))) + @@ -1053,9 +1052,9 @@ def _split_batch_beams_with_var_dim(self, x): var_dim_size = layers.shape(x)[self.var_dim_in_state] x = layers.reshape( x, [-1, self.beam_size] + - [int(size) - for size in x.shape[1:self.var_dim_in_state]] + [var_dim_size] + - [int(size) for size in x.shape[self.var_dim_in_state + 1:]]) + [int(size) + for size in x.shape[1:self.var_dim_in_state]] + [var_dim_size] + + [int(size) for size in x.shape[self.var_dim_in_state + 1:]]) return x def step(self, time, inputs, states, **kwargs): @@ -1118,7 +1117,7 @@ def __init__(self, elif cmd == "d": # add dropout self.functors.append(lambda x: layers.dropout( x, dropout_prob=dropout_rate, is_test=False) - if dropout_rate else x) + if dropout_rate else x) def forward(self, x, residual=None): for i, cmd in enumerate(self.process_cmd): @@ -1219,7 +1218,7 @@ def forward(self, queries, keys, values, attn_bias, cache=None): # scale dot product attention product = layers.matmul( - x=q, y=k, transpose_y=True, alpha=self.d_model ** -0.5) + x=q, y=k, transpose_y=True, alpha=self.d_model**-0.5) if attn_bias: product += attn_bias weights = layers.softmax(product) @@ -1309,6 +1308,7 @@ def __init__(self, reused_ffn_weights={"reused_fc1": None, "reused_fc2": None}, reused_post_ffn_layernorm=None): + super(TransformerEncoderLayer, self).__init__() self.preprocesser1 = PrePostProcessLayer(preprocess_cmd, d_model, @@ -1556,7 +1556,7 @@ def prepare_static_cache(self, enc_output): ] -# TODO: we should merge GRUCell with BasicGRUCell +#TODO: we should merge GRUCell with BasicGRUCell class GRUCell(RNNCell): def __init__(self, input_size, @@ -1590,7 +1590,7 @@ def state_shape(self): return [self.hidden_size] -# TODO: we should merge GRUCell with BasicGRUCell +#TODO: we should merge GRUCell with BasicGRUCell class GRUEncoderCell(RNNCell): def __init__(self, num_layers, @@ -1606,7 +1606,7 @@ def __init__(self, self.gru_cells.append( self.add_sublayer( "gru_%d" % i, - # BasicGRUCell( + #BasicGRUCell( GRUCell( input_size=input_size if i == 0 else hidden_size, hidden_size=hidden_size, @@ -1673,6 +1673,7 @@ def weight(self, value): self._transition = value def forward(self, input, label, length=None): + alpha = self._helper.create_variable_for_type_inference( dtype=self._dtype) emission_exps = self._helper.create_variable_for_type_inference( @@ -1723,6 +1724,7 @@ def weight(self, value): self._transition = value def forward(self, input, label=None, length=None): + viterbi_path = self._helper.create_variable_for_type_inference( dtype=self._dtype) this_inputs = { @@ -1919,7 +1921,7 @@ def forward(self, input): return x -class CNNEncoder(Layer): +class SimCNNEncoder(Layer): """ simple CNNEncoder for simnet """ @@ -1933,7 +1935,7 @@ def __init__(self, padding_idx, act ): - super(CNNEncoder, self).__init__() + super(SimCNNEncoder, self).__init__() self.dict_size = dict_size self.emb_dim = emb_dim self.filter_size = filter_size @@ -1962,7 +1964,7 @@ def forward(self, input): emb_out=self.cnn_layer(emb_reshape) return emb_out -class BOWEncoder(Layer): +class SimBOWEncoder(Layer): """ simple BOWEncoder for simnet """ @@ -1973,7 +1975,7 @@ def __init__(self, seq_len, padding_idx ): - super(BOWEncoder, self).__init__() + super(SimBOWEncoder, self).__init__() self.dict_size = dict_size self.bow_dim = bow_dim self.seq_len = seq_len @@ -2034,7 +2036,7 @@ def forward(self, inputs): res = fluid.layers.concat(res, axis=1) return res -class GRUEncoder(Layer): +class SimGRUEncoder(Layer): """ simple GRUEncoder for simnet """ @@ -2046,7 +2048,7 @@ def __init__(self, padding_idx, seq_len ): - super(GRUEncoder, self).__init__() + super(SimGRUEncoder, self).__init__() self.dict_size = dict_size self.emb_dim = emb_dim self.gru_dim = gru_dim @@ -2071,7 +2073,7 @@ def forward(self, input): gru = fluid.layers.tanh(gru) return gru -class LSTMEncoder(Layer): +class SimLSTMEncoder(Layer): """ simple LSTMEncoder for simnet """ @@ -2087,7 +2089,7 @@ def __init__(self, """ initialize """ - super(LSTMEncoder, self).__init__() + super(SimLSTMEncoder, self).__init__() self.dict_size = dict_size self.emb_dim = emb_dim self.lstm_dim = lstm_dim From 75dcc1612da18040d4df65cf36d9a4db222a1e6b Mon Sep 17 00:00:00 2001 From: jinyuKing <2943829328@qq.com> Date: Wed, 6 May 2020 09:58:16 +0000 Subject: [PATCH 2/4] update text.py --- hapi/text/text.py | 370 ++++++++++++++++++++++------------------------ 1 file changed, 173 insertions(+), 197 deletions(-) diff --git a/hapi/text/text.py b/hapi/text/text.py index 5ff5d1c..9fec48c 100644 --- a/hapi/text/text.py +++ b/hapi/text/text.py @@ -37,7 +37,7 @@ import paddle.fluid as fluid import paddle.fluid.layers.utils as utils from paddle.fluid.layers.utils import map_structure, flatten, pack_sequence_as -from paddle.fluid.dygraph import to_variable, Embedding, Linear, LayerNorm, GRUUnit, Conv2D +from paddle.fluid.dygraph import to_variable, Embedding, Linear, LayerNorm, GRUUnit, Conv2D, Pool2D from paddle.fluid.data_feeder import convert_dtype from paddle.fluid import layers @@ -49,8 +49,8 @@ 'BeamSearchDecoder', 'MultiHeadAttention', 'FFN', 'TransformerEncoderLayer', 'TransformerEncoder', 'TransformerDecoderLayer', 'TransformerDecoder', 'TransformerBeamSearchDecoder', 'Linear_chain_crf', - 'Crf_decoding', 'SequenceTagging', 'GRUEncoderLayer', 'SimCNNEncoder', - 'SimBOWEncoder', 'SimpleConvPoolLayer', 'SimGRUEncoder', 'DynamicGRU', 'SimLSTMEncoder' + 'Crf_decoding', 'SequenceTagging', 'GRUEncoderLayer', 'Conv1dPoolLayer', + 'CNNEncoder' ] @@ -1898,226 +1898,202 @@ def forward(self, word, lengths, target=None): crf_decode = self.crf_decoding(input=emission, length=lengths) return crf_decode, lengths -class SimpleConvPoolLayer(Layer): +class Conv1dPoolLayer(Layer): + """ + This interface is used to construct a callable object of the ``Conv1DPoolLayer`` class.The ``Conv1DPoolLayer`` is composed of a ``Conv2D`` and a ``Pool2D`` . + For more details, refer to code examples.The ``Conv1DPoolLayer`` layer calculates the output based on the input, filter and strides, paddings, dilations, + groups,global_pooling, pool_type,ceil_mode,exclusive parameters.Input and Output are in NCH format, where N is batch size, C is the number of the feature map, + H is the height of the feature map.The data type of Input data and Output data is 'float32' or 'float64'. + + Args: + input(Variable):3-D Tensor, shape is [N, C, H], data type can be float32 or float64 + num_channels(int): The number of channels in the input data. + num_filters(int): The number of filters. It is the same as the output channels. + filter_size (int): The filter size of Conv1DPoolLayer. + pool_size (int): The pooling size of Conv1DPoolLayer. + conv_stride (int): The stride size of the conv Layer in Conv1DPoolLayer. Default: 1 + pool_stride (int): The stride size of the pool layer in Conv1DPoolLayer. Default: 1 + conv_padding (int): The padding size of the conv Layer in Conv1DPoolLayer. Default: 0 + pool_padding (int): The padding of pool layer in Conv1DPoolLayer. Default: 0 + pool_type (str): Pooling type can be `max` for max-pooling or `avg` for average-pooling. Default: math:`max` + global_pooling (bool): Whether to use the global pooling. If global_pooling = true, pool_size and pool_padding while be ignored. Default: False + dilation (int): The dilation size of the conv Layer. Default: 1. + groups (int): The groups number of the conv Layer. According to grouped convolution in Alex Krizhevsky's Deep CNN paper: when group=2, + the first half of the filters is only connected to the first half of the input channels, while the second half of the filters is only + connected to the second half of the input channels. Default: 1. + param_attr (ParamAttr|None): The parameter attribute for learnable parameters/weights of conv layer. If it is set to None or one attribute of + ParamAttr, conv2d will create ParamAttr as param_attr. If the Initializer of the param_attr is not set, the parameter is initialized + with :`Normal(0.0, std)`,and the :`std` is :`(\\frac{2.0 }{filter\_elem\_num})^{0.5}`.Default: None. + bias_attr (ParamAttr|bool|None): The parameter attribute for the bias of conv.If it is set to False, no bias will be added to the output units. + If it is set to None or one attribute of ParamAttr, conv2d will create ParamAttr as bias_attr. If the Initializer of the bias_attr is not + set, the bias is initialized zero. Default: None. + name(str, optional): The default value is None. Normally there is no need for user to set this property. Default: None + act (str): Activation type for conv layer, if it is set to None, activation is not appended. Default: None. + use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: False + ceil_mode (bool, optional): Whether to use the ceil function to calculate output height and width. + False is the default. If it is set to False, the floor function will be used. Default: False. + exclusive (bool, optional): Whether to exclude padding points in average pooling mode. Default: True. + + Return: + 3-D Tensor, the result of input after conv and pool, with the same data type as :`input` + + Return Type: + Variable + + Example: + ```python + import paddle.fluid as fluid + from hapi.text import Conv1dPoolLayer + + test=np.random.uniform(-1,1,[2,3,4]).astype('float32') + with fluid.dygraph.guard(): + paddle_input=to_variable(test) + print(paddle_input.shape) + cov2d=Conv1dPoolLayer(3,4,2,2) + paddle_out=cov2d(paddle_input) + print(paddle_out.shape)#[2,4,2] + + ``` + """ def __init__(self, num_channels, num_filters, filter_size, + pool_size, + conv_stride=1, + pool_stride=1, + conv_padding=0, + pool_padding=0, + pool_type='max', + global_pooling=False, + dilation=1, + groups=None, + param_attr=None, + bias_attr=None, + act=None, use_cudnn=False, - act=None + ceil_mode=False, + exclusive=True, ): - super(SimpleConvPoolLayer, self).__init__() + super(Conv1dPoolLayer, self).__init__() self._conv2d = Conv2D(num_channels=num_channels, num_filters=num_filters, - filter_size=filter_size, - padding=[1, 1], + filter_size=[filter_size,1], + stride=[conv_stride,1], + padding=[conv_padding,0], + dilation=[dilation,1], + groups=groups, + param_attr=param_attr, + bias_attr=bias_attr, use_cudnn=use_cudnn, act=act) - - def forward(self, input): - x = self._conv2d(input) - x = fluid.layers.reduce_max(x, dim=-1) - x = fluid.layers.reshape(x, shape=[x.shape[0], -1]) + self._pool2d = Pool2D(pool_size=[pool_size,1], + pool_type=pool_type, + pool_stride=[pool_stride,1], + pool_padding=[pool_padding,0], + global_pooling=global_pooling, + use_cudnn=use_cudnn, + ceil_mode=ceil_mode, + exclusive=exclusive + ) + def forward(self, inputs): + x = fluid.layers.unsqueeze(inputs,axes=[-1]) + x = self._conv2d(x) + x = self._pool2d(x) + x = fluid.layers.squeeze(x, axes=[-1]) return x -class SimCNNEncoder(Layer): + + +class CNNEncoder(Layer): """ - simple CNNEncoder for simnet + This interface is used to construct a callable object of the ``CNNEncoder`` class.The ``CNNEncoder`` is composed of a ``Embedding`` and a ``Conv1dPoolLayer`` . + For more details, refer to code examples. The ``CNNEncoder`` layer calculates the output based on the input, dict_size and emb_dim, filter_size, num_filters, + use_cuda, is_sparse, param_attr parameters. The type of Input data is a Tensor or a lod-tensor .The data type of Input data is 'int64'. Output data are in NCH + format, where N is batch size, C is the number of the feature map, H is the height of the feature map. The data type of Output data is 'float32' or 'float64'. + + Args: + dict_size(int): the size of the dictionary of embeddings + emb_szie(int): the size of each embedding vector respectively. + num_channels(int): The number of channels in the input data.Default:1 + num_filters(int): The number of filters. It is the same as the output channels. + filter_size(int): The filter size of Conv1DPoolLayer in CNNEncoder. + pool_size(int): The pooling size of Conv1DPoolLayer in CNNEncoder. + use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: False + is_sparse(bool): The flag indicating whether to use sparse update. This parameter only affects the performance of the backwards gradient update. It is recommended + to set True because sparse update is faster. But some optimizer does not support sparse update,such as :ref:`api_fluid_optimizer_AdadeltaOptimizer` , + :ref:`api_fluid_optimizer_AdamaxOptimizer` , :ref:`api_fluid_optimizer_DecayedAdagradOptimizer` , :ref:`api_fluid_optimizer_FtrlOptimizer` , + :ref:`api_fluid_optimizer_LambOptimizer` and :ref:`api_fluid_optimizer_LarsMomentumOptimizer` . + In these case, is_sparse must be False. Default: True. + param_attr(ParamAttr): To specify the weight parameter property. Default: None, which means the default weight parameter property is used. See usage for details in + :ref:`api_fluid_ParamAttr` . In addition,user-defined or pre-trained word vectors can be loaded with the :attr:`param_attr` parameter. The local word vector + needs to be transformed into numpy format, and the shape of local word vector should be consistent with :attr:`size` . + Then :ref:`api_fluid_initializer_NumpyArrayInitializer` is used to load custom or pre-trained word vectors. Default: None. + padding_idx(int|long|None): padding_idx needs to be in the interval [-vocab_size, vocab_size). + If :math:`padding\_idx < 0`, the :math:`padding\_idx` will automatically be converted to :math:`vocab\_size + padding\_idx` . It will output all-zero padding + data whenever lookup encounters :math:`padding\_idx` in id. And the padding data will not be updated while training. If set None, it makes no effect to + output. Default: None. + act (str): Activation type for `Conv1dPoollayer` layer, if it is set to None, activation is not appended. Default: None. + + Return: + 3-D Tensor, the result of input after embedding and conv1dPoollayer + + Return Type: + Variable + + Example: + ```python + import paddle.fluid as fluid + from hapi.text import CNNEncoder + + test=np.random.uniform(1,5,[2,3,4]).astype('int64') + with fluid.dygraph.guard(): + paddle_input=to_variable(test) + print(paddle_input.shape) + cov2d=CNNEncoder(128,4,3,4,2,2) + paddle_out=cov2d(paddle_input) + print(paddle_out.shape)#[8,4,2] + + ``` + """ def __init__(self, dict_size, - emb_dim, - filter_size, + emb_size, + num_channels, num_filters, - hidden_dim, - seq_len, - padding_idx, - act + filter_size, + pool_size, + use_cuda=False, + is_sparse=True, + param_attr=None, + padding_idx=None, + act=None ): - super(SimCNNEncoder, self).__init__() + super(CNNEncoder, self).__init__() self.dict_size = dict_size - self.emb_dim = emb_dim + self.emb_size = emb_size self.filter_size = filter_size self.num_filters = num_filters - self.hidden_dim = hidden_dim - self.seq_len = seq_len - self.padding_idx = padding_idx - self.act = act - self.channels = 1 - self.emb_layer = Embedding(size=[self.dict_size, self.emb_dim], - is_sparse=True, - padding_idx=self.padding_idx, - param_attr=fluid.ParamAttr(name='emb', initializer=fluid.initializer.Xavier())) - self.cnn_layer = SimpleConvPoolLayer( + self.pool_size = pool_size + self.channels = num_channels + self._emb_layer = Embedding(size=[self.dict_size, self.emb_size], + is_sparse=is_sparse, + padding_idx=padding_idx, + param_attr=param_attr) + self._cnn_layer = Conv1dPoolLayer( self.channels, self.num_filters, self.filter_size, - use_cudnn=False, - act=self.act - ) - - def forward(self, input): - emb = self.emb_layer(input) - emb_reshape = fluid.layers.reshape( - emb, shape=[-1, self.channels, self.seq_len, self.hidden_dim]) - emb_out=self.cnn_layer(emb_reshape) - return emb_out - -class SimBOWEncoder(Layer): - """ - simple BOWEncoder for simnet - """ - def __init__(self, - dict_size, - emb_dim, - bow_dim, - seq_len, - padding_idx - ): - super(SimBOWEncoder, self).__init__() - self.dict_size = dict_size - self.bow_dim = bow_dim - self.seq_len = seq_len - self.emb_dim = emb_dim - self.padding_idx=padding_idx - self.emb_layer = Embedding(size=[self.dict_size, self.emb_dim], - is_sparse=True, - padding_idx=self.padding_idx, - param_attr=fluid.ParamAttr(name='emb', initializer=fluid.initializer.Xavier())) - - def forward(self, input): - emb = self.emb_layer(input) - emb_reshape = fluid.layers.reshape( - emb, shape=[-1, self.seq_len, self.bow_dim]) - bow_emb = fluid.layers.reduce_sum(emb_reshape, dim=1) - return bow_emb - -class DynamicGRU(fluid.dygraph.Layer): - def __init__(self, - size, - h_0=None, - param_attr=None, - bias_attr=None, - is_reverse=False, - gate_activation='sigmoid', - candidate_activation='tanh', - origin_mode=False, - init_size=None): - super(DynamicGRU, self).__init__() - - self.gru_unit = GRUUnit( - size * 3, - param_attr=param_attr, - bias_attr=bias_attr, - activation=candidate_activation, - gate_activation=gate_activation, - origin_mode=origin_mode) - - self.size = size - self.h_0 = h_0 - self.is_reverse = is_reverse - - def forward(self, inputs): - hidden = self.h_0 - res = [] - for i in range(inputs.shape[1]): - if self.is_reverse: - i = inputs.shape[1] - 1 - i - input_ = inputs[:, i:i + 1, :] - input_ = fluid.layers.reshape( - input_, [-1, input_.shape[2]], inplace=False) - hidden, reset, gate = self.gru_unit(input_, hidden) - hidden_ = fluid.layers.reshape( - hidden, [-1, 1, hidden.shape[1]], inplace=False) - res.append(hidden_) - if self.is_reverse: - res = res[::-1] - res = fluid.layers.concat(res, axis=1) - return res - -class SimGRUEncoder(Layer): - """ - simple GRUEncoder for simnet - """ - def __init__(self, - dict_size, - emb_dim, - gru_dim, - hidden_dim, - padding_idx, - seq_len - ): - super(SimGRUEncoder, self).__init__() - self.dict_size = dict_size - self.emb_dim = emb_dim - self.gru_dim = gru_dim - self.seq_len=seq_len - self.hidden_dim = hidden_dim - self.padding_idx=self.padding_idx - self.emb_layer = Embedding(size=[self.dict_size, self.emb_dim], - is_sparse=True, - padding_idx=self.padding_idx, - param_attr=fluid.ParamAttr(name='emb', - initializer=fluid.initializer.Xavier())) - self.gru_layer = DynamicGRU(self.gru_dim) - self.proj_layer = Linear(input_dim=self.hidden_dim, output_dim=self.gru_dim * 3) - - def forward(self, input): - emb = self.emb_layer(input) - emb_proj = self.proj_layer(emb) - h_0 = np.zeros((emb_proj.shape[0], self.hidden_dim), dtype="float32") - h_0 = to_variable(h_0) - gru = self.gru_layer(emb_proj, h_0=h_0) - gru = fluid.layers.reduce_max(gru, dim=1) - gru = fluid.layers.tanh(gru) - return gru - -class SimLSTMEncoder(Layer): - """ - simple LSTMEncoder for simnet - """ - def __init__(self, - dict_size, - emb_dim, - lstm_dim, - hidden_dim, - seq_len, - padding_idx, - is_reverse - ): - """ - initialize - """ - super(SimLSTMEncoder, self).__init__() - self.dict_size = dict_size - self.emb_dim = emb_dim - self.lstm_dim = lstm_dim - self.hidden_dim = hidden_dim - self.seq_len = seq_len - self.is_reverse = False - self.padding_idx=padding_idx - - self.emb_layer = Embedding(size=[self.dict_size, self.emb_dim], - is_sparse=True, - padding_idx=self.padding_idx, - param_attr=fluid.ParamAttr(name='emb', initializer=fluid.initializer.Xavier())) - - self.lstm_cell = BasicLSTMCell( - hidden_size=self.lstm_dim, input_size=self.lstm_dim * 4 - ) - self.lstm_layer = RNN( - cell=self.lstm_cell, time_major=True, is_reverse=self.is_reverse + self.pool_size, + use_cudnn=use_cuda, + act=act ) - self.proj_layer = Linear(input_dim=self.hidden_dim, output_dim=self.lstm_dim * 4) def forward(self, input): - emb = self.emb_layer(input) - emb_proj = self.proj_layer(emb) - emb_lstm, _ = self.lstm_layer(emb_proj) - emb_reduce = fluid.layers.reduce_max(emb_lstm, dim=1) + emb = self._emb_layer(input) emb_reshape = fluid.layers.reshape( - emb_reduce, shape=[-1, self.seq_len, self.hidden_dim]) - emb_lstm = fluid.layers.reduce_sum(emb_reshape, dim=1) - emb_last = fluid.layers.tanh(emb_lstm) - return emb_last + emb, shape=[-1, self.channels, self.emb_size]) + emb_out=self._cnn_layer(emb_reshape) + return emb_out \ No newline at end of file From 3e18097486f47db9b1e5a01ec24961944e4b5ea2 Mon Sep 17 00:00:00 2001 From: jinyuKing <2943829328@qq.com> Date: Fri, 8 May 2020 04:13:22 +0000 Subject: [PATCH 3/4] update text.py --- hapi/text/text.py | 108 +++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/hapi/text/text.py b/hapi/text/text.py index 9fec48c..4b9d928 100644 --- a/hapi/text/text.py +++ b/hapi/text/text.py @@ -2010,31 +2010,29 @@ class CNNEncoder(Layer): """ This interface is used to construct a callable object of the ``CNNEncoder`` class.The ``CNNEncoder`` is composed of a ``Embedding`` and a ``Conv1dPoolLayer`` . For more details, refer to code examples. The ``CNNEncoder`` layer calculates the output based on the input, dict_size and emb_dim, filter_size, num_filters, - use_cuda, is_sparse, param_attr parameters. The type of Input data is a Tensor or a lod-tensor .The data type of Input data is 'int64'. Output data are in NCH + use_cuda, is_sparse, param_attr parameters. The type of Input data is a 3-D Tensor .The data type of Input data is 'float32'. Output data are in NCH format, where N is batch size, C is the number of the feature map, H is the height of the feature map. The data type of Output data is 'float32' or 'float64'. Args: - dict_size(int): the size of the dictionary of embeddings - emb_szie(int): the size of each embedding vector respectively. - num_channels(int): The number of channels in the input data.Default:1 - num_filters(int): The number of filters. It is the same as the output channels. - filter_size(int): The filter size of Conv1DPoolLayer in CNNEncoder. - pool_size(int): The pooling size of Conv1DPoolLayer in CNNEncoder. + num_channels(int|list|tuple): The number of channels in the input data.If num_channels is a list or tuple, the length of num_channels must equal layer_num.If num_channels + is a int, all conv1dpoollayer's num_channels are the value of num_channels. + num_filters(int|list|tuple): The number of filters. It is the same as the output channels. If num_filters is a list or tuple, the length of num_filters must equal layer_num.If num_filters + is a int, all conv1dpoollayer's num_filters are the value of num_filters. + filter_size(int|list|tuple): The filter size of Conv1DPoolLayer in CNNEncoder. If filter_size is a list or tuple, the length of filter_size must equal layer_num.If filter_size + is a int, all conv1dpoollayer's filter_size are the value of filter_size. + pool_size(int|list|tuple): The pooling size of Conv1DPoolLayer in CNNEncoder.If pool_size is a list or tuple, the length of pool_size must equal layer_num.If pool_size + is a int, all conv1dpoollayer's pool_size are the value of pool_size. + layer_num(int): The number of conv1dpoolLayer used in CNNEncoder. + conv_stride(int|list|tuple): The stride size of the conv Layer in Conv1DPoolLayer. If conv_stride is a list or tuple, the length of conv_stride must equal layer_num.If conv_stride + is a int, all conv1dpoollayer's conv_stride are the value of conv_stride. Default: 1 + pool_stride(int|list|tuple): The stride size of the pool layer in Conv1DPoolLayer. If pool_stride is a list or tuple, the length of pool_stride must equal layer_num.If pool_stride + is a int, all conv1dpoollayer's pool_stride are the value of pool_stride. Default: 1 + conv_padding(int|list|tuple): The padding size of the conv Layer in Conv1DPoolLayer.If conv_padding is a list or tuple, the length of conv_padding must equal layer_num.If conv_padding + is a int, all conv1dpoollayer's conv_padding are the value of conv_padding. Default: 0 + pool_padding(int|list|tuple): The padding of pool layer in Conv1DPoolLayer. If pool_padding is a list or tuple, the length of pool_padding must equal layer_num.If pool_padding + is a int, all conv1dpoollayer's pool_padding are the value of pool_padding. Default: 0 use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: False - is_sparse(bool): The flag indicating whether to use sparse update. This parameter only affects the performance of the backwards gradient update. It is recommended - to set True because sparse update is faster. But some optimizer does not support sparse update,such as :ref:`api_fluid_optimizer_AdadeltaOptimizer` , - :ref:`api_fluid_optimizer_AdamaxOptimizer` , :ref:`api_fluid_optimizer_DecayedAdagradOptimizer` , :ref:`api_fluid_optimizer_FtrlOptimizer` , - :ref:`api_fluid_optimizer_LambOptimizer` and :ref:`api_fluid_optimizer_LarsMomentumOptimizer` . - In these case, is_sparse must be False. Default: True. - param_attr(ParamAttr): To specify the weight parameter property. Default: None, which means the default weight parameter property is used. See usage for details in - :ref:`api_fluid_ParamAttr` . In addition,user-defined or pre-trained word vectors can be loaded with the :attr:`param_attr` parameter. The local word vector - needs to be transformed into numpy format, and the shape of local word vector should be consistent with :attr:`size` . - Then :ref:`api_fluid_initializer_NumpyArrayInitializer` is used to load custom or pre-trained word vectors. Default: None. - padding_idx(int|long|None): padding_idx needs to be in the interval [-vocab_size, vocab_size). - If :math:`padding\_idx < 0`, the :math:`padding\_idx` will automatically be converted to :math:`vocab\_size + padding\_idx` . It will output all-zero padding - data whenever lookup encounters :math:`padding\_idx` in id. And the padding data will not be updated while training. If set None, it makes no effect to - output. Default: None. - act (str): Activation type for `Conv1dPoollayer` layer, if it is set to None, activation is not appended. Default: None. + act (str|list|tuple): Activation type for `Conv1dPoollayer` layer, if it is set to None, activation is not appended. Default: None. Return: 3-D Tensor, the result of input after embedding and conv1dPoollayer @@ -2047,53 +2045,55 @@ class CNNEncoder(Layer): import paddle.fluid as fluid from hapi.text import CNNEncoder - test=np.random.uniform(1,5,[2,3,4]).astype('int64') + test=np.random.uniform(1,5,[2,3,4]).astype('float32') with fluid.dygraph.guard(): paddle_input=to_variable(test) - print(paddle_input.shape) - cov2d=CNNEncoder(128,4,3,4,2,2) + #print(paddle_input.shape) + cov2d=CNNEncoder(3,4,2,2,3) paddle_out=cov2d(paddle_input) - print(paddle_out.shape)#[8,4,2] + print(paddle_out)#[2,12,2] ``` """ def __init__(self, - dict_size, - emb_size, num_channels, num_filters, filter_size, pool_size, - use_cuda=False, - is_sparse=True, - param_attr=None, - padding_idx=None, + layer_num, + conv_stride=1, + pool_stride=1, + conv_padding=0, + pool_padding=0, + use_cudnn=False, act=None ): super(CNNEncoder, self).__init__() - self.dict_size = dict_size - self.emb_size = emb_size - self.filter_size = filter_size - self.num_filters = num_filters - self.pool_size = pool_size - self.channels = num_channels - self._emb_layer = Embedding(size=[self.dict_size, self.emb_size], - is_sparse=is_sparse, - padding_idx=padding_idx, - param_attr=param_attr) - self._cnn_layer = Conv1dPoolLayer( - self.channels, - self.num_filters, - self.filter_size, - self.pool_size, - use_cudnn=use_cuda, - act=act - ) + self.num_channels=num_channels + self.num_filters=num_filters + self.filter_size=filter_size + self.pool_size=pool_size + self.layer_num=layer_num + self.conv_stride=conv_stride + self.pool_stride=pool_stride + self.conv_padding=conv_padding + self.pool_padding=pool_padding + self.use_cudnn=use_cudnn + self.act=act + self.conv_layer = fluid.dygraph.LayerList([Conv1dPoolLayer(num_channels=self.num_channels if isinstance(self.num_channels,int) else self.num_channels[i], + num_filters=self.num_filters if isinstance(self.num_channels,int) else self.num_filters [i], + filter_size=self.filter_size if isinstance(self.filter_size,int) else self.filter_size[i], + pool_size=self.pool_size if isinstance(self.pool_size,int) else self.pool_size[i], + conv_stride=self.conv_stride if isinstance(self.conv_stride,int) else self.conv_stride[i], + pool_stride=self.pool_stride if isinstance(self.pool_stride,int) else self.pool_stride[i], + conv_padding= self.conv_padding if isinstance(self.conv_padding,int) else self.conv_padding[i], + pool_padding=self.pool_padding if isinstance(self.pool_padding,int) else self.pool_padding[i], + act=self.act[i] if isinstance(self.act,(list,tuple)) else self.act, + use_cudnn=self.use_cudnn + ) for i in range(layer_num)]) def forward(self, input): - emb = self._emb_layer(input) - emb_reshape = fluid.layers.reshape( - emb, shape=[-1, self.channels, self.emb_size]) - emb_out=self._cnn_layer(emb_reshape) - return emb_out \ No newline at end of file + res=[Conv1dPoolLayer(input) for Conv1dPoolLayer in self.conv_layer] + out=fluid.layers.concat(input=res,axis=1) + return out \ No newline at end of file From 3b5e6d9cc12af93d14134aab25d59bb7a7f807fe Mon Sep 17 00:00:00 2001 From: jinyuKing <2943829328@qq.com> Date: Fri, 8 May 2020 09:34:39 +0000 Subject: [PATCH 4/4] update text.py --- hapi/text/text.py | 357 ++++++++++++++++++++++++++-------------------- 1 file changed, 204 insertions(+), 153 deletions(-) diff --git a/hapi/text/text.py b/hapi/text/text.py index 4b9d928..10f5f9f 100644 --- a/hapi/text/text.py +++ b/hapi/text/text.py @@ -1898,14 +1898,16 @@ def forward(self, word, lengths, target=None): crf_decode = self.crf_decoding(input=emission, length=lengths) return crf_decode, lengths + class Conv1dPoolLayer(Layer): """ - This interface is used to construct a callable object of the ``Conv1DPoolLayer`` class.The ``Conv1DPoolLayer`` is composed of a ``Conv2D`` and a ``Pool2D`` . - For more details, refer to code examples.The ``Conv1DPoolLayer`` layer calculates the output based on the input, filter and strides, paddings, dilations, - groups,global_pooling, pool_type,ceil_mode,exclusive parameters.Input and Output are in NCH format, where N is batch size, C is the number of the feature map, - H is the height of the feature map.The data type of Input data and Output data is 'float32' or 'float64'. + This interface is used to construct a callable object of the ``Conv1DPoolLayer`` class. + The ``Conv1DPoolLayer`` class does a ``Conv1D`` and a ``Pool1D`` .For more details, + refer to code examples.The ``Conv1DPoolLayer`` layer calculates the output based on the + input, filter and strides, paddings, dilations, groups,global_pooling, pool_type,ceil_mode, + exclusive parameters. - Args: + Parameters: input(Variable):3-D Tensor, shape is [N, C, H], data type can be float32 or float64 num_channels(int): The number of channels in the input data. num_filters(int): The number of filters. It is the same as the output channels. @@ -1915,147 +1917,174 @@ class Conv1dPoolLayer(Layer): pool_stride (int): The stride size of the pool layer in Conv1DPoolLayer. Default: 1 conv_padding (int): The padding size of the conv Layer in Conv1DPoolLayer. Default: 0 pool_padding (int): The padding of pool layer in Conv1DPoolLayer. Default: 0 - pool_type (str): Pooling type can be `max` for max-pooling or `avg` for average-pooling. Default: math:`max` - global_pooling (bool): Whether to use the global pooling. If global_pooling = true, pool_size and pool_padding while be ignored. Default: False + pool_type (str): Pooling type can be `max` for max-pooling or `avg` for average-pooling. + Default: math:`max` + global_pooling (bool): Whether to use the global pooling. If global_pooling = true, + pool_size and pool_padding while be ignored. Default: False dilation (int): The dilation size of the conv Layer. Default: 1. - groups (int): The groups number of the conv Layer. According to grouped convolution in Alex Krizhevsky's Deep CNN paper: when group=2, - the first half of the filters is only connected to the first half of the input channels, while the second half of the filters is only - connected to the second half of the input channels. Default: 1. - param_attr (ParamAttr|None): The parameter attribute for learnable parameters/weights of conv layer. If it is set to None or one attribute of - ParamAttr, conv2d will create ParamAttr as param_attr. If the Initializer of the param_attr is not set, the parameter is initialized - with :`Normal(0.0, std)`,and the :`std` is :`(\\frac{2.0 }{filter\_elem\_num})^{0.5}`.Default: None. - bias_attr (ParamAttr|bool|None): The parameter attribute for the bias of conv.If it is set to False, no bias will be added to the output units. - If it is set to None or one attribute of ParamAttr, conv2d will create ParamAttr as bias_attr. If the Initializer of the bias_attr is not - set, the bias is initialized zero. Default: None. - name(str, optional): The default value is None. Normally there is no need for user to set this property. Default: None - act (str): Activation type for conv layer, if it is set to None, activation is not appended. Default: None. - use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: False - ceil_mode (bool, optional): Whether to use the ceil function to calculate output height and width. - False is the default. If it is set to False, the floor function will be used. Default: False. - exclusive (bool, optional): Whether to exclude padding points in average pooling mode. Default: True. - - Return: - 3-D Tensor, the result of input after conv and pool, with the same data type as :`input` - - Return Type: - Variable + groups (int): The groups number of the conv Layer. According to grouped convolution in + Alex Krizhevsky's Deep CNN paper: when group=2, the first half of the filters is + only connected to the first half of the input channels, while the second half + of the filters is only connected to the second half of the input channels. + Default: 1. + param_attr (ParamAttr|None): The parameter attribute for learnable parameters/weights + of conv layer. If it is set to None or one attribute of ParamAttr, conv2d will + create ParamAttr as param_attr. If the Initializer of the param_attr + is not set, the parameter is initialized with :`Normal(0.0, std)`,and + the :`std` is :`(\\frac{2.0 }{filter\_elem\_num})^{0.5}`.Default: None. + bias_attr (ParamAttr|bool|None): The parameter attribute for the bias of conv.If it + is set to False, no bias will be added to the output units.If it is set to + None or one attribute of ParamAttr, conv2d will create ParamAttr as bias_attr. + If the Initializer of the bias_attr is not set, the bias is initialized zero. + Default: None. + act (str): Activation type for conv layer, if it is set to None, activation is not appended. + Default: None. + use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library + is installed. Default: False + ceil_mode (bool, optional): Whether to use the ceil function to calculate output + height and width.False is the default. If it is set to False, the floor function + will be used. Default: False. + exclusive (bool, optional): Whether to exclude padding points in average pooling mode. + Default: True. Example: - ```python - import paddle.fluid as fluid - from hapi.text import Conv1dPoolLayer - - test=np.random.uniform(-1,1,[2,3,4]).astype('float32') - with fluid.dygraph.guard(): - paddle_input=to_variable(test) - print(paddle_input.shape) - cov2d=Conv1dPoolLayer(3,4,2,2) - paddle_out=cov2d(paddle_input) - print(paddle_out.shape)#[2,4,2] - - ``` + + .. code-block:: python + + import paddle.fluid as fluid + from paddle.incubate.hapi.text import Conv1dPoolLayer + + test=np.random.uniform(-1,1,[2,3,4]).astype('float32') + with fluid.dygraph.guard(): + paddle_input=to_variable(test) + cov2d=Conv1dPoolLayer(3,4,2,2) + paddle_out=cov2d(paddle_input) + print(paddle_out.shape)#[2,4,2] + """ - def __init__(self, - num_channels, - num_filters, - filter_size, - pool_size, - conv_stride=1, - pool_stride=1, - conv_padding=0, - pool_padding=0, - pool_type='max', - global_pooling=False, - dilation=1, - groups=None, - param_attr=None, - bias_attr=None, - act=None, - use_cudnn=False, - ceil_mode=False, - exclusive=True, - ): + + def __init__( + self, + num_channels, + num_filters, + filter_size, + pool_size, + conv_stride=1, + pool_stride=1, + conv_padding=0, + pool_padding=0, + pool_type='max', + global_pooling=False, + dilation=1, + groups=None, + param_attr=None, + bias_attr=None, + act=None, + use_cudnn=False, + ceil_mode=False, + exclusive=True, ): super(Conv1dPoolLayer, self).__init__() - self._conv2d = Conv2D(num_channels=num_channels, - num_filters=num_filters, - filter_size=[filter_size,1], - stride=[conv_stride,1], - padding=[conv_padding,0], - dilation=[dilation,1], - groups=groups, - param_attr=param_attr, - bias_attr=bias_attr, - use_cudnn=use_cudnn, - act=act) - self._pool2d = Pool2D(pool_size=[pool_size,1], - pool_type=pool_type, - pool_stride=[pool_stride,1], - pool_padding=[pool_padding,0], - global_pooling=global_pooling, - use_cudnn=use_cudnn, - ceil_mode=ceil_mode, - exclusive=exclusive - ) + self._conv2d = Conv2D( + num_channels=num_channels, + num_filters=num_filters, + filter_size=[filter_size, 1], + stride=[conv_stride, 1], + padding=[conv_padding, 0], + dilation=[dilation, 1], + groups=groups, + param_attr=param_attr, + bias_attr=bias_attr, + use_cudnn=use_cudnn, + act=act) + self._pool2d = Pool2D( + pool_size=[pool_size, 1], + pool_type=pool_type, + pool_stride=[pool_stride, 1], + pool_padding=[pool_padding, 0], + global_pooling=global_pooling, + use_cudnn=use_cudnn, + ceil_mode=ceil_mode, + exclusive=exclusive) + def forward(self, inputs): - x = fluid.layers.unsqueeze(inputs,axes=[-1]) + """ + Performs :Code:`Conv1dPoolLayer.forward` receives input data. After a conv and a pool, + the result will be output. + + Parameters: + inputs(Variable): Inputs are 3-D Tensor, shape is [N, C, H] , where N is batch size, + C is the number of the feature map, H is the height of the feature map.The data + type of Input data is 'float32' or 'float64'. + + Returns: + 3-D Tensor, with the same data type as :`input` + """ + x = fluid.layers.unsqueeze(inputs, axes=[-1]) x = self._conv2d(x) x = self._pool2d(x) x = fluid.layers.squeeze(x, axes=[-1]) return x - - class CNNEncoder(Layer): """ - This interface is used to construct a callable object of the ``CNNEncoder`` class.The ``CNNEncoder`` is composed of a ``Embedding`` and a ``Conv1dPoolLayer`` . - For more details, refer to code examples. The ``CNNEncoder`` layer calculates the output based on the input, dict_size and emb_dim, filter_size, num_filters, - use_cuda, is_sparse, param_attr parameters. The type of Input data is a 3-D Tensor .The data type of Input data is 'float32'. Output data are in NCH - format, where N is batch size, C is the number of the feature map, H is the height of the feature map. The data type of Output data is 'float32' or 'float64'. - - Args: - num_channels(int|list|tuple): The number of channels in the input data.If num_channels is a list or tuple, the length of num_channels must equal layer_num.If num_channels - is a int, all conv1dpoollayer's num_channels are the value of num_channels. - num_filters(int|list|tuple): The number of filters. It is the same as the output channels. If num_filters is a list or tuple, the length of num_filters must equal layer_num.If num_filters - is a int, all conv1dpoollayer's num_filters are the value of num_filters. - filter_size(int|list|tuple): The filter size of Conv1DPoolLayer in CNNEncoder. If filter_size is a list or tuple, the length of filter_size must equal layer_num.If filter_size - is a int, all conv1dpoollayer's filter_size are the value of filter_size. - pool_size(int|list|tuple): The pooling size of Conv1DPoolLayer in CNNEncoder.If pool_size is a list or tuple, the length of pool_size must equal layer_num.If pool_size - is a int, all conv1dpoollayer's pool_size are the value of pool_size. - layer_num(int): The number of conv1dpoolLayer used in CNNEncoder. - conv_stride(int|list|tuple): The stride size of the conv Layer in Conv1DPoolLayer. If conv_stride is a list or tuple, the length of conv_stride must equal layer_num.If conv_stride - is a int, all conv1dpoollayer's conv_stride are the value of conv_stride. Default: 1 - pool_stride(int|list|tuple): The stride size of the pool layer in Conv1DPoolLayer. If pool_stride is a list or tuple, the length of pool_stride must equal layer_num.If pool_stride - is a int, all conv1dpoollayer's pool_stride are the value of pool_stride. Default: 1 - conv_padding(int|list|tuple): The padding size of the conv Layer in Conv1DPoolLayer.If conv_padding is a list or tuple, the length of conv_padding must equal layer_num.If conv_padding - is a int, all conv1dpoollayer's conv_padding are the value of conv_padding. Default: 0 - pool_padding(int|list|tuple): The padding of pool layer in Conv1DPoolLayer. If pool_padding is a list or tuple, the length of pool_padding must equal layer_num.If pool_padding - is a int, all conv1dpoollayer's pool_padding are the value of pool_padding. Default: 0 - use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: False - act (str|list|tuple): Activation type for `Conv1dPoollayer` layer, if it is set to None, activation is not appended. Default: None. - - Return: - 3-D Tensor, the result of input after embedding and conv1dPoollayer - - Return Type: - Variable + This interface is used to construct a callable object of the ``CNNEncoder`` class.The ``CNNEncoder`` + is composed of ``Conv1dPoolLayer`` .``CNNEncoder`` can define every Conv1dPoolLayer with different + or same parameters. The ``Conv1dPoolLayer`` in ``CNNEncoder`` is parallel.The result of every + ``Conv1dPoolLayer`` will concat as the final output.For more details, refer to code examples. + The ``CNNEncoder`` layer calculates the output based on the input, dict_size and emb_dim, + filter_size, num_filters, use_cuda, is_sparse, param_attr parameters. + + Parameters: + num_channels(int|list|tuple): The number of channels in the input data.If num_channels is + a list or tuple, the length of num_channels must equal layer_num.If num_channels + is a int, all conv1dpoollayer's num_channels are the value of num_channels. + num_filters(int|list|tuple): The number of filters. It is the same as the output channels. + If num_filters is a list or tuple, the length of num_filters must equal layer_num. + If num_filters is a int, all conv1dpoollayer's num_filters are the value of num_filters. + filter_size(int|list|tuple): The filter size of Conv1DPoolLayer in CNNEncoder. If filter_size + is a list or tuple, the length of filter_size must equal layer_num.If filter_size is a + int, all conv1dpoollayer's filter_size are the value of filter_size. + pool_size(int|list|tuple): The pooling size of Conv1DPoolLayer in CNNEncoder.If pool_size is + a list or tuple, the length of pool_size must equal layer_num.If pool_size is a int, + all conv1dpoollayer's pool_size are the value of pool_size. + layer_num(int): The number of conv1dpoolLayer used in CNNEncoder. + conv_stride(int|list|tuple): The stride size of the conv Layer in Conv1DPoolLayer. If + conv_stride is a list or tuple, the length of conv_stride must equal layer_num. + If conv_stride is a int, all conv1dpoollayer's conv_stride are the value of + conv_stride. Default: 1 + pool_stride(int|list|tuple): The stride size of the pool layer in Conv1DPoolLayer. If + pool_stride is a list or tuple, the length of pool_stride must equal layer_num. + If pool_stride is a int, all conv1dpoollayer's pool_stride are the value of + pool_stride. Default: 1 + conv_padding(int|list|tuple): The padding size of the conv Layer in Conv1DPoolLayer. + If conv_padding is a list or tuple, the length of conv_padding must equal layer_num. + If conv_padding is a int, all conv1dpoollayer's conv_padding are the value of + conv_padding. Default: 0 + pool_padding(int|list|tuple): The padding of pool layer in Conv1DPoolLayer. If pool_padding is + a list or tuple, the length of pool_padding must equal layer_num.If pool_padding is a + int, all conv1dpoollayer's pool_padding are the value of pool_padding. Default: 0 + use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn library is installed. + Default: False + act (str|list|tuple): Activation type for `Conv1dPoollayer` layer, if it is set to None, + activation is not appended. Default: None. Example: - ```python - import paddle.fluid as fluid - from hapi.text import CNNEncoder - test=np.random.uniform(1,5,[2,3,4]).astype('float32') - with fluid.dygraph.guard(): - paddle_input=to_variable(test) - #print(paddle_input.shape) - cov2d=CNNEncoder(3,4,2,2,3) - paddle_out=cov2d(paddle_input) - print(paddle_out)#[2,12,2] + .. code-block:: python + + import paddle.fluid as fluid + from paddle.incubate.hapi.text import Conv1dPoolLayer - ``` + test=np.random.uniform(-1,1,[2,3,4]).astype('float32') + with fluid.dygraph.guard(): + paddle_input=to_variable(test) + cov2d=CNNEncoder(3,4,2,2,3) + paddle_out=cov2d(paddle_input) + print(paddle_out)#[2,12,2] """ + def __init__(self, num_channels, num_filters, @@ -2067,33 +2096,55 @@ def __init__(self, conv_padding=0, pool_padding=0, use_cudnn=False, - act=None - ): + act=None): super(CNNEncoder, self).__init__() - self.num_channels=num_channels - self.num_filters=num_filters - self.filter_size=filter_size - self.pool_size=pool_size - self.layer_num=layer_num - self.conv_stride=conv_stride - self.pool_stride=pool_stride - self.conv_padding=conv_padding - self.pool_padding=pool_padding - self.use_cudnn=use_cudnn - self.act=act - self.conv_layer = fluid.dygraph.LayerList([Conv1dPoolLayer(num_channels=self.num_channels if isinstance(self.num_channels,int) else self.num_channels[i], - num_filters=self.num_filters if isinstance(self.num_channels,int) else self.num_filters [i], - filter_size=self.filter_size if isinstance(self.filter_size,int) else self.filter_size[i], - pool_size=self.pool_size if isinstance(self.pool_size,int) else self.pool_size[i], - conv_stride=self.conv_stride if isinstance(self.conv_stride,int) else self.conv_stride[i], - pool_stride=self.pool_stride if isinstance(self.pool_stride,int) else self.pool_stride[i], - conv_padding= self.conv_padding if isinstance(self.conv_padding,int) else self.conv_padding[i], - pool_padding=self.pool_padding if isinstance(self.pool_padding,int) else self.pool_padding[i], - act=self.act[i] if isinstance(self.act,(list,tuple)) else self.act, - use_cudnn=self.use_cudnn - ) for i in range(layer_num)]) + self.num_channels = num_channels + self.num_filters = num_filters + self.filter_size = filter_size + self.pool_size = pool_size + self.layer_num = layer_num + self.conv_stride = conv_stride + self.pool_stride = pool_stride + self.conv_padding = conv_padding + self.pool_padding = pool_padding + self.use_cudnn = use_cudnn + self.act = act + self.conv_layer = fluid.dygraph.LayerList([ + Conv1dPoolLayer( + num_channels=self.num_channels if + isinstance(self.num_channels, int) else self.num_channels[i], + num_filters=self.num_filters + if isinstance(self.num_channels, int) else self.num_filters[i], + filter_size=self.filter_size + if isinstance(self.filter_size, int) else self.filter_size[i], + pool_size=self.pool_size + if isinstance(self.pool_size, int) else self.pool_size[i], + conv_stride=self.conv_stride + if isinstance(self.conv_stride, int) else self.conv_stride[i], + pool_stride=self.pool_stride + if isinstance(self.pool_stride, int) else self.pool_stride[i], + conv_padding=self.conv_padding if + isinstance(self.conv_padding, int) else self.conv_padding[i], + pool_padding=self.pool_padding if + isinstance(self.pool_padding, int) else self.pool_padding[i], + act=self.act[i] + if isinstance(self.act, (list, tuple)) else self.act, + use_cudnn=self.use_cudnn) for i in range(layer_num) + ]) def forward(self, input): - res=[Conv1dPoolLayer(input) for Conv1dPoolLayer in self.conv_layer] - out=fluid.layers.concat(input=res,axis=1) - return out \ No newline at end of file + """ + Performs :Code:`CNNEncoder.forward` receives input data.The result of every ``Conv1dPoolLayer`` + will store in a list. Then, the results will be concated as the final output. + + Parameters: + inputs(Variable): Inputs are 3-D Tensor, shape is [N, C, H] , where N is batch size, + C is the number of the feature map, H is the height of the feature map.The data + type of Input data is 'float32' or 'float64'. + + Returns: + 3-D Tensor, with the same data type as :`input` + """ + res = [Conv1dPoolLayer(input) for Conv1dPoolLayer in self.conv_layer] + out = fluid.layers.concat(input=res, axis=1) + return out