fluid.nets

glu

paddle.fluid.nets.glu(input, dim=-1)

The Gated Linear Units(GLU) composed by split, sigmoid activation and element-wise multiplication. Specifically, Split the input into two equal sized parts, \(a\) and \(b\), along the given dimension and then compute as following:

\[{GLU}(a, b)= a \otimes \sigma(b)\]

Refer to Language Modeling with Gated Convolutional Networks.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • dim (int) – The dimension along which to split. If \(dim < 0\), the dimension to split along is \(rank(input) + dim\). Default -1.
Returns:

Variable with half the size of input.

Return type:

Variable

Examples

data = fluid.layers.data(name="words", shape=[3, 6, 9], dtype="float32")
output = fluid.nets.glu(input=data, dim=1)  # shape of output: [3, 3, 9]

img_conv_group

paddle.fluid.nets.img_conv_group(input, conv_num_filter, pool_size, conv_padding=1, conv_filter_size=3, conv_act=None, param_attr=None, conv_with_batchnorm=False, conv_batchnorm_drop_rate=0.0, pool_stride=1, pool_type='max', use_cudnn=True)

The Image Convolution Group is composed of Convolution2d, BatchNorm, DropOut, and Pool2d. According to the input arguments, img_conv_group will do serials of computation for Input using Convolution2d, BatchNorm, DropOut, and pass the last result to Pool2d.

Parameters:
  • input (Variable) – The input image with [N, C, H, W] format.
  • conv_num_filter (list|tuple) – Indicates the numbers of filter of this group.
  • pool_size (int|list|tuple) – The pooling size of Pool2d Layer. If pool_size is a list or tuple, it must contain two integers, (pool_size_H, pool_size_W). Otherwise, the pool_size_H = pool_size_W = pool_size.
  • conv_padding (int|list|tuple) – The padding size of the Conv2d Layer. If padding is a list or tuple, its length must be equal to the length of conv_num_filter. Otherwise the conv_padding of all Conv2d Layers are the same. Default 1.
  • conv_filter_size (int|list|tuple) – The filter size. If filter_size is a list or tuple, its length must be equal to the length of conv_num_filter. Otherwise the conv_filter_size of all Conv2d Layers are the same. Default 3.
  • conv_act (str) – Activation type for Conv2d Layer that is not followed by BatchNorm. Default: None.
  • param_attr (ParamAttr) – The parameters to the Conv2d Layer. Default: None
  • conv_with_batchnorm (bool|list) – Indicates whether to use BatchNorm after Conv2d Layer. If conv_with_batchnorm is a list, its length must be equal to the length of conv_num_filter. Otherwise, conv_with_batchnorm indicates whether all the Conv2d Layer follows a BatchNorm. Default False.
  • conv_batchnorm_drop_rate (float|list) – Indicates the drop_rate of Dropout Layer after BatchNorm. If conv_batchnorm_drop_rate is a list, its length must be equal to the length of conv_num_filter. Otherwise, drop_rate of all Dropout Layers is conv_batchnorm_drop_rate. Default 0.0.
  • pool_stride (int|list|tuple) – The pooling stride of Pool2d layer. If pool_stride is a list or tuple, it must contain two integers, (pooling_stride_H, pooling_stride_W). Otherwise, the pooling_stride_H = pooling_stride_W = pool_stride. Default 1.
  • pool_type (str) – Pooling type can be \(max\) for max-pooling and \(avg\) for average-pooling. Default \(max\).
  • use_cudnn (bool) – Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: True
Returns:

The final result after serial computation using Convolution2d,

BatchNorm, DropOut, and Pool2d.

Return type:

Variable

Examples

img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
conv_pool = fluid.nets.img_conv_group(input=img,
                                      num_channels=3,
                                      conv_padding=1,
                                      conv_num_filter=[3, 3],
                                      conv_filter_size=3,
                                      conv_act="relu",
                                      pool_size=2,
                                      pool_stride=2)

scaled_dot_product_attention

paddle.fluid.nets.scaled_dot_product_attention(queries, keys, values, num_heads=1, dropout_rate=0.0)

The dot-product attention.

Attention mechanism can be seen as mapping a query and a set of key-value pairs to an output. The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function (dot-product here) of the query with the corresponding key.

The dot-product attention can be implemented through (batch) matrix multipication as follows:

\[Attention(Q, K, V)= softmax(QK^\mathrm{T})V\]

Refer to Attention Is All You Need.

Parameters:
  • queries (Variable) – The input variable which should be a 3-D Tensor.
  • keys (Variable) – The input variable which should be a 3-D Tensor.
  • values (Variable) – The input variable which should be a 3-D Tensor.
  • num_heads (int) – Head number to compute the scaled dot product attention. Default: 1.
  • dropout_rate (float) – The dropout rate to drop the attention weight. Default: 0.0.
Returns:

A 3-D Tensor computed by multi-head scaled dot product attention.

Return type:

Variable

Raises:

ValueError – If input queries, keys, values are not 3-D Tensors.

Notes

  1. When num_heads > 1, three linear projections are learned respectively to map input queries, keys and values into queries’, keys’ and values’. queries’, keys’ and values’ have the same shapes with queries, keys and values.
  2. When num_heads == 1, scaled_dot_product_attention has no learnable parameters.

Examples

