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, use_mkldnn=False)

The simple_img_conv_pool is composed with one Convolution2d and one Pool2d.

  • 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) – The parameters to the Conv2d Layer. Default: None
  • bias_attr (ParamAttr) – Bias parameter for the Conv2d layer. Default: None
  • act (str) – Activation type for Conv2d. Default: None
  • use_cudnn (bool) – Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: True
  • use_mkldnn (bool) – Use mkldnn kernels or not, it is valid only when compiled with mkldnn library. Default: False

The result of input after Convolution2d and Pool2d.

Return type:



img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
conv_pool = fluid.nets.simple_img_conv_pool(input=img,


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.

  • 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\).

The final result after Sequence Convolution and Pooling.

Return type:



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,


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.

  • 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.

Variable with half the size of input.

Return type:



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]


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.

  • 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.

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

Return type:



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


  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.


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