Layers

paddle.v2.layer is a part of model config packages in paddle.v2. In API v2, we want to make Paddle a plain Python package. The model config package defined the way how to configure a neural network topology in Paddle Python code.

The primary usage shows below.

import paddle.v2 as paddle

img = paddle.layer.data(name='img', type=paddle.data_type.dense_vector(784))
hidden = paddle.layer.fc(input=img, size=200)
prediction = paddle.layer.fc(input=hidden, size=10,
                             act=paddle.activation.Softmax())

# use prediction instance where needed.
parameters = paddle.parameters.create(cost)
paddle.v2.layer.parse_network(*outputs)

Parse all output layers and then generate a ModelConfig object.

Note

This function is used internally in paddle.v2 module. User should never invoke this method.

Parameters:outputs (Layer) – Output layers.
Returns:A ModelConfig object instance.
Return type:ModelConfig
class paddle.v2.layer.data(name, type, **kwargs)

Define DataLayer For NeuralNetwork.

The example usage is:

data = paddle.layer.data(name="input", type=paddle.data_type.dense_vector(1000))
Parameters:
  • name (basestring) – Name of this data layer.
  • type – Data type of this data layer
  • height (int|None) – Height of this data layer, used for image
  • width (int|None) – Width of this data layer, used for image
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.addto(*args, **kwargs)

AddtoLayer.

\[y = f(\sum_{i} x_i + b)\]

where \(y\) is output, \(x\) is input, \(b\) is bias, and \(f\) is activation function.

The example usage is:

addto = addto(input=[layer1, layer2],
                    act=paddle.v2.Activation.Relu(),
                    bias_attr=False)

This layer just simply add all input layers together, then activate the sum inputs. Each input of this layer should be the same size, which is also the output size of this layer.

There is no weight matrix for each input, because it just a simple add operation. If you want a complicated operation before add, please use mixed.

It is a very good way to set dropout outside the layers. Since not all PaddlePaddle layer support dropout, you can add an add_to layer, set dropout here. Please refer to dropout for details.

Parameters:
  • name (basestring) – Layer name.
  • input (paddle.v2.config_base.Layer|list|tuple) – Input layers. It could be a paddle.v2.config_base.Layer or list/tuple of paddle.v2.config_base.Layer.
  • act (paddle.v2.Activation.Base) – Activation Type, default is tanh.
  • bias_attr (paddle.v2.attr.ParameterAttribute|bool) – Bias attribute. If False, means no bias. None is default bias.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.batch_norm(*args, **kwargs)

Batch Normalization Layer. The notation of this layer as follow.

\(x\) is the input features over a mini-batch.