queries = fluid.layers.data(name="queries",
                            shape=[3, 5, 9],
                            dtype="float32",
                            append_batch_size=False)
queries.stop_gradient = False
keys = fluid.layers.data(name="keys",
                         shape=[3, 6, 9],
                         dtype="float32",
                         append_batch_size=False)
keys.stop_gradient = False
values = fluid.layers.data(name="values",
                           shape=[3, 6, 10],
                           dtype="float32",
                           append_batch_size=False)
values.stop_gradient = False
contexts = fluid.nets.scaled_dot_product_attention(queries, keys, values)
contexts.shape  # [3, 5, 10]

sequence_conv_pool

paddle.fluid.nets.sequence_conv_pool(input, num_filters, filter_size, param_attr=None, act='sigmoid', pool_type='max')

The sequence_conv_pool is composed with Sequence Convolution and Pooling.

Parameters:
  • input (Variable) – The input of sequence_conv, which supports variable-time length input sequence. The underlying of input is a matrix with shape (T, N), where T is the total time steps in this mini-batch and N is the input_hidden_size
  • num_filters (int) – The number of filter.
  • filter_size (int) – The filter size.
  • param_attr (ParamAttr) – The parameters to the Sequence_conv Layer. Default: None.
  • act (str) – Activation type for Sequence_conv Layer. Default: “sigmoid”.
  • pool_type (str) – Pooling type can be \(max\) for max-pooling, \(average\) for average-pooling, \(sum\) for sum-pooling, \(sqrt\) for sqrt-pooling. Default \(max\).
Returns:

The final result after Sequence Convolution and Pooling.

Return type:

Variable

Examples

input_dim = len(word_dict)
emb_dim = 128
hid_dim = 512
data = fluid.layers.data( ame="words", shape=[1], dtype="int64", lod_level=1)
emb = fluid.layers.embedding(input=data, size=[input_dim, emb_dim], is_sparse=True)
seq_conv = fluid.nets.sequence_conv_pool(input=emb,
                                         num_filters=hid_dim,
                                         filter_size=3,
                                         act="tanh",
                                         pool_type="sqrt")

simple_img_conv_pool

paddle.fluid.nets.simple_img_conv_pool(input, num_filters, filter_size, pool_size, pool_stride, pool_padding=0, pool_type='max', global_pooling=False, conv_stride=1, conv_padding=0, conv_dilation=1, conv_groups=1, param_attr=None, bias_attr=None, act=None, use_cudnn=True)

The simple_img_conv_pool is composed with one Convolution2d and one Pool2d.

Parameters:
  • input (Variable) – The input image with [N, C, H, W] format.
  • num_filters (int) – The number of filter. It is as same as the output feature channel.
  • filter_size (int|list|tuple) – The filter size. If filter_size is a list or tuple, it must contain two integers, (filter_size_H, filter_size_W). Otherwise, the filter_size_H = filter_size_W = filter_size.
  • pool_size (int|list|tuple) – The pooling size of Pool2d layer. If pool_size is a list or tuple, it must contain two integers, (pool_size_H, pool_size_W). Otherwise, the pool_size_H = pool_size_W = pool_size.
  • pool_stride (int|list|tuple) – The pooling stride of Pool2d layer. If pool_stride is a list or tuple, it must contain two integers, (pooling_stride_H, pooling_stride_W). Otherwise, the pooling_stride_H = pooling_stride_W = pool_stride.
  • pool_padding (int|list|tuple) – The padding of Pool2d layer. If pool_padding is a list or tuple, it must contain two integers, (pool_padding_H, pool_padding_W). Otherwise, the pool_padding_H = pool_padding_W = pool_padding. Default 0.
  • pool_type (str) – Pooling type can be \(max\) for max-pooling and \(avg\) for average-pooling. Default \(max\).
  • global_pooling (bool) – Whether to use the global pooling. If global_pooling = true, pool_size and pool_padding while be ignored. Default False
  • conv_stride (int|list|tuple) – The stride size of the conv2d Layer. If stride is a list or tuple, it must contain two integers, (conv_stride_H, conv_stride_W). Otherwise, the conv_stride_H = conv_stride_W = conv_stride. Default: conv_stride = 1.
  • conv_padding (int|list|tuple) – The padding size of the conv2d Layer. If padding is a list or tuple, it must contain two integers, (conv_padding_H, conv_padding_W). Otherwise, the conv_padding_H = conv_padding_W = conv_padding. Default: conv_padding = 0.
  • conv_dilation (int|list|tuple) – The dilation size of the conv2d Layer. If dilation is a list or tuple, it must contain two integers, (conv_dilation_H, conv_dilation_W). Otherwise, the conv_dilation_H = conv_dilation_W = conv_dilation. Default: conv_dilation = 1.
  • conv_groups (int) – The groups number of the conv2d 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: groups=1.
  • param_attr (ParamAttr|None) – The parameter attribute for learnable parameters/weights of conv2d. 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 conv2d. 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 conv2d, 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: True
Returns:

The result of input after Convolution2d and Pool2d.

Return type:

Variable

Examples

img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
conv_pool = fluid.nets.simple_img_conv_pool(input=img,
                                            filter_size=5,
                                            num_filters=20,
                                            pool_size=2,
                                            pool_stride=2,
                                            act="relu")