\[\begin{split}\mu_{\beta} &\gets \frac{1}{m} \sum_{i=1}^{m} x_i \qquad &//\ \ mini-batch\ mean \\ \sigma_{\beta}^{2} &\gets \frac{1}{m} \sum_{i=1}^{m}(x_i - \ \mu_{\beta})^2 \qquad &//\ mini-batch\ variance \\ \hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift\end{split}\]

The details of batch normalization please refer to this paper.

The example usage is:

norm = batch_norm(input=net, act=paddle.v2.Activation.Relu())
Parameters:
  • name (basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – batch normalization input. Better be linear activation. Because there is an activation inside batch_normalization.
  • batch_norm_type (None|string, None or "batch_norm" or "cudnn_batch_norm") – We have batch_norm and cudnn_batch_norm. batch_norm supports both CPU and GPU. cudnn_batch_norm requires cuDNN version greater or equal to v4 (>=v4). But cudnn_batch_norm is faster and needs less memory than batch_norm. By default (None), we will automaticly select cudnn_batch_norm for GPU and batch_norm for CPU. Otherwise, select batch norm type based on the specified type. If you use cudnn_batch_norm, we suggested you use latest version, such as v5.1.
  • act (paddle.v2.Activation.Base) – Activation Type. Better be relu. Because batch normalization will normalize input near zero.
  • num_channels (int) – num of image channels or previous layer’s number of filters. None will automatically get from layer’s input.
  • bias_attr (paddle.v2.attr.ParameterAttribute) – \(\beta\), better be zero when initialize. So the initial_std=0, initial_mean=1 is best practice.
  • param_attr (paddle.v2.attr.ParameterAttribute) – \(\gamma\), better be one when initialize. So the initial_std=0, initial_mean=1 is best practice.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
  • use_global_stats (bool|None.) – whether use moving mean/variance statistics during testing peroid. If None or True, it will use moving mean/variance statistics during testing. If False, it will use the mean and variance of current batch of test data for testing.
  • moving_average_fraction (float.) – Factor used in the moving average computation, referred to as facotr, \(runningMean = newMean*(1-factor) + runningMean*factor\)
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.bilinear_interp(*args, **kwargs)

This layer is to implement bilinear interpolation on conv layer output.

Please refer to Wikipedia: https://en.wikipedia.org/wiki/Bilinear_interpolation

The simple usage is:

bilinear = bilinear_interp(input=layer1, out_size_x=64, out_size_y=64)
Parameters:
  • input (paddle.v2.config_base.Layer.) – A input layer.
  • out_size_x (int|None) – bilinear interpolation output width.
  • out_size_y (int|None) – bilinear interpolation output height.
  • name (None|basestring) – The layer’s name, which cna not be specified.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.block_expand(*args, **kwargs)
Expand feature map to minibatch matrix.
  • matrix width is: block_y * block_x * num_channels
  • matirx height is: outputH * outputW
\[ \begin{align}\begin{aligned}outputH = 1 + (2 * padding_y + imgSizeH - block_y + stride_y - 1) / stride_y\\outputW = 1 + (2 * padding_x + imgSizeW - block_x + stride_x - 1) / stride_x\end{aligned}\end{align} \]

The expand method is the same with ExpandConvLayer, but saved the transposed value. After expanding, output.sequenceStartPositions will store timeline. The number of time steps are outputH * outputW and the dimension of each time step is block_y * block_x * num_channels. This layer can be used after convolution neural network, and before recurrent neural network.

The simple usage is:

block_expand = block_expand(input=layer,
                                  num_channels=128,
                                  stride_x=1,
                                  stride_y=1,
                                  block_x=1,
                                  block_x=3)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • num_channels (int|None) – The channel number of input layer.
  • block_x (int) – The width of sub block.
  • block_y (int) – The width of sub block.
  • stride_x (int) – The stride size in horizontal direction.
  • stride_y (int) – The stride size in vertical direction.
  • padding_x (int) – The padding size in horizontal direction.
  • padding_y (int) – The padding size in vertical direction.
  • name (None|basestring.) – The name of this layer, which can not specify.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.classification_cost(*args, **kwargs)

classification cost Layer.

Parameters:
  • name (basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – input layer name. network output.
  • label (paddle.v2.config_base.Layer) – label layer name. data often.
  • weight (paddle.v2.config_base.Layer) – The weight affects the cost, namely the scale of cost. It is an optional argument.
  • top_k (int) – number k in top-k error rate
  • evaluator – Evaluator method.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – layer’s extra attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.concat(*args, **kwargs)

Concat all input vector into one huge vector. Inputs can be list of paddle.v2.config_base.Layer or list of projection.

The example usage is:

concat = concat(input=[layer1, layer2])
Parameters:
  • name (basestring) – Layer name.
  • input (list|tuple|collections.Sequence) – input layers or projections
  • act (paddle.v2.Activation.Base) – Activation type.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.conv_shift(*args, **kwargs)
This layer performs cyclic convolution for two input. For example:
  • a[in]: contains M elements.
  • b[in]: contains N elements (N should be odd).
  • c[out]: contains M elements.
\[c[i] = \sum_{j=-(N-1)/2}^{(N-1)/2}a_{i+j} * b_{j}\]
In this formular:
  • a’s index is computed modulo M. When it is negative, then get item from the right side (which is the end of array) to the left.
  • b’s index is computed modulo N. When it is negative, then get item from the right size (which is the end of array) to the left.

The example usage is:

conv_shift = conv_shift(a=layer1, b=layer2)
Parameters:
  • name (basestring) – layer name
  • a (paddle.v2.config_base.Layer) – Input layer a.
  • b (paddle.v2.config_base.Layer) – input layer b.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – layer’s extra attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.convex_comb(*args, **kwargs)
A layer for weighted sum of vectors takes two inputs.
  • Input: size of weights is M
    size of vectors is M*N
  • Output: a vector of size=N
\[z(i) = \sum_{j=0}^{M-1} x(j) y(i+Nj)\]

where \(0 \le i \le N-1\)

Or in the matrix notation:

\[z = x^\mathrm{T} Y\]
In this formular:
  • \(x\): weights
  • \(y\): vectors.
  • \(z\): the output.

Note that the above computation is for one sample. Multiple samples are processed in one batch.

The simple usage is:

linear_comb = linear_comb(weights=weight, vectors=vectors,
                                size=elem_dim)
Parameters:
  • weights (paddle.v2.config_base.Layer) – The weight layer.
  • vectors (paddle.v2.config_base.Layer) – The vector layer.
  • size (int) – the dimension of this layer.
  • name (basestring) – The Layer Name.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.cos_sim(*args, **kwargs)

Cosine Similarity Layer. The cosine similarity equation is here.

\[similarity = cos(\theta) = {\mathbf{a} \cdot \mathbf{b} \over \|\mathbf{a}\| \|\mathbf{b}\|}\]

The size of a is M, size of b is M*N, Similarity will be calculated N times by step M. The output size is N. The scale will be multiplied to similarity.

Note that the above computation is for one sample. Multiple samples are processed in one batch.

The example usage is:

cos = cos_sim(a=layer1, b=layer2, size=3)
Parameters:
  • name (basestring) – layer name
  • a (paddle.v2.config_base.Layer) – input layer a
  • b (paddle.v2.config_base.Layer) – input layer b
  • scale (float) – scale for cosine value. default is 5.
  • size (int) – layer size. NOTE size_a * size should equal size_b.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.crf_decoding(*args, **kwargs)

A layer for calculating the decoding sequence of sequential conditional random field model. The decoding sequence is stored in output.ids. If a second input is provided, it is treated as the ground-truth label, and this layer will also calculate error. output.value[i] is 1 for incorrect decoding or 0 for correct decoding.

The simple usage:

crf_decoding = crf_decoding(input=input,
                                  size=label_dim)
Parameters:
  • input (paddle.v2.config_base.Layer) – The first input layer.
  • size (int) – size of this layer.
  • label (paddle.v2.config_base.Layer or None) – None or ground-truth label.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter attribute. None means default attribute
  • name (None|basestring) – The name of this layers. It is not necessary.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.crf(*args, **kwargs)

A layer for calculating the cost of sequential conditional random field model.

The simple usage:

crf = crf(input=input,
                label=label,
                size=label_dim)
Parameters:
  • input (paddle.v2.config_base.Layer) – The first input layer is the feature.
  • label (paddle.v2.config_base.Layer) – The second input layer is label.
  • size (int) – The category number.
  • weight (paddle.v2.config_base.Layer) – The third layer is “weight” of each sample, which is an optional argument.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter attribute. None means default attribute
  • name (None|basestring) – The name of this layers. It is not necessary.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.cross_entropy_cost(*args, **kwargs)

A loss layer for multi class entropy.

cost = cross_entropy(input=input,
                     label=label)
Parameters:
  • input (paddle.v2.config_base.Layer.) – The first input layer.
  • label – The input label.
  • name (None|basestring.) – The name of this layers. It is not necessary.
  • coeff (float.) – The coefficient affects the gradient in the backward.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer.

class paddle.v2.layer.cross_entropy_with_selfnorm_cost(*args, **kwargs)

A loss layer for multi class entropy with selfnorm. Input should be a vector of positive numbers, without normalization.

cost = cross_entropy_with_selfnorm(input=input,
                                   label=label)
Parameters:
  • input (paddle.v2.config_base.Layer.) – The first input layer.
  • label – The input label.
  • name (None|basestring.) – The name of this layers. It is not necessary.
  • coeff (float.) – The coefficient affects the gradient in the backward.
  • softmax_selfnorm_alpha (float.) – The scale factor affects the cost.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer.

class paddle.v2.layer.ctc(*args, **kwargs)

Connectionist Temporal Classification (CTC) is designed for temporal classication task. That is, for sequence labeling problems where the alignment between the inputs and the target labels is unknown.

More details can be found by referring to Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks

Note

Considering the ‘blank’ label needed by CTC, you need to use (num_classes + 1) as the input size. num_classes is the category number. And the ‘blank’ is the last category index. So the size of ‘input’ layer, such as fc with softmax activation, should be num_classes + 1. The size of ctc should also be num_classes + 1.

The simple usage:

ctc = ctc(input=input,
                label=label,
                size=9055,
                norm_by_times=True)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • label (paddle.v2.config_base.Layer) – The data layer of label with variable length.
  • size (int) – category numbers + 1.
  • name (basestring|None) – The name of this layer
  • norm_by_times (bool) – Whether to normalization by times. False by default.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.dropout(*args, **kwargs)

@TODO(yuyang18): Add comments.

Parameters:
  • name
  • input
  • dropout_rate
Returns:

class paddle.v2.layer.embedding(*args, **kwargs)

Define a embedding Layer.

Parameters:
  • name (basestring) – Name of this embedding layer.
  • input (paddle.v2.config_base.Layer) – The input layer for this embedding. NOTE: must be Index Data.
  • size (int) – The embedding dimension.
  • param_attr (paddle.v2.attr.ParameterAttribute|None) – The embedding parameter attribute. See paddle.v2.attr.ParameterAttribute for details.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra layer Config. Default is None.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.eos(*args, **kwargs)

A layer for checking EOS for each sample: - output_id = (input_id == conf.eos_id)

The result is stored in output_.ids. It is used by recurrent layer group.

The example usage is:

eos = eos(input=layer, eos_id=id)
Parameters:
  • name (basestring) – Layer name.
  • input (paddle.v2.config_base.Layer) – Input layer name.
  • eos_id (int) – end id of sequence
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.expand(*args, **kwargs)

A layer for “Expand Dense data or (sequence data where the length of each sequence is one) to sequence data.”

The example usage is:

expand = expand(input=layer1,
                      expand_as=layer2,
                      expand_level=ExpandLevel.FROM_TIMESTEP)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer
  • expand_as (paddle.v2.config_base.Layer) – Expand as this layer’s sequence info.
  • name (basestring) – Layer name.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|False) – Bias attribute. None means default bias. False means no bias.
  • expand_level (ExpandLevel) – whether input layer is timestep(default) or sequence.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.fc(*args, **kwargs)

Helper for declare fully connected layer.

The example usage is:

fc = fc(input=layer,
              size=1024,
              act=paddle.v2.Activation.Linear(),
              bias_attr=False)

which is equal to:

with mixed(size=1024) as fc:
    fc += full_matrix_projection(input=layer)
Parameters:
  • name (basestring) – The Layer Name.
  • input (paddle.v2.config_base.Layer|list|tuple) – The input layer. Could be a list/tuple of input layer.
  • size (int) – The layer dimension.
  • act (paddle.v2.Activation.Base) – Activation Type. Default is tanh.
  • param_attr (paddle.v2.attr.ParameterAttribute) – The Parameter Attribute|list.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|Any) – The Bias Attribute. If no bias, then pass False or something not type of paddle.v2.attr.ParameterAttribute. None will get a default Bias.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.first_seq(*args, **kwargs)

Get First Timestamp Activation of a sequence.

The simple usage is:

seq = first_seq(input=layer)
Parameters:
  • agg_level – aggregation level
  • name (basestring) – Layer name.
  • input (paddle.v2.config_base.Layer) – Input layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.get_output(*args, **kwargs)

Get layer’s output by name. In PaddlePaddle, a layer might return multiple values, but returns one layer’s output. If the user wants to use another output besides the default one, please use get_output first to get the output from input.

Parameters:
  • name (basestring) – Layer’s name.
  • input (paddle.v2.config_base.Layer) – get output layer’s input. And this layer should contains multiple outputs.
  • arg_name (basestring) – Output name from input.
  • layer_attr – Layer’s extra attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.gru_step(*args, **kwargs)
Parameters:
  • input (paddle.v2.config_base.Layer) –
  • output_mem
  • size
  • act
  • name
  • gate_act
  • bias_attr
  • param_attr – the parameter_attribute for transforming the output_mem from previous step.
  • layer_attr
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.grumemory(*args, **kwargs)

Gate Recurrent Unit Layer.

The memory cell was implemented as follow equations.

1. update gate \(z\): defines how much of the previous memory to keep around or the unit updates its activations. The update gate is computed by:

\[z_t = \sigma(W_{z}x_{t} + U_{z}h_{t-1} + b_z)\]

2. reset gate \(r\): determines how to combine the new input with the previous memory. The reset gate is computed similarly to the update gate:

\[r_t = \sigma(W_{r}x_{t} + U_{r}h_{t-1} + b_r)\]

3. The candidate activation \(\tilde{h_t}\) is computed similarly to that of the traditional recurrent unit:

\[{\tilde{h_t}} = tanh(W x_{t} + U (r_{t} \odot h_{t-1}) + b)\]

4. The hidden activation \(h_t\) of the GRU at time t is a linear interpolation between the previous activation \(h_{t-1}\) and the candidate activation \(\tilde{h_t}\):

\[h_t = (1 - z_t) h_{t-1} + z_t {\tilde{h_t}}\]

NOTE: In PaddlePaddle’s implementation, the multiplication operations \(W_{r}x_{t}\), \(W_{z}x_{t}\) and \(W x_t\) are not computed in gate_recurrent layer. Consequently, an additional mixed with full_matrix_projection or a fc must be included before grumemory is called.

More details can be found by referring to Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling.

The simple usage is:

gru = grumemory(input)
Parameters:
  • name (None|basestring) – The gru layer name.
  • input (paddle.v2.config_base.Layer.) – input layer.
  • reverse (bool) – Whether sequence process is reversed or not.
  • act (paddle.v2.Activation.Base) – activation type, paddle.v2.Activation.Tanh by default. This activation affects the \({\tilde{h_t}}\).
  • gate_act (paddle.v2.Activation.Base) – gate activation type, paddle.v2.Activation.Sigmoid by default. This activation affects the \(z_t\) and \(r_t\). It is the \(\sigma\) in the above formula.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|False) – Bias attribute. None means default bias. False means no bias.
  • param_attr (paddle.v2.attr.ParameterAttribute|None|False) – Parameter Attribute.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer attribute
  • size (None) – Stub parameter of size, but actually not used. If set this size will get a warning.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.hsigmoid(*args, **kwargs)

Organize the classes into a binary tree. At each node, a sigmoid function is used to calculate the probability of belonging to the right branch. This idea is from “F. Morin, Y. Bengio (AISTATS 05): Hierarchical Probabilistic Neural Network Language Model.”

The example usage is:

cost = hsigmoid(input=[layer1, layer2],
                label=data,
                num_classes=3)
Parameters:
  • input (paddle.v2.config_base.Layer|list|tuple) – Input layers. It could be a paddle.v2.config_base.Layer or list/tuple of paddle.v2.config_base.Layer.
  • label (paddle.v2.config_base.Layer) – Label layer.
  • num_classes (int) – number of classes.
  • name (basestring) – layer name
  • bias_attr (paddle.v2.attr.ParameterAttribute|False) – Bias attribute. None means default bias. False means no bias.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.huber_cost(*args, **kwargs)

A loss layer for huber loss.

cost = huber_cost(input=input,
                  label=label)
Parameters:
  • input (paddle.v2.config_base.Layer.) – The first input layer.
  • label – The input label.
  • name (None|basestring.) – The name of this layers. It is not necessary.
  • coeff (float.) – The coefficient affects the gradient in the backward.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer.

class paddle.v2.layer.img_cmrnorm(*args, **kwargs)

Response normalization across feature maps. The details please refer to Alex’s paper.

The example usage is:

norm = img_cmrnorm(input=net, size=5)
Parameters:
  • name (None|basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – layer’s input.
  • size (int) – Normalize in number of \(size\) feature maps.
  • scale (float) – The hyper-parameter.
  • power (float) – The hyper-parameter.
  • num_channels – input layer’s filers number or channels. If num_channels is None, it will be set automatically.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.img_conv(*args, **kwargs)

Convolution layer for image. Paddle can support both square and non-square input currently.

The details of convolution layer, please refer UFLDL’s convolution .

Convolution Transpose (deconv) layer for image. Paddle can support both square and non-square input currently.

The details of convolution transpose layer, please refer to the following explanation and references therein <http://datascience.stackexchange.com/questions/6107/ what-are-deconvolutional-layers/>`_ . The num_channel means input image’s channel number. It may be 1 or 3 when input is raw pixels of image(mono or RGB), or it may be the previous layer’s num_filters * num_group.

There are several group of filter in PaddlePaddle implementation. Each group will process some channel of the inputs. For example, if an input num_channel = 256, group = 4, num_filter=32, the PaddlePaddle will create 32*4 = 128 filters to process inputs. The channels will be split into 4 pieces. First 256/4 = 64 channels will process by first 32 filters. The rest channels will be processed by rest group of filters.

The example usage is:

conv = img_conv(input=data, filter_size=1, filter_size_y=1,
                      num_channels=8,
                      num_filters=16, stride=1,
                      bias_attr=False,
                      act=paddle.v2.Activation.Relu())
Parameters:
  • name (basestring) – Layer name.
  • input (paddle.v2.config_base.Layer) – Layer Input.
  • filter_size (int|tuple|list) – The x dimension of a filter kernel. Or input a tuple for two image dimension.
  • filter_size_y (int|None) – The y dimension of a filter kernel. Since PaddlePaddle currently supports rectangular filters, the filter’s shape will be (filter_size, filter_size_y).
  • num_filters – Each filter group’s number of filter
  • act (paddle.v2.Activation.Base) – Activation type. Default is tanh
  • groups (int) – Group size of filters.
  • stride (int|tuple|list) – The x dimension of the stride. Or input a tuple for two image dimension.
  • stride_y (int) – The y dimension of the stride.
  • padding (int|tuple|list) – The x dimension of the padding. Or input a tuple for two image dimension
  • padding_y (int) – The y dimension of the padding.
  • bias_attr (paddle.v2.attr.ParameterAttribute|False) – Convolution bias attribute. None means default bias. False means no bias.
  • num_channels (int) – number of input channels. If None will be set automatically from previous output.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Convolution param attribute. None means default attribute
  • shared_biases (bool) – Is biases will be shared between filters or not.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Layer Extra Attribute.
  • trans (bool) – true if it is a convTransLayer, false if it is a convLayer
  • layer_type (String) – specify the layer_type, default is None. If trans=True, layer_type has to be “exconvt”, otherwise layer_type has to be either “exconv” or “cudnn_conv”
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.img_pool(*args, **kwargs)

Image pooling Layer.

The details of pooling layer, please refer ufldl’s pooling .

  • ceil_mode=True:
\[w = 1 + int(ceil(input\_width + 2 * padding - pool\_size) / float(stride)) h = 1 + int(ceil(input\_height + 2 * padding\_y - pool\_size\_y) / float(stride\_y))\]
  • ceil_mode=False:
\[w = 1 + int(floor(input\_width + 2 * padding - pool\_size) / float(stride)) h = 1 + int(floor(input\_height + 2 * padding\_y - pool\_size\_y) / float(stride\_y))\]

The example usage is:

maxpool = img_pool(input=conv,
                         pool_size=3,
                         pool_size_y=5,
                         num_channels=8,
                         stride=1,
                         stride_y=2,
                         padding=1,
                         padding_y=2,
                         pool_type=MaxPooling())
Parameters:
  • padding (int) – pooling padding width.
  • padding_y (int|None) – pooling padding height. It’s equal to padding by default.
  • name (basestring.) – name of pooling layer
  • input (paddle.v2.config_base.Layer) – layer’s input
  • pool_size (int) – pooling window width
  • pool_size_y (int|None) – pooling window height. It’s eaqual to pool_size by default.
  • num_channels (int) – number of input channel.
  • pool_type (BasePoolingType) – pooling type. MaxPooling or AvgPooling. Default is MaxPooling.
  • stride (int) – stride width of pooling.
  • stride_y (int|None) – stride height of pooling. It is equal to stride by default.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer attribute.
  • ceil_mode (bool) – Wether to use ceil mode to calculate output height and with. Defalut is True. If set false, Otherwise use floor.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.interpolation(*args, **kwargs)

This layer is for linear interpolation with two inputs, which is used in NEURAL TURING MACHINE.

\[y.row[i] = w[i] * x_1.row[i] + (1 - w[i]) * x_2.row[i]\]

where \(x_1\) and \(x_2\) are two (batchSize x dataDim) inputs, \(w\) is (batchSize x 1) weight vector, and \(y\) is (batchSize x dataDim) output.

The example usage is:

interpolation = interpolation(input=[layer1, layer2], weight=layer3)
Parameters:
  • input (list|tuple) – Input layer.
  • weight (paddle.v2.config_base.Layer) – Weight layer.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.lambda_cost(*args, **kwargs)

lambdaCost for lambdaRank LTR approach.

The simple usage:

cost = lambda_cost(input=input,
                   score=score,
                   NDCG_num=8,
                   max_sort_size=-1)
Parameters:
  • input (paddle.v2.config_base.Layer) – Samples of the same query should be loaded as sequence.
  • score – The 2nd input. Score of each sample.
  • NDCG_num (int) – The size of NDCG (Normalized Discounted Cumulative Gain), e.g., 5 for NDCG@5. It must be less than for equal to the minimum size of lists.
  • max_sort_size (int) – The size of partial sorting in calculating gradient. If max_sort_size = -1, then for each list, the algorithm will sort the entire list to get gradient. In other cases, max_sort_size must be greater than or equal to NDCG_num. And if max_sort_size is greater than the size of a list, the algorithm will sort the entire list of get gradient.
  • name (None|basestring) – The name of this layers. It is not necessary.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.last_seq(*args, **kwargs)

Get Last Timestamp Activation of a sequence.

The simple usage is:

seq = last_seq(input=layer)
Parameters:
  • agg_level – Aggregated level
  • name (basestring) – Layer name.
  • input (paddle.v2.config_base.Layer) – Input layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.linear_comb(*args, **kwargs)
A layer for weighted sum of vectors takes two inputs.
  • Input: size of weights is M
    size of vectors is M*N
  • Output: a vector of size=N
\[z(i) = \sum_{j=0}^{M-1} x(j) y(i+Nj)\]

where \(0 \le i \le N-1\)

Or in the matrix notation:

\[z = x^\mathrm{T} Y\]
In this formular:
  • \(x\): weights
  • \(y\): vectors.
  • \(z\): the output.

Note that the above computation is for one sample. Multiple samples are processed in one batch.

The simple usage is:

linear_comb = linear_comb(weights=weight, vectors=vectors,
                                size=elem_dim)
Parameters:
  • weights (paddle.v2.config_base.Layer) – The weight layer.
  • vectors (paddle.v2.config_base.Layer) – The vector layer.
  • size (int) – the dimension of this layer.
  • name (basestring) – The Layer Name.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.lstm_step(*args, **kwargs)

LSTM Step Layer. It used in recurrent_group. The lstm equations are shown as follow.

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{xi}x_{t} + W_{hi}h_{t-1} + W_{ci}c_{t-1} + b_i)\\f_t & = \sigma(W_{xf}x_{t} + W_{hf}h_{t-1} + W_{cf}c_{t-1} + b_f)\\c_t & = f_tc_{t-1} + i_t tanh (W_{xc}x_t+W_{hc}h_{t-1} + b_c)\\o_t & = \sigma(W_{xo}x_{t} + W_{ho}h_{t-1} + W_{co}c_t + b_o)\\h_t & = o_t tanh(c_t)\end{aligned}\end{align} \]

The input of lstm step is \(Wx_t + Wh_{t-1}\), and user should use mixed and full_matrix_projection to calculate these input vector.

The state of lstm step is \(c_{t-1}\). And lstm step layer will do

\[ \begin{align}\begin{aligned}i_t = \sigma(input + W_{ci}c_{t-1} + b_i)\\...\end{aligned}\end{align} \]

This layer contains two outputs. Default output is \(h_t\). The other output is \(o_t\), which name is ‘state’ and can use get_output to extract this output.

Parameters:
  • name (basestring) – Layer’s name.
  • size (int) – Layer’s size. NOTE: lstm layer’s size, should be equal as input.size/4, and should be equal as state.size.
  • input (paddle.v2.config_base.Layer) – input layer. \(Wx_t + Wh_{t-1}\)
  • state (paddle.v2.config_base.Layer) – State Layer. \(c_{t-1}\)
  • act (paddle.v2.Activation.Base) – Activation type. Default is tanh
  • gate_act (paddle.v2.Activation.Base) – Gate Activation Type. Default is sigmoid, and should be sigmoid only.
  • state_act (paddle.v2.Activation.Base) – State Activation Type. Default is sigmoid, and should be sigmoid only.
  • bias_attr (paddle.v2.attr.ParameterAttribute) – Bias Attribute.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – layer’s extra attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.lstmemory(*args, **kwargs)

Long Short-term Memory Cell.

The memory cell was implemented as follow equations.

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{xi}x_{t} + W_{hi}h_{t-1} + W_{ci}c_{t-1} + b_i)\\f_t & = \sigma(W_{xf}x_{t} + W_{hf}h_{t-1} + W_{cf}c_{t-1} + b_f)\\c_t & = f_tc_{t-1} + i_t tanh (W_{xc}x_t+W_{hc}h_{t-1} + b_c)\\o_t & = \sigma(W_{xo}x_{t} + W_{ho}h_{t-1} + W_{co}c_t + b_o)\\h_t & = o_t tanh(c_t)\end{aligned}\end{align} \]

NOTE: In PaddlePaddle’s implementation, the multiplications \(W_{xi}x_{t}\) , \(W_{xf}x_{t}\), \(W_{xc}x_t\), \(W_{xo}x_{t}\) are not done in the lstmemory layer, so an additional mixed with full_matrix_projection or a fc must be included in the configuration file to complete the input-to-hidden mappings before lstmemory is called.

NOTE: This is a low level user interface. You can use network.simple_lstm to config a simple plain lstm layer.

Please refer to Generating Sequences With Recurrent Neural Networks for more details about LSTM.

Link goes as below.

Parameters:
  • name (basestring) – The lstmemory layer name.
  • input (paddle.v2.config_base.Layer) – input layer name.
  • reverse (bool) – is sequence process reversed or not.
  • act (paddle.v2.Activation.Base) – activation type, paddle.v2.Activation.Tanh by default. \(h_t\)
  • gate_act (paddle.v2.Activation.Base) – gate activation type, paddle.v2.Activation.Sigmoid by default.
  • state_act (paddle.v2.Activation.Base) – state activation type, paddle.v2.Activation.Tanh by default.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|False) – Bias attribute. None means default bias. False means no bias.
  • param_attr (paddle.v2.attr.ParameterAttribute|None|False) – Parameter Attribute.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer attribute
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.max_id(*args, **kwargs)

A layer for finding the id which has the maximal value for each sample. The result is stored in output.ids.

The example usage is:

maxid = maxid(input=layer)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer name.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.maxout(*args, **kwargs)
A layer to do max out on conv layer output.
  • Input: output of a conv layer.
  • Output: feature map size same as input. Channel is (input channel) / groups.

So groups should be larger than 1, and the num of channels should be able to devided by groups.

Please refer to Paper:

The simple usage is:

maxout = maxout(input,
                      num_channels=128,
                      groups=4)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • num_channels (int|None) – The channel number of input layer. If None will be set automatically from previous output.
  • groups (int) – The group number of input layer.
  • name (None|basestring.) – The name of this layer, which can not specify.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.multi_binary_label_cross_entropy_cost(*args, **kwargs)

A loss layer for multi binary label cross entropy.

cost = multi_binary_label_cross_entropy(input=input,
                                        label=label)
Parameters:
  • input (paddle.v2.config_base.Layer) – The first input layer.
  • label – The input label.
  • type (basestring) – The type of cost.
  • name (None|basestring) – The name of this layers. It is not necessary.
  • coeff (float) – The coefficient affects the gradient in the backward.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.nce(*args, **kwargs)

Noise-contrastive estimation. Implements the method in the following paper: A fast and simple algorithm for training neural probabilistic language models.

The example usage is:

cost = nce(input=layer1, label=layer2, weight=layer3,
                 num_classes=3, neg_distribution=[0.1,0.3,0.6])
Parameters:
  • name (basestring) – layer name
  • input (paddle.v2.config_base.Layer|list|tuple|collections.Sequence) – input layers. It could be a paddle.v2.config_base.Layer of list/tuple of paddle.v2.config_base.Layer.
  • label (paddle.v2.config_base.Layer) – label layer
  • weight (paddle.v2.config_base.Layer) – weight layer, can be None(default)
  • num_classes (int) – number of classes.
  • num_neg_samples (int) – number of negative samples. Default is 10.
  • neg_distribution (list|tuple|collections.Sequence|None) – The distribution for generating the random negative labels. A uniform distribution will be used if not provided. If not None, its length must be equal to num_classes.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|False) – Bias parameter attribute. True if no bias.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

layer name.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.out_prod(*args, **kwargs)

A layer for computing the outer product of two vectors The result is a matrix of size(input1) x size(input2)

The example usage is:

out_prod = out_prod(input1=vec1, input2=vec2)
Parameters:
  • name (basestring) – Layer name.
  • input1 – The first input layer name.
  • input2 (paddle.v2.config_base.Layer) – The second input layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.pad(*args, **kwargs)

This operation pads zeros to the input data according to pad_c,pad_h and pad_w. pad_c, pad_h, pad_w specifies the which dimension and size of padding. And the input data shape is NCHW.

For example, pad_c=[2,3] means padding 2 zeros before the input data and 3 zeros after the input data in channel dimension. pad_h means padding zeros in height dimension. pad_w means padding zeros in width dimension.

For example,

input(2,2,2,3)  = [
                    [ [[1,2,3], [3,4,5]],
                      [[2,3,5], [1,6,7]] ],
                    [ [[4,3,1], [1,8,7]],
                      [[3,8,9], [2,3,5]] ]
                  ]

pad_c=[1,1], pad_h=[0,0], pad_w=[0,0]

output(2,4,2,3) = [
                    [ [[0,0,0], [0,0,0]],
                      [[1,2,3], [3,4,5]],
                      [[2,3,5], [1,6,7]],
                      [[0,0,0], [0,0,0]] ],
                    [ [[0,0,0], [0,0,0]],
                      [[4,3,1], [1,8,7]],
                      [[3,8,9], [2,3,5]],
                      [[0,0,0], [0,0,0]] ]
                  ]

The simply usage is:

pad = pad(input=ipt,
                pad_c=[4,4],
                pad_h=[0,0],
                pad_w=[2,2])
Parameters:
  • input (paddle.v2.config_base.Layer) – layer’s input.
  • pad_c (list|None) – padding size in channel dimension.
  • pad_h (list|None) – padding size in height dimension.
  • pad_w (list|None) – padding size in width dimension.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
  • name (basestring) – layer name.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.pooling(*args, **kwargs)

Pooling layer for sequence inputs, not used for Image.

The example usage is:

seq_pool = pooling(input=layer,
                         pooling_type=AvgPooling(),
                         agg_level=AggregateLevel.EACH_SEQUENCE)
Parameters:
  • agg_level (AggregateLevel) – AggregateLevel.EACH_TIMESTEP or AggregateLevel.EACH_SEQUENCE
  • name (basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – input layer name.
  • pooling_type (BasePoolingType|None) – Type of pooling, MaxPooling(default), AvgPooling, SumPooling, SquareRootNPooling.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|False) – Bias parameter attribute. False if no bias.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – The Extra Attributes for layer, such as dropout.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.power(*args, **kwargs)

This layer applies a power function to a vector element-wise, which is used in NEURAL TURING MACHINE.

\[y = x^w\]

where \(x\) is a input vector, \(w\) is scalar weight, and \(y\) is a output vector.

The example usage is:

power = power(input=layer1, weight=layer2)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • weight (paddle.v2.config_base.Layer) – Weight layer.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.print(*args, **kwargs)

Print the output value of input layers. This layer is useful for debugging.

Parameters:
  • name (basestring) – The Layer Name.
  • input (paddle.v2.config_base.Layer|list|tuple) – The input layer. Could be a list/tuple of input layer.
Returns:

paddle.v2.config_base.Layer

class paddle.v2.layer.priorbox(*args, **kwargs)

Compute the priorbox and set the variance. This layer is necessary for ssd.

Parameters:
  • name (basestring) – The Layer Name.
  • input (paddle.v2.config_base.Layer) – The input layer.
  • image (paddle.v2.config_base.Layer) – The network input image.
  • aspect_ratio (list) – The aspect ratio.
  • variance – The bounding box variance.
  • min_size (The min size of the priorbox width/height.) – list
  • max_size (The max size of the priorbox width/height. Could be NULL.) – list
Returns:

paddle.v2.config_base.Layer

class paddle.v2.layer.rank_cost(*args, **kwargs)

A cost Layer for learning to rank using gradient descent. Details can refer to papers. This layer contains at least three inputs. The weight is an optional argument, which affects the cost.

\[ \begin{align}\begin{aligned}C_{i,j} & = -\tilde{P_{ij}} * o_{i,j} + log(1 + e^{o_{i,j}})\\o_{i,j} & = o_i - o_j\\\tilde{P_{i,j}} & = \{0, 0.5, 1\} \ or \ \{0, 1\}\end{aligned}\end{align} \]
In this formula:
  • \(C_{i,j}\) is the cross entropy cost.
  • \(\tilde{P_{i,j}}\) is the label. 1 means positive order and 0 means reverse order.
  • \(o_i\) and \(o_j\): the left output and right output. Their dimension is one.

The simple usage:

cost = rank_cost(left=out_left,
                 right=out_right,
                 label=label)
Parameters:
  • left (paddle.v2.config_base.Layer) – The first input, the size of this layer is 1.
  • right (paddle.v2.config_base.Layer) – The right input, the size of this layer is 1.
  • label (paddle.v2.config_base.Layer) – Label is 1 or 0, means positive order and reverse order.
  • weight (paddle.v2.config_base.Layer) – The weight affects the cost, namely the scale of cost. It is an optional argument.
  • name (None|basestring) – The name of this layers. It is not necessary.
  • coeff (float) – The coefficient affects the gradient in the backward.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.recurrent(*args, **kwargs)

Simple recurrent unit layer. It is just a fully connect layer through both time and neural network.

For each sequence [start, end] it performs the following computation:

\[\begin{split}out_{i} = act(in_{i}) \ \ \text{for} \ i = start \\ out_{i} = act(in_{i} + out_{i-1} * W) \ \ \text{for} \ start < i <= end\end{split}\]

If reversed is true, the order is reversed:

\[\begin{split}out_{i} = act(in_{i}) \ \ \text{for} \ i = end \\ out_{i} = act(in_{i} + out_{i+1} * W) \ \ \text{for} \ start <= i < end\end{split}\]
Parameters:
  • input (paddle.v2.config_base.Layer) – Input Layer
  • act (paddle.v2.Activation.Base) – activation.
  • bias_attr (paddle.v2.attr.ParameterAttribute) – bias attribute.
  • param_attr (paddle.v2.attr.ParameterAttribute) – parameter attribute.
  • name (basestring) – name of the layer
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.regression_cost(*args, **kwargs)

Regression Layer.

TODO(yuyang18): Complete this method.

Parameters:
  • name (basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – Network prediction.
  • label (paddle.v2.config_base.Layer) – Data label.
  • weight (paddle.v2.config_base.Layer) – The weight affects the cost, namely the scale of cost. It is an optional argument.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – layer’s extra attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.repeat(*args, **kwargs)

A layer for repeating the input for num_repeats times. This is equivalent to apply concat() with num_repeats same input.

\[y = [x, x, \cdots, x]\]

The example usage is:

expand = repeat(input=layer, num_repeats=4)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer
  • num_repeats (int) – Repeat the input so many times
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.rotate(*args, **kwargs)

A layer for rotating 90 degrees (clock-wise) for each feature channel, usually used when the input sample is some image or feature map.

\[y(j,i,:) = x(M-i-1,j,:)\]

where \(x\) is (M x N x C) input, and \(y\) is (N x M x C) output.

The example usage is:

rot = rotate(input=layer,
                   height=100,
                   width=100)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • height (int) – The height of the sample matrix
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.sampling_id(*args, **kwargs)

A layer for sampling id from multinomial distribution from the input layer. Sampling one id for one sample.

The simple usage is:

samping_id = sampling_id(input=input)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • name (basestring) – The Layer Name.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.scaling(*args, **kwargs)

A layer for multiplying input vector by weight scalar.

\[y = w x\]

where \(x\) is size=dataDim input, \(w\) is size=1 weight, and \(y\) is size=dataDim output.

Note that the above computation is for one sample. Multiple samples are processed in one batch.

The example usage is:

scale = scaling(input=layer1, weight=layer2)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • weight (paddle.v2.config_base.Layer) – Weight layer.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.selective_fc(*args, **kwargs)

Selectived fully connected layer. Different from fc, the output of this layer maybe sparse. It requires an additional input to indicate several selected columns for output. If the selected columns is not specified, selective_fc acts exactly like fc.

The simple usage is:

sel_fc = selective_fc(input=input, size=128, act=paddle.v2.Activation.Tanh())
Parameters:
  • name (basestring) – The Layer Name.
  • input (paddle.v2.config_base.Layer|list|tuple) – The input layer.
  • select (paddle.v2.config_base.Layer) – The select layer. The output of select layer should be a sparse binary matrix, and treat as the mask of selective fc. If is None, acts exactly like fc.
  • size (int) – The layer dimension.
  • act (paddle.v2.Activation.Base) – Activation Type. Default is tanh.
  • param_attr (paddle.v2.attr.ParameterAttribute) – The Parameter Attribute.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|Any) – The Bias Attribute. If no bias, then pass False or something not type of paddle.v2.attr.ParameterAttribute. None will get a default Bias.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.seq_concat(*args, **kwargs)

Concat sequence a with sequence b.

Inputs:
  • a = [a1, a2, ..., an]
  • b = [b1, b2, ..., bn]
  • Note that the length of a and b should be the same.

Output: [a1, b1, a2, b2, ..., an, bn]

The example usage is:

concat = seq_concat(a=layer1, b=layer2)
Parameters:
  • name (basestring) – Layer name.
  • a (paddle.v2.config_base.Layer) – input sequence layer
  • b (paddle.v2.config_base.Layer) – input sequence layer
  • act (paddle.v2.Activation.Base) – Activation type.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
  • bias_attr (paddle.v2.attr.ParameterAttribute or None or bool) – The Bias Attribute. If no bias, then pass False or something not type of paddle.v2.attr.ParameterAttribute. None will get a default Bias.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.seq_reshape(*args, **kwargs)

A layer for reshaping the sequence. Assume the input sequence has T instances, the dimension of each instance is M, and the input reshape_size is N, then the output sequence has T*M/N instances, the dimension of each instance is N.

Note that T*M/N must be an integer.

The example usage is:

reshape = seq_reshape(input=layer, reshape_size=4)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • reshape_size (int) – the size of reshaped sequence.
  • name (basestring) – Layer name.
  • act (paddle.v2.Activation.Base) – Activation type.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
  • bias_attr (paddle.v2.attr.ParameterAttribute or None or bool) – The Bias Attribute. If no bias, then pass False or something not type of paddle.v2.attr.ParameterAttribute. None will get a default Bias.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.slope_intercept(*args, **kwargs)

This layer for applying a slope and an intercept to the input element-wise. There is no activation and weight.

\[y = slope * x + intercept\]

The simple usage is:

scale = slope_intercept(input=input, slope=-1.0, intercept=1.0)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • name (basestring) – The Layer Name.
  • slope (float.) – the scale factor.
  • intercept (float.) – the offset.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.spp(*args, **kwargs)

Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition. The details please refer to Kaiming He’s paper.

The example usage is:

spp = spp(input=data,
                pyramid_height=2,
                num_channels=16,
                pool_type=MaxPooling())
Parameters:
  • name (basestring) – layer name.
  • input (paddle.v2.config_base.Layer) – layer’s input.
  • num_channels (int) – number of input channel.
  • pool_type – Pooling type. MaxPooling or AveragePooling. Default is MaxPooling.
  • pyramid_height (int) – pyramid height.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.sum_cost(*args, **kwargs)

A loss layer which calculate the sum of the input as loss

cost = sum_cost(input=input)
Parameters:
  • input (paddle.v2.config_base.Layer.) – The first input layer.
  • name (None|basestring.) – The name of this layers. It is not necessary.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – Extra Layer Attribute.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer.

class paddle.v2.layer.sum_to_one_norm(*args, **kwargs)

A layer for sum-to-one normalization, which is used in NEURAL TURING MACHINE.

\[out[i] = \frac {in[i]} {\sum_{k=1}^N in[k]}\]

where \(in\) is a (batchSize x dataDim) input vector, and \(out\) is a (batchSize x dataDim) output vector.

The example usage is:

sum_to_one_norm = sum_to_one_norm(input=layer)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.tensor(*args, **kwargs)

This layer performs tensor operation for two input. For example, each sample:

\[y_{i} = a * W_{i} * {b^\mathrm{T}}, i=0,1,...,K-1\]
In this formular:
  • \(a\): the first input contains M elements.
  • \(b\): the second input contains N elements.
  • \(y_{i}\): the i-th element of y.
  • \(W_{i}\): the i-th learned weight, shape if [M, N]
  • \(b^\mathrm{T}\): the transpose of \(b_{2}\).

The simple usage is:

tensor = tensor(a=layer1, b=layer2, size=1000)
Parameters:
  • name (basestring) – layer name
  • a (paddle.v2.config_base.Layer) – Input layer a.
  • b (paddle.v2.config_base.Layer) – input layer b.
  • size (int.) – the layer dimension.
  • act (paddle.v2.Activation.Base) – Activation Type. Default is tanh.
  • param_attr (paddle.v2.attr.ParameterAttribute) – The Parameter Attribute.
  • bias_attr (paddle.v2.attr.ParameterAttribute|None|Any) – The Bias Attribute. If no bias, then pass False or something not type of paddle.v2.attr.ParameterAttribute. None will get a default Bias.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.trans(*args, **kwargs)

A layer for transposing a minibatch matrix.

\[y = x^\mathrm{T}\]

where \(x\) is (M x N) input, and \(y\) is (N x M) output.

The example usage is:

trans = trans(input=layer)
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer.
  • name (basestring) – Layer name.
  • layer_attr (paddle.v2.attr.ExtraAttribute) – extra layer attributes.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.warp_ctc(*args, **kwargs)

A layer intergrating the open-source warp-ctc <https://github.com/baidu-research/warp-ctc> library, which is used in Deep Speech 2: End-toEnd Speech Recognition in English and Mandarin <https://arxiv.org/pdf/1512.02595v1.pdf>, to compute Connectionist Temporal Classification (CTC) loss.

More details of CTC can be found by referring to Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks

Note

  • Let num_classes represent the category number. Considering the ‘blank’ label needed by CTC, you need to use (num_classes + 1) as the input size. Thus, the size of both warp_ctc and ‘input’ layer should be set to num_classes + 1.
  • You can set ‘blank’ to any value ranged in [0, num_classes], which should be consistent as that used in your labels.
  • As a native ‘softmax’ activation is interated to the warp-ctc library, ‘linear’ activation is expected instead in the ‘input’ layer.

The simple usage:

ctc = warp_ctc(input=input,
                     label=label,
                     size=1001,
                     blank=1000,
                     norm_by_times=False)
Parameters:
  • input (paddle.v2.config_base.Layer) – The input layer.
  • label (paddle.v2.config_base.Layer) – The data layer of label with variable length.
  • size (int) – category numbers + 1.
  • name (basestring|None) – The name of this layer, which can not specify.
  • blank (int) – the ‘blank’ label used in ctc
  • norm_by_times (bool) – Whether to normalization by times. False by default.
  • layer_attr (paddle.v2.attr.ExtraAttributeNone) – Extra Layer config.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.layer.context_projection(**kwargs)

Context Projection.

It just simply reorganizes input sequence, combines “context_len” sequence to one context from context_start. “context_start” will be set to -(context_len - 1) / 2 by default. If context position out of sequence length, padding will be filled as zero if padding_attr = False, otherwise it is trainable.

For example, origin sequence is [A B C D E F G], context len is 3, then after context projection and not set padding_attr, sequence will be [ 0AB ABC BCD CDE DEF EFG FG0 ].

Parameters:
  • input (paddle.v2.config_base.Layer) – Input Sequence.
  • context_len (int) – context length.
  • context_start (int) – context start position. Default is -(context_len - 1)/2
  • padding_attr (bool|paddle.v2.attr.ParameterAttribute) – Padding Parameter Attribute. If false, it means padding always be zero. Otherwise Padding is learnable, and parameter attribute is set by this parameter.
Returns:

Projection

Return type:

Projection

class paddle.v2.layer.conv_projection(**kwargs)

Different from img_conv and conv_op, conv_projection is an Projection, which can be used in mixed and conat. It use cudnn to implement conv and only support GPU mode.

The example usage is:

proj = conv_projection(input=input1,
                       filter_size=3,
                       num_filters=64,
                       num_channels=64)
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer
  • filter_size (int) – The x dimension of a filter kernel.
  • filter_size_y (int) – The y dimension of a filter kernel. Since PaddlePaddle now supports rectangular filters, the filter’s shape can be (filter_size, filter_size_y).
  • num_filters (int) – channel of output data.
  • num_channels (int) – channel of input data.
  • stride (int) – The x dimension of the stride.
  • stride_y (int) – The y dimension of the stride.
  • padding (int) – The x dimension of padding.
  • padding_y (int) – The y dimension of padding.
  • groups (int) – The group number.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Convolution param attribute. None means default attribute
Returns:

A DotMulProjection Object.

Return type:

DotMulProjection

class paddle.v2.layer.dotmul_projection(**kwargs)

DotMulProjection with a layer as input. It performs element-wise multiplication with weight.

\[out.row[i] += in.row[i] .* weight\]

where \(.*\) means element-wise multiplication.

The example usage is:

proj = dotmul_projection(input=layer)
Parameters:
Returns:

A DotMulProjection Object.

Return type:

DotMulProjection

class paddle.v2.layer.full_matrix_projection(**kwargs)

Full Matrix Projection. It performs full matrix multiplication.

\[out.row[i] += in.row[i] * weight\]

There are two styles of usage.

  1. When used in mixed like this, you can only set the input:
with mixed(size=100) as m:
    m += full_matrix_projection(input=layer)
  1. When used as an independant object like this, you must set the size:
proj = full_matrix_projection(input=layer,
                              size=100,
                              param_attr=ParamAttr(name='_proj'))
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer
  • size (int) – The parameter size. Means the width of parameter.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter config, None if use default.
Returns:

A FullMatrixProjection Object.

Return type:

FullMatrixProjection

class paddle.v2.layer.identity_projection(**kwargs)
  1. IdentityProjection if offset=None. It performs:
\[out.row[i] += in.row[i]\]

The example usage is:

proj = identity_projection(input=layer)

2. IdentityOffsetProjection if offset!=None. It likes IdentityProjection, but layer size may be smaller than input size. It select dimesions [offset, offset+layer_size) from input:

\[out.row[i] += in.row[i + \textrm{offset}]\]

The example usage is:

proj = identity_projection(input=layer,
                           offset=10)

Note that both of two projections should not have any parameter.

Parameters:
  • input (paddle.v2.config_base.Layer) – Input Layer.
  • offset (int) – Offset, None if use default.
Returns:

A IdentityProjection or IdentityOffsetProjection object

Return type:

IdentityProjection or IdentityOffsetProjection

class paddle.v2.layer.scaling_projection(**kwargs)

scaling_projection multiplies the input with a scalar parameter and add to the output.

\[out += w * in\]

The example usage is:

proj = scaling_projection(input=layer)
Parameters:
Returns:

A ScalingProjection object

Return type:

ScalingProjection

class paddle.v2.layer.table_projection(**kwargs)

Table Projection. It selects rows from parameter where row_id is in input_ids.

\[out.row[i] += table.row[ids[i]]\]

where \(out\) is output, \(table\) is parameter, \(ids\) is input_ids, and \(i\) is row_id.

There are two styles of usage.

  1. When used in mixed like this, you can only set the input:
with mixed(size=100) as m:
    m += table_projection(input=layer)
  1. When used as an independant object like this, you must set the size:
proj = table_projection(input=layer,
                        size=100,
                        param_attr=ParamAttr(name='_proj'))
Parameters:
  • input (paddle.v2.config_base.Layer) – Input layer, which must contains id fields.
  • size (int) – The parameter size. Means the width of parameter.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter config, None if use default.
Returns:

A TableProjection Object.

Return type:

TableProjection

class paddle.v2.layer.trans_full_matrix_projection(**kwargs)

Different from full_matrix_projection, this projection performs matrix multiplication, using transpose of weight.

\[out.row[i] += in.row[i] * w^\mathrm{T}\]

\(w^\mathrm{T}\) means transpose of weight. The simply usage is:

proj = trans_full_matrix_projection(input=layer,
                                    size=100,
                                    param_attr=ParamAttr(
                                         name='_proj',
                                         initial_mean=0.0,
                                         initial_std=0.01))
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer
  • size (int) – The parameter size. Means the width of parameter.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter config, None if use default.
Returns:

A TransposedFullMatrixProjection Object.

Return type:

TransposedFullMatrixProjection

class paddle.v2.layer.conv_operator(**kwargs)

Different from img_conv, conv_op is an Operator, which can be used in mixed. And conv_op takes two inputs to perform convolution. The first input is the image and the second is filter kernel. It only support GPU mode.

The example usage is:

op = conv_operator(img=input1,
                   filter=input2,
                   filter_size=3,
                   num_filters=64,
                   num_channels=64)
Parameters:
  • img (paddle.v2.config_base.Layer) – input image
  • filter (paddle.v2.config_base.Layer) – input filter
  • filter_size (int) – The x dimension of a filter kernel.
  • filter_size_y (int) – The y dimension of a filter kernel. Since PaddlePaddle now supports rectangular filters, the filter’s shape can be (filter_size, filter_size_y).
  • num_filters (int) – channel of output data.
  • num_channels (int) – channel of input data.
  • stride (int) – The x dimension of the stride.
  • stride_y (int) – The y dimension of the stride.
  • padding (int) – The x dimension of padding.
  • padding_y (int) – The y dimension of padding.
Returns:

A ConvOperator Object.

Return type:

ConvOperator

class paddle.v2.layer.dotmul_operator(**kwargs)

DotMulOperator takes two inputs and performs element-wise multiplication:

\[out.row[i] += scale * (x.row[i] .* y.row[i])\]

where \(.*\) means element-wise multiplication, and scale is a config scalar, its default value is one.

The example usage is:

op = dotmul_operator(x=layer1, y=layer2, scale=0.5)
Parameters:
  • a (paddle.v2.config_base.Layer) – Input layer1
  • b (paddle.v2.config_base.Layer) – Input layer2
  • scale (float) – config scalar, default value is one.
Returns:

A DotMulOperator Object.

Return type:

DotMulOperator

Attributes

paddle.v2.attr.Param

alias of ParameterAttribute

paddle.v2.attr.Extra

alias of ExtraLayerAttribute

paddle.v2.attr.ParamAttr

alias of ParameterAttribute

paddle.v2.attr.ExtraAttr

alias of ExtraLayerAttribute

class paddle.v2.attr.ParameterAttribute(name=None, is_static=False, initial_std=None, initial_mean=None, initial_max=None, initial_min=None, l1_rate=None, l2_rate=None, learning_rate=None, momentum=None, gradient_clipping_threshold=None, sparse_update=False)

Parameter Attributes object. To fine-tuning network training process, user can set attribute to control training details, such as l1,l2 rate / learning rate / how to init param.

NOTE: IT IS A HIGH LEVEL USER INTERFACE.

Parameters:
  • is_static (bool) – True if this parameter will be fixed while training.
  • initial_std (float or None) – Gauss Random initialization standard deviation. None if not using Gauss Random initialize parameter.
  • initial_mean (float or None) – Gauss Random initialization mean. None if not using Gauss Random initialize parameter.
  • initial_max (float or None) – Uniform initialization max value.
  • initial_min (float or None) – Uniform initialization min value.
  • l1_rate (float or None) – the l1 regularization factor
  • l2_rate (float or None) – the l2 regularization factor
  • learning_rate (float or None) – The parameter learning rate. None means 1. The learning rate when optimize is LEARNING_RATE = GLOBAL_LEARNING_RATE * PARAMETER_LEARNING_RATE * SCHEDULER_FACTOR.
  • momentum (float or None) – The parameter momentum. None means use global value.
  • gradient_clipping_threshold (float) – gradient clipping threshold. If gradient value larger than some value, will be clipped.
  • sparse_update (bool) – Enable sparse update for this parameter. It will enable both local and remote sparse update.
set_default_parameter_name(name)

Set default parameter name. If parameter not set, then will use default parameter name.

Parameters:name (basestring) – default parameter name.
class paddle.v2.attr.ExtraLayerAttribute(error_clipping_threshold=None, drop_rate=None, device=None)

Some high level layer attributes config. You can set all attributes here, but some layer doesn’t support all attributes. If you set an attribute to a layer that not support this attribute, paddle will print an error and core.

Parameters:
  • error_clipping_threshold (float) – Error clipping threshold.
  • drop_rate (float) – Dropout rate. Dropout will create a mask on layer output. The dropout rate is the zero rate of this mask. The details of what dropout is please refer to here.
  • device (int) –

    device ID of layer. device=-1, use CPU. device>0, use GPU. The details allocation in parallel_nn please refer to here.

Activations

class paddle.v2.activation.Tanh

Tanh activation.

\[f(z)=tanh(z)=\frac{e^z-e^{-z}}{e^z+e^{-z}}\]
class paddle.v2.activation.Sigmoid

Sigmoid activation.

\[f(z) = \frac{1}{1+exp(-z)}\]
class paddle.v2.activation.Softmax

Softmax activation for simple input

\[P(y=j|x) = \frac{e^{x_j}} {\sum^K_{k=1} e^{x_j} }\]
paddle.v2.activation.Identity

alias of Linear

class paddle.v2.activation.Linear

Identity Activation.

Just do nothing for output both forward/backward.

class paddle.v2.activation.SequenceSoftmax

Softmax activation for one sequence. The dimension of input feature must be 1 and a sequence.

result = softmax(for each_feature_vector[0] in input_feature)
for i, each_time_step_output in enumerate(output):
    each_time_step_output = result[i]
class paddle.v2.activation.Exp

Exponential Activation.

\[f(z) = e^z.\]
class paddle.v2.activation.Relu

Relu activation.

forward. \(y = max(0, z)\)

derivative:

\[\begin{split}1 &\quad if z > 0 \\ 0 &\quad\mathrm{otherwize}\end{split}\]
class paddle.v2.activation.BRelu

BRelu Activation.

forward. \(y = min(24, max(0, z))\)

derivative:

\[\begin{split}1 &\quad if 0 < z < 24 \\ 0 &\quad \mathrm{otherwise}\end{split}\]
class paddle.v2.activation.SoftRelu

SoftRelu Activation.

class paddle.v2.activation.STanh

Scaled Tanh Activation.

\[f(z) = 1.7159 * tanh(2/3*z)\]
class paddle.v2.activation.Abs

Abs Activation.

Forward: \(f(z) = abs(z)\)

Derivative:

\[\begin{split}1 &\quad if \quad z > 0 \\ -1 &\quad if \quad z < 0 \\ 0 &\quad if \quad z = 0\end{split}\]
class paddle.v2.activation.Square

Square Activation.

\[f(z) = z^2.\]
class paddle.v2.activation.Base(name, support_hppl)

A mark for activation class. Each activation inherit BaseActivation, which has two parameters.

Parameters:
  • name (basestring) – activation name in paddle config.
  • support_hppl (bool) – True if supported by hppl. HPPL is a library used by paddle internally. Currently, lstm layer can only use activations supported by hppl.
class paddle.v2.activation.Log

Logarithm Activation.

\[f(z) = log(z)\]

Poolings

class paddle.v2.pooling.BasePool(name)

Base Pooling Type. Note these pooling types are used for sequence input, not for images. Each PoolingType contains one parameter:

Parameters:name (basestring) – pooling layer type name used by paddle.
class paddle.v2.pooling.Max(output_max_index=None)

Max pooling.

Return the very large values for each dimension in sequence or time steps.

\[max(samples\_of\_a\_sequence)\]
Parameters:output_max_index (bool|None) – True if output sequence max index instead of max value. None means use default value in proto.
class paddle.v2.pooling.Avg(strategy='average')

Average pooling.

Return the average values for each dimension in sequence or time steps.

\[sum(samples\_of\_a\_sequence)/sample\_num\]
class paddle.v2.pooling.CudnnMax

Cudnn max pooling only support GPU. Return the maxinum value in the pooling window.

class paddle.v2.pooling.CudnnAvg

Cudnn average pooling only support GPU. Return the average value in the pooling window.

class paddle.v2.pooling.Sum

Sum pooling.

Return the sum values of each dimension in sequence or time steps.

\[sum(samples\_of\_a\_sequence)\]
class paddle.v2.pooling.SquareRootN

Square Root Pooling.

Return the square root values of each dimension in sequence or time steps.

\[sum(samples\_of\_a\_sequence)/sqrt(sample\_num)\]

Networks

class paddle.v2.networks.sequence_conv_pool(*args, **kwargs)

Text convolution pooling layers helper.

Text input => Context Projection => FC Layer => Pooling => Output.

Parameters:
  • name (basestring) – name of output layer(pooling layer name)
  • input (paddle.v2.config_base.Layer) – name of input layer
  • context_len (int) – context projection length. See context_projection’s document.
  • hidden_size (int) – FC Layer size.
  • context_start (int or None) – context projection length. See context_projection’s context_start.
  • pool_type (BasePoolingType.) – pooling layer type. See pooling’s document.
  • context_proj_name (basestring) – context projection layer name. None if user don’t care.
  • context_proj_param_attr (paddle.v2.attr.ParameterAttribute or None.) – context projection parameter attribute. None if user don’t care.
  • fc_name (basestring) – fc layer name. None if user don’t care.
  • fc_param_attr (paddle.v2.attr.ParameterAttribute or None) – fc layer parameter attribute. None if user don’t care.
  • fc_bias_attr (paddle.v2.attr.ParameterAttribute or None) – fc bias parameter attribute. False if no bias, None if user don’t care.
  • fc_act (paddle.v2.Activation.Base) – fc layer activation type. None means tanh
  • pool_bias_attr (paddle.v2.attr.ParameterAttribute or None.) – pooling layer bias attr. None if don’t care. False if no bias.
  • fc_attr (paddle.v2.attr.ExtraAttribute) – fc layer extra attribute.
  • context_attr (paddle.v2.attr.ExtraAttribute) – context projection layer extra attribute.
  • pool_attr (paddle.v2.attr.ExtraAttribute) – pooling layer extra attribute.
Returns:

output layer name.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.simple_lstm(*args, **kwargs)

Simple LSTM Cell.

It just combine a mixed layer with fully_matrix_projection and a lstmemory layer. The simple lstm cell was implemented as follow equations.

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{xi}x_{t} + W_{hi}h_{t-1} + W_{ci}c_{t-1} + b_i)\\f_t & = \sigma(W_{xf}x_{t} + W_{hf}h_{t-1} + W_{cf}c_{t-1} + b_f)\\c_t & = f_tc_{t-1} + i_t tanh (W_{xc}x_t+W_{hc}h_{t-1} + b_c)\\o_t & = \sigma(W_{xo}x_{t} + W_{ho}h_{t-1} + W_{co}c_t + b_o)\\h_t & = o_t tanh(c_t)\end{aligned}\end{align} \]

Please refer Generating Sequences With Recurrent Neural Networks if you want to know what lstm is. Link is here.

Parameters:
  • name (basestring) – lstm layer name.
  • input (paddle.v2.config_base.Layer) – input layer name.
  • size (int) – lstm layer size.
  • reverse (bool) – whether to process the input data in a reverse order
  • mat_param_attr (paddle.v2.attr.ParameterAttribute) – mixed layer’s matrix projection parameter attribute.
  • bias_param_attr (paddle.v2.attr.ParameterAttribute|False) – bias parameter attribute. False means no bias, None means default bias.
  • inner_param_attr (paddle.v2.attr.ParameterAttribute) – lstm cell parameter attribute.
  • act (paddle.v2.Activation.Base) – lstm final activiation type
  • gate_act (paddle.v2.Activation.Base) – lstm gate activiation type
  • state_act (paddle.v2.Activation.Base) – lstm state activiation type.
  • mixed_attr (paddle.v2.attr.ExtraAttribute) – mixed layer’s extra attribute.
  • lstm_cell_attr (paddle.v2.attr.ExtraAttribute) – lstm layer’s extra attribute.
Returns:

lstm layer name.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.simple_img_conv_pool(*args, **kwargs)

Simple image convolution and pooling group.

Input => conv => pooling

Parameters:
  • name (basestring) – group name
  • input (paddle.v2.config_base.Layer) – input layer name.
  • filter_size (int) – see img_conv for details
  • num_filters (int) – see img_conv for details
  • pool_size (int) – see img_pool for details
  • pool_type (BasePoolingType) – see img_pool for details
  • act (paddle.v2.Activation.Base) – see img_conv for details
  • groups (int) – see img_conv for details
  • conv_stride (int) – see img_conv for details
  • conv_padding (int) – see img_conv for details
  • bias_attr (paddle.v2.attr.ParameterAttribute) – see img_conv for details
  • num_channel (int) – see img_conv for details
  • param_attr (paddle.v2.attr.ParameterAttribute) – see img_conv for details
  • shared_bias (bool) – see img_conv for details
  • conv_attr (paddle.v2.attr.ExtraAttribute) – see img_conv for details
  • pool_stride (int) – see img_pool for details
  • pool_padding (int) – see img_pool for details
  • pool_attr (paddle.v2.attr.ExtraAttribute) – see img_pool for details
Returns:

Layer’s output

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.img_conv_bn_pool(*args, **kwargs)

Convolution, batch normalization, pooling group.

Parameters:
  • name (basestring) – group name
  • input (paddle.v2.config_base.Layer) – layer’s input
  • filter_size (int) – see img_conv’s document
  • num_filters (int) – see img_conv’s document
  • pool_size (int) – see img_pool’s document.
  • pool_type (BasePoolingType) – see img_pool’s document.
  • act (paddle.v2.Activation.Base) – see batch_norm’s document.
  • groups (int) – see img_conv’s document
  • conv_stride (int) – see img_conv’s document.
  • conv_padding (int) – see img_conv’s document.
  • conv_bias_attr (paddle.v2.attr.ParameterAttribute) – see img_conv’s document.
  • num_channel (int) – see img_conv’s document.
  • conv_param_attr (paddle.v2.attr.ParameterAttribute) – see img_conv’s document.
  • shared_bias (bool) – see img_conv’s document.
  • conv_attr (Extrapaddle.v2.config_base.Layer) – see img_conv’s document.
  • bn_param_attr (paddle.v2.attr.ParameterAttribute.) – see batch_norm’s document.
  • bn_bias_attr – see batch_norm’s document.
  • bn_attr – paddle.v2.attr.ParameterAttribute.
  • pool_stride (int) – see img_pool’s document.
  • pool_padding (int) – see img_pool’s document.
  • pool_attr (paddle.v2.attr.ExtraAttribute) – see img_pool’s document.
Returns:

Layer groups output

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.dropout_layer(*args, **kwargs)

@TODO(yuyang18): Add comments.

Parameters:
  • name
  • input
  • dropout_rate
Returns:

class paddle.v2.networks.lstmemory_group(*args, **kwargs)

lstm_group is a recurrent layer group version of Long Short Term Memory. It does exactly the same calculation as the lstmemory layer (see lstmemory in layers.py for the maths) does. A promising benefit is that LSTM memory cell states, or hidden states in every time step are accessible to the user. This is especially useful in attention model. If you do not need to access the internal states of the lstm, but merely use its outputs, it is recommended to use the lstmemory, which is relatively faster than lstmemory_group.

NOTE: In PaddlePaddle’s implementation, the following input-to-hidden multiplications: \(W_{xi}x_{t}\) , \(W_{xf}x_{t}\), \(W_{xc}x_t\), \(W_{xo}x_{t}\) are not done in lstmemory_unit to speed up the calculations. Consequently, an additional mixed with full_matrix_projection must be included before lstmemory_unit is called.

The example usage is:

lstm_step = lstmemory_group(input=[layer1],
                            size=256,
                            act=paddle.v2.Activation.Tanh(),
                            gate_act=paddle.v2.Activation.Sigmoid(),
                            state_act=paddle.v2.Activation.Tanh())
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – lstmemory group name.
  • size (int) – lstmemory group size.
  • reverse (bool) – is lstm reversed
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter config, None if use default.
  • act (paddle.v2.Activation.Base) – lstm final activiation type
  • gate_act (paddle.v2.Activation.Base) – lstm gate activiation type
  • state_act (paddle.v2.Activation.Base) – lstm state activiation type.
  • mixed_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias parameter attribute of mixed layer. False means no bias, None means default bias.
  • lstm_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias parameter attribute of lstm layer. False means no bias, None means default bias.
  • mixed_attr (paddle.v2.attr.ExtraAttribute) – mixed layer’s extra attribute.
  • lstm_attr (paddle.v2.attr.ExtraAttribute) – lstm layer’s extra attribute.
  • get_output_attr (paddle.v2.attr.ExtraAttribute) – get output layer’s extra attribute.
Returns:

the lstmemory group.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.lstmemory_unit(*args, **kwargs)

Define calculations that a LSTM unit performs in a single time step. This function itself is not a recurrent layer, so that it can not be directly applied to sequence input. This function is always used in recurrent_group (see layers.py for more details) to implement attention mechanism.

Please refer to Generating Sequences With Recurrent Neural Networks for more details about LSTM. The link goes as follows: .. _Link: https://arxiv.org/abs/1308.0850

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{xi}x_{t} + W_{hi}h_{t-1} + W_{ci}c_{t-1} + b_i)\\f_t & = \sigma(W_{xf}x_{t} + W_{hf}h_{t-1} + W_{cf}c_{t-1} + b_f)\\c_t & = f_tc_{t-1} + i_t tanh (W_{xc}x_t+W_{hc}h_{t-1} + b_c)\\o_t & = \sigma(W_{xo}x_{t} + W_{ho}h_{t-1} + W_{co}c_t + b_o)\\h_t & = o_t tanh(c_t)\end{aligned}\end{align} \]

The example usage is:

lstm_step = lstmemory_unit(input=[layer1],
                           size=256,
                           act=paddle.v2.Activation.Tanh(),
                           gate_act=paddle.v2.Activation.Sigmoid(),
                           state_act=paddle.v2.Activation.Tanh())
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – lstmemory unit name.
  • size (int) – lstmemory unit size.
  • param_attr (paddle.v2.attr.ParameterAttribute) – Parameter config, None if use default.
  • act (paddle.v2.Activation.Base) – lstm final activiation type
  • gate_act (paddle.v2.Activation.Base) – lstm gate activiation type
  • state_act (paddle.v2.Activation.Base) – lstm state activiation type.
  • mixed_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias parameter attribute of mixed layer. False means no bias, None means default bias.
  • lstm_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias parameter attribute of lstm layer. False means no bias, None means default bias.
  • mixed_attr (paddle.v2.attr.ExtraAttribute) – mixed layer’s extra attribute.
  • lstm_attr (paddle.v2.attr.ExtraAttribute) – lstm layer’s extra attribute.
  • get_output_attr (paddle.v2.attr.ExtraAttribute) – get output layer’s extra attribute.
Returns:

lstmemory unit name.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.img_conv_group(**kwargs)

Image Convolution Group, Used for vgg net.

TODO(yuyang18): Complete docs

Parameters:
  • conv_batchnorm_drop_rate
  • input
  • conv_num_filter
  • pool_size
  • num_channels
  • conv_padding
  • conv_filter_size
  • conv_act
  • conv_with_batchnorm
  • pool_stride
  • pool_type
Returns:

class paddle.v2.networks.vgg_16_network(**kwargs)

Same model from https://gist.github.com/ksimonyan/211839e770f7b538e2d8

Parameters:
  • num_classes
  • input_image (paddle.v2.config_base.Layer) –
  • num_channels (int) –
Returns:

class paddle.v2.networks.gru_unit(*args, **kwargs)

Define calculations that a gated recurrent unit performs in a single time step. This function itself is not a recurrent layer, so that it can not be directly applied to sequence input. This function is almost always used in the recurrent_group (see layers.py for more details) to implement attention mechanism.

Please see grumemory in layers.py for the details about the maths.

Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – name of the gru group.
  • size (int) – hidden size of the gru.
  • act (paddle.v2.Activation.Base) – type of the activation
  • gate_act (paddle.v2.Activation.Base) – type of the gate activation
  • gru_attr (paddle.v2.attr.ParameterAttribute|False) – Extra parameter attribute of the gru layer.
Returns:

the gru output layer.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.gru_group(*args, **kwargs)

gru_group is a recurrent layer group version of Gated Recurrent Unit. It does exactly the same calculation as the grumemory layer does. A promising benefit is that gru hidden states are accessible to the user. This is especially useful in attention model. If you do not need to access any internal state, but merely use the outputs of a GRU, it is recommended to use the grumemory, which is relatively faster.

Please see grumemory in layers.py for more detail about the maths.

The example usage is:

gru = gur_group(input=[layer1],
                size=256,
                act=paddle.v2.Activation.Tanh(),
                gate_act=paddle.v2.Activation.Sigmoid())
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – name of the gru group.
  • size (int) – hidden size of the gru.
  • reverse (bool) – whether to process the input data in a reverse order
  • act (paddle.v2.Activation.Base) – type of the activiation
  • gate_act (paddle.v2.Activation.Base) – type of the gate activiation
  • gru_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias. False means no bias, None means default bias.
  • gru_attr (paddle.v2.attr.ParameterAttribute|False) – Extra parameter attribute of the gru layer.
Returns:

the gru group.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.simple_gru(*args, **kwargs)

You maybe see gru_step, grumemory in layers.py, gru_unit, gru_group, simple_gru in network.py. The reason why there are so many interfaces is that we have two ways to implement recurrent neural network. One way is to use one complete layer to implement rnn (including simple rnn, gru and lstm) with multiple time steps, such as recurrent, lstmemory, grumemory. But, the multiplication operation \(W x_t\) is not computed in these layers. See details in their interfaces in layers.py. The other implementation is to use an recurrent group which can ensemble a series of layers to compute rnn step by step. This way is flexible for attenion mechanism or other complex connections.

  • gru_step: only compute rnn by one step. It needs an memory as input and can be used in recurrent group.
  • gru_unit: a wrapper of gru_step with memory.
  • gru_group: a GRU cell implemented by a combination of multiple layers in recurrent group. But \(W x_t\) is not done in group.
  • gru_memory: a GRU cell implemented by one layer, which does same calculation with gru_group and is faster than gru_group.
  • simple_gru: a complete GRU implementation inlcuding \(W x_t\) and gru_group. \(W\) contains \(W_r\), \(W_z\) and \(W\), see formula in grumemory.

The computational speed is that, grumemory is relatively better than gru_group, and gru_group is relatively better than simple_gru.

The example usage is:

gru = simple_gru(input=[layer1], size=256)
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – name of the gru group.
  • size (int) – hidden size of the gru.
  • reverse (bool) – whether to process the input data in a reverse order
  • act (paddle.v2.Activation.Base) – type of the activiation
  • gate_act (paddle.v2.Activation.Base) – type of the gate activiation
  • gru_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias. False means no bias, None means default bias.
  • gru_attr (paddle.v2.attr.ParameterAttribute|False) – Extra parameter attribute of the gru layer.
Returns:

the gru group.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.simple_attention(*args, **kwargs)

Calculate and then return a context vector by attention machanism. Size of the context vector equals to size of the encoded_sequence.

\[ \begin{align}\begin{aligned}a(s_{i-1},h_{j}) & = v_{a}f(W_{a}s_{t-1} + U_{a}h_{j})\\e_{i,j} & = a(s_{i-1}, h_{j})\\a_{i,j} & = \frac{exp(e_{i,j})}{\sum_{k=1}^{T_x}{exp(e_{i,k})}}\\c_{i} & = \sum_{j=1}^{T_{x}}a_{i,j}h_{j}\end{aligned}\end{align} \]

where \(h_{j}\) is the jth element of encoded_sequence, \(U_{a}h_{j}\) is the jth element of encoded_proj \(s_{i-1}\) is decoder_state \(f\) is weight_act, and is set to tanh by default.

Please refer to Neural Machine Translation by Jointly Learning to Align and Translate for more details. The link is as follows: https://arxiv.org/abs/1409.0473.

The example usage is:

context = simple_attention(encoded_sequence=enc_seq,
                           encoded_proj=enc_proj,
                           decoder_state=decoder_prev,)
Parameters:
  • name (basestring) – name of the attention model.
  • softmax_param_attr (paddle.v2.attr.ParameterAttribute) – parameter attribute of sequence softmax that is used to produce attention weight
  • weight_act (Activation) – activation of the attention model
  • encoded_sequence (paddle.v2.config_base.Layer) – output of the encoder
  • encoded_proj (paddle.v2.config_base.Layer) – attention weight is computed by a feed forward neural network which has two inputs : decoder’s hidden state of previous time step and encoder’s output. encoded_proj is output of the feed-forward network for encoder’s output. Here we pre-compute it outside simple_attention for speed consideration.
  • decoder_state (paddle.v2.config_base.Layer) – hidden state of decoder in previous time step
  • transform_param_attr (paddle.v2.attr.ParameterAttribute) – parameter attribute of the feed-forward network that takes decoder_state as inputs to compute attention weight.
Returns:

a context vector

class paddle.v2.networks.simple_gru2(*args, **kwargs)

simple_gru2 is the same with simple_gru, but using grumemory instead Please see grumemory in layers.py for more detail about the maths. simple_gru2 is faster than simple_gru.

The example usage is:

gru = simple_gru2(input=[layer1], size=256)
Parameters:
  • input (paddle.v2.config_base.Layer) – input layer name.
  • name (basestring) – name of the gru group.
  • size (int) – hidden size of the gru.
  • reverse (bool) – whether to process the input data in a reverse order
  • act (paddle.v2.Activation.Base) – type of the activiation
  • gate_act (paddle.v2.Activation.Base) – type of the gate activiation
  • gru_bias_attr (paddle.v2.attr.ParameterAttribute|False) – bias. False means no bias, None means default bias.
  • gru_attr (paddle.v2.attr.ParameterAttribute|False) – Extra parameter attribute of the gru layer.
Returns:

the gru group.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.bidirectional_gru(*args, **kwargs)

A bidirectional_gru is a recurrent unit that iterates over the input sequence both in forward and bardward orders, and then concatenate two outputs to form a final output. However, concatenation of two outputs is not the only way to form the final output, you can also, for example, just add them together.

The example usage is:

bi_gru = bidirectional_gru(input=[input1], size=512)
Parameters:
  • name (basestring) – bidirectional gru layer name.
  • input (paddle.v2.config_base.Layer) – input layer.
  • size (int) – gru layer size.
  • return_seq (bool) – If set False, outputs of the last time step are concatenated and returned. If set True, the entire output sequences that are processed in forward and backward directions are concatenated and returned.
Returns:

paddle.v2.config_base.Layer object.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.text_conv_pool(*args, **kwargs)

Text convolution pooling layers helper.

Text input => Context Projection => FC Layer => Pooling => Output.

Parameters:
  • name (basestring) – name of output layer(pooling layer name)
  • input (paddle.v2.config_base.Layer) – name of input layer
  • context_len (int) – context projection length. See context_projection’s document.
  • hidden_size (int) – FC Layer size.
  • context_start (int or None) – context projection length. See context_projection’s context_start.
  • pool_type (BasePoolingType.) – pooling layer type. See pooling’s document.
  • context_proj_name (basestring) – context projection layer name. None if user don’t care.
  • context_proj_param_attr (paddle.v2.attr.ParameterAttribute or None.) – context projection parameter attribute. None if user don’t care.
  • fc_name (basestring) – fc layer name. None if user don’t care.
  • fc_param_attr (paddle.v2.attr.ParameterAttribute or None) – fc layer parameter attribute. None if user don’t care.
  • fc_bias_attr (paddle.v2.attr.ParameterAttribute or None) – fc bias parameter attribute. False if no bias, None if user don’t care.
  • fc_act (paddle.v2.Activation.Base) – fc layer activation type. None means tanh
  • pool_bias_attr (paddle.v2.attr.ParameterAttribute or None.) – pooling layer bias attr. None if don’t care. False if no bias.
  • fc_attr (paddle.v2.attr.ExtraAttribute) – fc layer extra attribute.
  • context_attr (paddle.v2.attr.ExtraAttribute) – context projection layer extra attribute.
  • pool_attr (paddle.v2.attr.ExtraAttribute) – pooling layer extra attribute.
Returns:

output layer name.

Return type:

paddle.v2.config_base.Layer

class paddle.v2.networks.bidirectional_lstm(*args, **kwargs)

A bidirectional_lstm is a recurrent unit that iterates over the input sequence both in forward and bardward orders, and then concatenate two outputs form a final output. However, concatenation of two outputs is not the only way to form the final output, you can also, for example, just add them together.

Please refer to Neural Machine Translation by Jointly Learning to Align and Translate for more details about the bidirectional lstm. The link goes as follows: .. _Link: https://arxiv.org/pdf/1409.0473v3.pdf

The example usage is:

bi_lstm = bidirectional_lstm(input=[input1], size=512)
Parameters:
  • name (basestring) – bidirectional lstm layer name.
  • input (paddle.v2.config_base.Layer) – input layer.
  • size (int) – lstm layer size.
  • return_seq (bool) – If set False, outputs of the last time step are concatenated and returned. If set True, the entire output sequences that are processed in forward and backward directions are concatenated and returned.
Returns:

paddle.v2.config_base.Layer object accroding to the return_seq.

Return type:

paddle.v2.config_base.Layer