layers

control_flow

split_lod_tensor

paddle.v2.fluid.layers.split_lod_tensor(input, mask, level=0)

split_lod_tensor

This function takes in an input that contains the complete lod information, and takes in a mask which is used to mask certain parts of the input. The output is the true branch and the false branch with the mask applied to the input at a certain level in the tensor.

Parameters:
  • input (tuple|list|None) – The input tensor that contains complete lod information needed to construct the output.
  • mask (list) – A bool column vector which masks the input.
  • level (int) – The specific lod level to rank.
Returns:

The true branch of tensor as per the mask applied to input. Variable: The false branch of tensor as per the mask applied to input.

Return type:

Variable

Examples

x = layers.data(name='x', shape=[1])
x.persistable = True

y = layers.data(name='y', shape=[1])
y.persistable = True

out_true, out_false = layers.split_lod_tensor(
      input=x, mask=y, level=level)

merge_lod_tensor

paddle.v2.fluid.layers.merge_lod_tensor(in_true, in_false, x, mask, level=0)

merge_lod_tensor

This function takes in an input \(x\), the True branch, the False branch and a binary \(mask\). Using this information, this function merges the True and False branches of the tensor into a single Output at a certain lod level indiacted by \(level\).

Parameters:
  • in_true (tuple|list|None) – The True branch to be merged.
  • in_false (tuple|list|None) – The False branch to be merged.
  • x (tuple|list|None) – The input tensor that contains complete lod information needed to construct the output.
  • mask (list) – A bool column vector which masks the input.
  • level (int) – The specific lod level to rank.
Returns:

The merged output tensor.

Return type:

Variable

Examples

x = layers.data(
            name='x', shape=[1], dtype='float32', stop_gradient=False)
y = layers.data(
      name='y', shape=[1], dtype='bool', stop_gradient=False)

level = 0

out_true, out_false = layers.split_lod_tensor(
      input=x, mask=y, level=level)
out = layers.merge_lod_tensor(
      in_true=out_true, in_false=out_false, mask=y, x=x, level=level)

BlockGuard

class paddle.v2.fluid.layers.BlockGuard(main_program)

BlockGuard class.

BlockGuard class is used to create a sub-block in a program by using the Python with keyword.

BlockGuardWithCompletion

class paddle.v2.fluid.layers.BlockGuardWithCompletion(rnn)

BlockGuardWithCompletion class.

BlockGuardWithCompletion class is used to create an op with a block in a program.

WhileGuard

class paddle.v2.fluid.layers.WhileGuard(while_op)

While

class paddle.v2.fluid.layers.While(cond, name=None)

lod_rank_table

paddle.v2.fluid.layers.lod_rank_table(x, level=0)

LoD Rank Table Operator. Given an input variable x and a level number of LoD, this layer creates a LodRankTable object. A LoDRankTable object contains a list of bi-element tuples. Each tuple consists of an index and a length, both of which are int type. Refering to specified level of LoD, the index is the sequence index number and the length representes the sequence length. Please note that the list is ranked in descending order by the length. The following is an example:

x is a LoDTensor:
    x.lod = [[0,                2, 3],
             [0,             5, 6, 7]]
    x.data = [a, b, c, d, e, f, g]

1. set level to 0:
    Create lod rank table:
        lod_rank_table_obj = lod_rank_table(x, level=0)

    Get:
        lod_rank_table_obj.items() = [(0, 2), (1, 1)]

2. set level to 1:
    Create lod rank table:
        lod_rank_table_obj = lod_rank_table(x, level=1)

    Get:
        lod_rank_table_obj.items() = [(0, 5), (1, 1), (2, 1)]
Parameters:
  • x (Variable) – Input variable, a LoDTensor based which to create the lod rank table.
  • level (int) – Specify the LoD level, on which to create the lod rank table.
Returns:

The created LoDRankTable object.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[10],
                dtype='float32', lod_level=1)
out = layers.lod_rank_table(x=x, level=0)

max_sequence_len

paddle.v2.fluid.layers.max_sequence_len(rank_table)

Max Sequence Len Operator. Given a LoDRankTable object, this layer returns the max length of a batch of sequences. In fact, a LoDRankTable object contains a list of tuples(<sequence index, sequence length>) and the list is already sorted by sequence length in descending order, so the operator just returns the sequence length of the first tuple element.

Parameters:rank_table (Variable) – Input variable which is a LoDRankTable object.
Returns:The max length of sequence.
Return type:Variable

Examples

x = fluid.layers.data(name='x', shape=[10],
                dtype='float32', lod_level=1)
rank_table = layers.lod_rank_table(x=x, level=0)
max_seq_len = layers.max_sequence_len(rank_table)

topk

paddle.v2.fluid.layers.topk(input, k)

topk

This function performs the operation that selects the k entries in the input vector and outputs their values and indices as vectors. Thus topk_out[j] is the j-th largest entry in input, and its index is topk_indices[j]

Parameters:
  • input (Variable|list) – The input tensor that has all the data.
  • k (int) – The number of top elements that the function will pick.
Returns:

The variable of type array that contains the k largest entries

from input.

Variable: The variable of type array that contains the indices of k

largest entries from input.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[10])
k = 5
array = fluid.layers.topk(x, k)

lod_tensor_to_array

paddle.v2.fluid.layers.lod_tensor_to_array(x, table)

Convert a LOD_TENSOR to an LOD_TENSOR_ARRAY.

Parameters:
  • x (Variable|list) – The LOD tensor to be converted to a LOD tensor array.
  • table (ParamAttr|list) – The variable that stores the level of lod which is ordered by sequence length in descending order.
Returns:

The variable of type array that has been converted from a

tensor.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[10])
table = fluid.layers.lod_rank_table(x, level=0)
array = fluid.layers.lod_tensor_to_array(x, table)

array_to_lod_tensor

paddle.v2.fluid.layers.array_to_lod_tensor(x, table)

Convert a LoD_Tensor_Aarry to an LoDTensor.

Parameters:
  • x (Variable|list) – The lod tensor array to be converted to a tensor.
  • table (ParamAttr|list) – The variable that stores the level of lod which is ordered by sequence length in descending order.
Returns:

The variable of type tensor that has been converted

from an array.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[10])
table = fluid.layers.lod_rank_table(x, level=0)
array = fluid.layers.lod_tensor_to_array(x, table)
lod_tensor = fluid.layers.array_to_lod_tensor(array, table)

increment

paddle.v2.fluid.layers.increment(x, value=1.0, in_place=True)

This function performs an operation that increments each value in the input \(x\) by an amount: \(value\) as mentioned in the input parameter. This operation is performed in-place by default.

Parameters:
  • x (Variable|list) – The tensor that has the input values.
  • value (float) – The amount by which the values should be incremented.
  • in_place (bool) – If the increment should be performed in-place.
Returns:

The tensor variable storing the transformation of

element-wise increment of each value in the input.

Return type:

Variable

Examples

data = fluid.layers.data(name='data', shape=[32, 32], dtype='float32')
data = fluid.layers.increment(x=data, value=3.0, in_place=True)

array_write

paddle.v2.fluid.layers.array_write(x, i, array=None)

This function writes the given input variable to the specified position indicating by the arrary index to an output LOD_TENSOR_ARRAY. If the output LOD_TENSOR_ARRAY is not given(None), a new one will be created and returned.

Parameters:
  • x (Variable|list) – The input tensor from which the data will be read.
  • i (Variable|list) – The index of the output LOD_TENSOR_ARRAY, pointing to the position to which the input tensor will be written.
  • array (Variable|list) – The output LOD_TENSOR_ARRAY to which the input tensor will be written. If this parameter is NONE, a new LOD_TENSOR_ARRAY will be created and returned.
Returns:

The output LOD_TENSOR_ARRAY where the input tensor is written.

Return type:

Variable

Examples

create_array

paddle.v2.fluid.layers.create_array(dtype)

This function creates an array of type \(LOD_TENSOR_ARRAY\) using the LayerHelper.

Parameters:dtype (int|float) – The data type of the elements in the array.
Returns:The tensor variable storing the elements of data type.
Return type:Variable

Examples

data = fluid.layers.create_array(dtype='float32')

less_than

paddle.v2.fluid.layers.less_than(x, y, cond=None, **ignored)

Less than

This layer returns the truth value of \(x < y\) elementwise.

Parameters:
  • x (Variable) – First operand of less_than
  • y (Variable) – Second operand of less_than
  • cond (Variable|None) – Optional output variable to store the result of less_than
Returns:

The tensor variable storing the output of less_than.

Return type:

Variable

Examples

less = fluid.layers.less_than(x=label, y=limit)

array_read

paddle.v2.fluid.layers.array_read(array, i)

This function performs the operation to read the data in as an LOD_TENSOR_ARRAY. :param array: The input tensor that will be written to an array. :type array: Variable|list :param i: The subscript index in tensor array, that points the

place where data will be written to.
Returns:The tensor type variable that has the data written to it.
Return type:Variable

Examples

shrink_memory

paddle.v2.fluid.layers.shrink_memory(x, i, table)

This function creates an operator to shrink_rnn_memory using the RankTable as mentioned in the input parameter.

array_length

paddle.v2.fluid.layers.array_length(array)

This function performs the operation to find the length of the input LOD_TENSOR_ARRAY.

Parameters:array (LOD_TENSOR_ARRAY) – The input array that will be used to compute the length.
Returns:The length of the input LoDTensorArray.
Return type:Variable

Examples

IfElse

class paddle.v2.fluid.layers.IfElse(cond, name=None)

DynamicRNN

class paddle.v2.fluid.layers.DynamicRNN(name=None)

ConditionalBlock

class paddle.v2.fluid.layers.ConditionalBlock(inputs, is_scalar_condition=False, name=None)

StaticRNN

class paddle.v2.fluid.layers.StaticRNN(name=None)

StaticRNN class.

StaticRNN class is used to create a StaticRNN. The RNN will have its own parameters like inputs, outputs, memories, status and length.

memory(init=None, shape=None, batch_ref=None, init_value=0.0, init_batch_dim_idx=0, ref_batch_dim_idx=1)
Parameters:
  • init – boot memory, if not set, a shape, batch_ref must be provided
  • shape – shape of the boot memory
  • batch_ref – batch size reference variable
  • init_value – the init value of boot memory
  • init_batch_dim_idx – the index of batch size in init’s dimension
  • ref_batch_dim_idx – the index of batch size in batch_ref’s dimension

reorder_lod_tensor_by_rank

paddle.v2.fluid.layers.reorder_lod_tensor_by_rank(x, rank_table)

ReorderLoDTensorByRankTable operator.

Input(X) is a batch of sequences. Input(RankTable) stores new orders of the input sequence batch. The reorder_lod_tensor_by_rank operator reorders the Input(X) according to the information provided by Input(RankTable).

For example:

If the indices stored in the Input(RankTable) are [3, 0, 2, 1], the Input(X) will be reordered that the fourth sequence in Input(X) will become the first one, and then followed by the original first, third, and the second one.

This is: X = [Seq0, Seq1, Seq2, Seq3]. The indices in RankTable are [3, 0, 2, 1]. Out = [Seq3, Seq0, Seq2, Seq1] with a new LoD information.

If the LoD information of Input(X) is empty, this means Input(X) is not sequence data. This is also identical to a batch of sequences where each sequence has a fixed length 1. In this case, the reorder_lod_tensor_by_rank operator reorders each slice of Input(X) along the first axis according to Input(RankTable).

This is: X = [Slice0, Slice1, Slice2, Slice3] and its LoD information is empty. The indices in RankTable are [3, 0, 2, 1]. Out = [Slice3, Slice0, Slice2, Slice1] with no LoD information is appended.

NOTE: This operator sorts Input(X) according to a given LoDRankTable which does not need to be calculated according to Input(X). It can be calculated according to another different sequence, and then this operator sorts Input(X) according to the given LoDRankTable.

Parameters:
  • x – (LoDTensor), the input lod tensor to be reordered according to Input(RankTable). Duplicable: False Optional: False
  • rank_table – (LoDRankTable), the rank table according to which Input(X) is reordered. Duplicable: False Optional: False
Returns:

(LoDTensor), the reordered lod tensor.

ParallelDo

class paddle.v2.fluid.layers.ParallelDo(places, use_nccl=False, name=None)

ParallelDo class.

ParallelDo class is used to create a ParallelDo.

Print

paddle.v2.fluid.layers.Print(input, first_n=-1, message=None, summarize=-1, print_tensor_name=True, print_tensor_type=True, print_tensor_shape=True, print_tensor_lod=True, print_phase='both')

Print operator

This creates a print op that will print when a tensor is accessed.

Wraps the tensor passed in so that whenever that a tensor is accessed, the message message is printed, along with the current value of the tensor t.

Parameters:
  • input (Variable) – A Tensor to print.
  • summarize (int) – Print this number of elements in the tensor, will print all if left is negative.
  • message (str) – A string message to print as a prefix.
  • first_n (int) – Only log first_n number of times.
  • print_tensor_name (bool) – Print the tensor name.
  • print_tensor_type (bool) – Print the tensor type.
  • print_tensor_shape (bool) – Print the tensor shape.
  • print_tensor_lod (bool) – Print the tensor lod.
  • print_phase (str) – Which phase to displace, including ‘forward’, ‘backward’ and ‘both’. If set to ‘backward’ or ‘both’, will print the gradients of input tensor.
Returns:

Output tensor, same data with input tensor.

Return type:

Variable

Examples


value = some_layer(...) Print(value, summarize=10,

message=”The content of some_layer: ”)

device

get_places

paddle.v2.fluid.layers.get_places(device_count=None, device_type=None)

Returns a list of places based on flags. The list will be used for parallel execution.

Parameters:
  • device_count (INT) – device count
  • device_type (STRING) – device type
Returns:

vector of Place

io

data

paddle.v2.fluid.layers.data(name, shape, append_batch_size=True, dtype='float32', lod_level=0, type=VarType.LOD_TENSOR, stop_gradient=True)

Data Layer

This function takes in the input and based on whether data has to be returned back as a minibatch, it creates the global variable by using the helper functions. The global variables can be accessed by all the following operators in the graph.

All the input variables of this function are passed in as local variables to the LayerHelper constructor.

Parameters:
  • name (str) – The name/alias of the function
  • shape (list) – Tuple declaring the shape.
  • append_batch_size (bool) – Whether or not to append the data as a batch.
  • dtype (int|float) – The type of data : float32, float_16, int etc
  • type (VarType) – The output type. By default it is LOD_TENSOR.
  • lod_level (int) – The LoD Level. 0 means the input data is not a sequence.
  • main_program (Program) – Name of the main program that calls this
  • startup_program (Program) – Name of the startup program
  • stop_gradient (bool) – A boolean that mentions whether gradient should flow.
Returns:

The global variable that gives access to the data.

Return type:

Variable

Examples

data = fluid.layers.data(name='x', shape=[784], dtype='float32')

BlockGuardServ

class paddle.v2.fluid.layers.BlockGuardServ(server)

BlockGuardServ class.

BlockGuardServ class is used to create an op with a block in a program.

ListenAndServ

class paddle.v2.fluid.layers.ListenAndServ(endpoint, fan_in=1, optimizer_mode=True)

ListenAndServ class.

ListenAndServ class is used to wrap listen_and_serv op to create a server which can receive variables from clients and run a block.

Send

paddle.v2.fluid.layers.Send(endpoints, send_vars, get_vars)

Send layer

Parameters:
  • endpoints – comma seperated IP:PORT pairs in the order of send_vars to send
  • send_vars – vars to send
  • get_vars – vars to get from server after send completes.

Send variables to the server side, and get vars from server side when server have finished running server side program.

nn

fc

paddle.v2.fluid.layers.fc(input, size, num_flatten_dims=1, param_attr=None, bias_attr=None, act=None, name=None)

Fully Connected Layer

The fully connected layer can take multiple tensors as its inputs. It creates a variable (one for each input tensor) called weights for each input tensor, which represents a fully connected weight matrix from each input unit to each output unit. The fully connected layer multiplies each input tensor with its coresponding weight to produce an output Tensor. If multiple input tensors are given, the results of multiple multiplications will be sumed up. If bias_attr is not None, a biases variable will be created and added to the output. Finally, if activation is not None, it will be applied to the output as well.

This process can be formulated as follows:

\[Out = Act({\sum_{i=0}^{N-1}X_iW_i + b})\]

In the above equation:

  • \(N\): Number of the input.
  • \(X_i\): The input tensor.
  • \(W\): The weights created by this layer.
  • \(b\): The bias parameter created by this layer (if needed).
  • \(Act\): The activation function.
  • \(Out\): The output tensor.
Parameters:
  • input (Variable|list) – The input tensor(s) to the fully connected layer.
  • size (int) – The number of output units in the fully connected layer.
  • num_flatten_dims (int) – The fc layer can accept an input tensor with more than two dimensions. If this happens, the multidimensional tensor will first be flattened into a 2-dimensional matrix. The parameter num_flatten_dims determines how the input tensor is flattened: the first num_flatten_dims (inclusive, index starts from 1) dimensions will be flatten to form the first dimension of the final matrix (height of the matrix), and the rest rank(X) - num_flatten_dims dimensions are flattened to form the second dimension of the final matrix (width of the matrix). For example, suppose X is a 6-dimensional tensor with a shape [2, 3, 4, 5, 6], and num_flatten_dims = 3. Then, the flattened matrix will have a shape [2 x 3 x 4, 5 x 6] = [24, 30]. By default, num_flatten_dims is set to 1.
  • param_attr (ParamAttr|list) – The parameter attribute for learnable parameters/weights of the fully connected layer.
  • param_initializer (ParamAttr|list) – The initializer used for the weight/parameter. If set None, XavierInitializer() will be used.
  • bias_attr (ParamAttr|list) – The parameter attribute for the bias parameter for this layer. If set None, no bias will be added to the output units.
  • bias_initializer (ParamAttr|list) – The initializer used for the bias. If set None, then ConstantInitializer() will be used.
  • act (str) – Activation to be applied to the output of the fully connected layer.
  • name (str) – Name/alias of the fully connected layer.
Returns:

The output tensor variable.

Return type:

Variable

Raises:

ValueError – If rank of the input tensor is less than 2.

Examples

data = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
fc = fluid.layers.fc(input=data, size=1000, act="tanh")

embedding

paddle.v2.fluid.layers.embedding(input, size, is_sparse=False, padding_idx=None, param_attr=None, dtype='float32')

Embedding Layer

This layer is used to lookup embeddings of IDs, provided by input, in a lookup table. The result of this lookup is the embedding of each ID in the input.

All the input variables are passed in as local variables to the LayerHelper constructor.

Parameters:
  • input (Variable) – The tensor variable containing the IDs.
  • size (tuple|list) – The shape of the look up table parameter. It should have two elements which indicate the size of the dictionary of embeddings and the size of each embedding vector respectively.
  • is_sparse (bool) – The flag indicating whether to use sparse update.
  • padding_idx (int|long|None) – If None, it makes no effect to lookup. Otherwise the given padding_idx indicates padding the output with zeros whenever lookup encounters it in input. If \(padding_idx < 0\), the padding_idx to use in lookup is \(size[0] + dim\).
  • param_attr (ParamAttr) – Parameters for this layer
  • dtype (np.dtype|core.VarDesc.VarType|str) – The type of data : float32, float_16, int etc
Returns:

The tensor variable storing the embeddings of the supplied inputs.

Return type:

Variable

Examples

dict_size = len(dataset.ids)
data = fluid.layers.data(name='ids', shape=[32, 32], dtype='float32')
fc = fluid.layers.embedding(input=data, size=[dict_size, 16])

dynamic_lstm

paddle.v2.fluid.layers.dynamic_lstm(input, size, param_attr=None, bias_attr=None, use_peepholes=True, is_reverse=False, gate_activation='sigmoid', cell_activation='tanh', candidate_activation='tanh', dtype='float32', name=None)

Dynamic LSTM Layer

The defalut implementation is diagonal/peephole connection (https://arxiv.org/pdf/1402.1128.pdf), the formula is as follows:

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{ix}x_{t} + W_{ih}h_{t-1} + W_{ic}c_{t-1} + b_i)\\f_t & = \sigma(W_{fx}x_{t} + W_{fh}h_{t-1} + W_{fc}c_{t-1} + b_f)\\\tilde{c_t} & = act_g(W_{cx}x_t + W_{ch}h_{t-1} + b_c)\\o_t & = \sigma(W_{ox}x_{t} + W_{oh}h_{t-1} + W_{oc}c_t + b_o)\\c_t & = f_t \odot c_{t-1} + i_t \odot \tilde{c_t}\\h_t & = o_t \odot act_h(c_t)\end{aligned}\end{align} \]

where the \(W\) terms denote weight matrices (e.g. \(W_{xi}\) is the matrix of weights from the input gate to the input), \(W_{ic}, W_{fc}, W_{oc}\) are diagonal weight matrices for peephole connections. In our implementation, we use vectors to reprenset these diagonal weight matrices. The \(b\) terms denote bias vectors (\(b_i\) is the input gate bias vector), \(\sigma\) is the non-linear activations, such as logistic sigmoid function, and \(i, f, o\) and \(c\) are the input gate, forget gate, output gate, and cell activation vectors, respectively, all of which have the same size as the cell output activation vector \(h\).

The \(\odot\) is the element-wise product of the vectors. \(act_g\) and \(act_h\) are the cell input and cell output activation functions and tanh is usually used for them. \(\tilde{c_t}\) is also called candidate hidden state, which is computed based on the current input and the previous hidden state.

Set use_peepholes to False to disable peephole connection. The formula is omitted here, please refer to the paper http://www.bioinf.jku.at/publications/older/2604.pdf for details.

Note that these \(W_{xi}x_{t}, W_{xf}x_{t}, W_{xc}x_{t}, W_{xo}x_{t}\) operations on the input \(x_{t}\) are NOT included in this operator. Users can choose to use fully-connect layer before LSTM layer.

Parameters:
  • input (Variable) – The input of dynamic_lstm layer, which supports variable-time length input sequence. The underlying tensor in this Variable is a matrix with shape (T X 4D), where T is the total time steps in this mini-batch, D is the hidden size.
  • size (int) – 4 * hidden size.
  • param_attr (ParamAttr|None) –

    The parameter attribute for the learnable hidden-hidden weights.

    • Weights = {\(W_{ch}, W_{ih}, W_{fh}, W_{oh}\)}
    • The shape is (D x 4D), where D is the hidden size.
  • bias_attr (ParamAttr|None) –

    The bias attribute for the learnable bias weights, which contains two parts, input-hidden bias weights and peephole connections weights if setting use_peepholes to True.

    1. use_peepholes = False
    • Biases = {\(b_c, b_i, b_f, b_o\)}.
    • The shape is (1 x 4D).
    1. use_peepholes = True
    • Biases = { \(b_c, b_i, b_f, b_o, W_{ic}, W_{fc}, W_{oc}\)}.
    • The shape is (1 x 7D).
  • use_peepholes (bool) – Whether to enable diagonal/peephole connections, default True.
  • is_reverse (bool) – Whether to compute reversed LSTM, default False.
  • gate_activation (str) – The activation for input gate, forget gate and output gate. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “sigmoid”.
  • cell_activation (str) – The activation for cell output. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
  • candidate_activation (str) – The activation for candidate hidden state. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
  • dtype (str) – Data type. Choices = [“float32”, “float64”], default “float32”.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The hidden state, and cell state of LSTM. The shape of both is (T x D), and lod is the same with the input.

Return type:

tuple

Examples

hidden_dim = 512
forward_proj = fluid.layers.fc(input=input_seq, size=hidden_dim * 4,
                               act=None, bias_attr=None)
forward, _ = fluid.layers.dynamic_lstm(
    input=forward_proj, size=hidden_dim * 4, use_peepholes=False)

dynamic_lstmp

paddle.v2.fluid.layers.dynamic_lstmp(input, size, proj_size, param_attr=None, bias_attr=None, use_peepholes=True, is_reverse=False, gate_activation='sigmoid', cell_activation='tanh', candidate_activation='tanh', proj_activation='tanh', dtype='float32', name=None)

Dynamic LSTMP Layer

LSTMP (LSTM with recurrent projection) layer has a separate projection layer after the LSTM layer, projecting the original hidden state to a lower-dimensional one, which is proposed to reduce the number of total parameters and furthermore computational complexity for the LSTM, espeacially for the case that the size of output units is relative large (https://research.google.com/pubs/archive/43905.pdf).

The formula is as follows:

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{ix}x_{t} + W_{ir}r_{t-1} + W_{ic}c_{t-1} + b_i)\\f_t & = \sigma(W_{fx}x_{t} + W_{fr}r_{t-1} + W_{fc}c_{t-1} + b_f)\\\tilde{c_t} & = act_g(W_{cx}x_t + W_{cr}r_{t-1} + b_c)\\o_t & = \sigma(W_{ox}x_{t} + W_{or}r_{t-1} + W_{oc}c_t + b_o)\\c_t & = f_t \odot c_{t-1} + i_t \odot \tilde{c_t}\\h_t & = o_t \odot act_h(c_t)\\r_t & = \overline{act_h}(W_{rh}h_t)\end{aligned}\end{align} \]

In the above formula:

  • \(W\): Denotes weight matrices (e.g. \(W_{xi}\) is the matrix of weights from the input gate to the input).
  • \(W_{ic}\), \(W_{fc}\), \(W_{oc}\): Diagonal weight matrices for peephole connections. In our implementation, we use vectors to reprenset these diagonal weight matrices.
  • \(b\): Denotes bias vectors (e.g. \(b_i\) is the input gate bias vector).
  • \(\sigma\): The activation, such as logistic sigmoid function.
  • \(i, f, o\) and \(c\): The input gate, forget gate, output gate, and cell activation vectors, respectively, all of which have the same size as the cell output activation vector \(h\).
  • \(h\): The hidden state.
  • \(r\): The recurrent projection of the hidden state.
  • \(\tilde{c_t}\): The candidate hidden state, whose computation is based on the current input and previous hidden state.
  • \(\odot\): The element-wise product of the vectors.
  • \(act_g\) and \(act_h\): The cell input and cell output activation functions and tanh is usually used for them.
  • \(\overline{act_h}\): The activation function for the projection output, usually using identity or same as \(act_h\).

Set use_peepholes to False to disable peephole connection. The formula is omitted here, please refer to the paper http://www.bioinf.jku.at/publications/older/2604.pdf for details.

Note that these \(W_{xi}x_{t}, W_{xf}x_{t}, W_{xc}x_{t}, W_{xo}x_{t}\) operations on the input \(x_{t}\) are NOT included in this operator. Users can choose to use fully-connected layer before LSTMP layer.

Parameters:
  • input (Variable) – The input of dynamic_lstmp layer, which supports variable-time length input sequence. The underlying tensor in this Variable is a matrix with shape (T X 4D), where T is the total time steps in this mini-batch, D is the hidden size.
  • size (int) – 4 * hidden size.
  • proj_size (int) – The size of projection output.
  • param_attr (ParamAttr|None) –

    The parameter attribute for the learnable hidden-hidden weight and projection weight.

    • Hidden-hidden weight = {\(W_{ch}, W_{ih}, W_{fh}, W_{oh}\)}.
    • The shape of hidden-hidden weight is (P x 4D), where P is the projection size and D the hidden size.
    • Projection weight = {\(W_{rh}\)}.
    • The shape of projection weight is (D x P).
  • bias_attr (ParamAttr|None) –

    The bias attribute for the learnable bias weights, which contains two parts, input-hidden bias weights and peephole connections weights if setting use_peepholes to True.

    1. use_peepholes = False
    • Biases = {\(b_c, b_i, b_f, b_o\)}.
    • The shape is (1 x 4D).
    1. use_peepholes = True
    • Biases = { \(b_c, b_i, b_f, b_o, W_{ic}, W_{fc}, W_{oc}\)}.
    • The shape is (1 x 7D).
  • use_peepholes (bool) – Whether to enable diagonal/peephole connections, default True.
  • is_reverse (bool) – Whether to compute reversed LSTM, default False.
  • gate_activation (str) – The activation for input gate, forget gate and output gate. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “sigmoid”.
  • cell_activation (str) – The activation for cell output. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
  • candidate_activation (str) – The activation for candidate hidden state. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
  • proj_activation (str) – The activation for projection output. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
  • dtype (str) – Data type. Choices = [“float32”, “float64”], default “float32”.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The projection of hidden state, and cell state of LSTMP. The shape of projection is (T x P), for the cell state which is (T x D), and both LoD is the same with the input.

Return type:

tuple

Examples

hidden_dim, proj_dim = 512, 256
fc_out = fluid.layers.fc(input=input_seq, size=hidden_dim * 4,
                         act=None, bias_attr=None)
proj_out, _ = fluid.layers.dynamic_lstmp(input=fc_out,
                                         size=hidden_dim * 4,
                                         proj_size=proj_dim,
                                         use_peepholes=False,
                                         is_reverse=True,
                                         cell_activation="tanh",
                                         proj_activation="tanh")

dynamic_gru

paddle.v2.fluid.layers.dynamic_gru(input, size, param_attr=None, bias_attr=None, is_reverse=False, gate_activation='sigmoid', candidate_activation='tanh', h_0=None)

Dynamic GRU Layer

Refer to Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling

The formula is as follows:

\[ \begin{align}\begin{aligned}u_t & = act_g(W_{ux}x_{t} + W_{uh}h_{t-1} + b_u)\\r_t & = act_g(W_{rx}x_{t} + W_{rh}h_{t-1} + b_r)\\\tilde{h_t} & = act_c(W_{cx}x_{t} + W_{ch}(r_t \odot h_{t-1}) + b_c)\\h_t & = (1-u_t) \odot h_{t-1} + u_t \odot \tilde{h_t}\end{aligned}\end{align} \]

The \(\odot\) is the element-wise product of the vectors. \(act_g\) is the update gate and reset gate activation function and \(sigmoid\) is usually used for it. \(act_c\) is the activation function for candidate hidden state and \(tanh\) is usually used for it.

Note that these \(W_{ux}x_{t}, W_{rx}x_{t}, W_{cx}x_{t}\) operations on the input \(x_{t}\) are NOT included in this operator. Users can choose to use fully-connect layer before GRU layer.

Parameters:
  • input (Variable) – The input of dynamic_gru layer, which supports variable-time length input sequence. The underlying tensor in this Variable is a matrix with shape \((T \times 3D)\), where \(T\) is the total time steps in this mini-batch, \(D\) is the hidden size.
  • size (int) – The dimension of the gru cell.
  • param_attr (ParamAttr|None) –

    The parameter attribute for the learnable hidden-hidden weight matrix. Note:

    • The shape of the weight matrix is \((T \times 3D)\), where \(D\) is the hidden size.
    • All elements in the weight matrix can be divided into two parts. The first part are weights of the update gate and reset gate with shape \((D \times 2D)\), and the second part are weights for candidate hidden state with shape \((D \times D)\).
  • bias_attr (ParamAttr) – The parameter attribute for learnable the hidden-hidden bias.
  • is_reverse (bool) – Whether to compute reversed GRU, default False.
  • gate_activation (str) – The activation for update gate and reset gate. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “sigmoid”.
  • activation (str) – The activation for candidate hidden state. Choices = [“sigmoid”, “tanh”, “relu”, “identity”], default “tanh”.
Returns:

The hidden state of GRU. The shape is \((T \times D)\), and lod is the same with the input.

Return type:

Variable

Examples

hidden_dim = 512
x = fluid.layers.fc(input=data, size=hidden_dim * 3)
hidden = fluid.layers.dynamic_gru(input=x, dim=hidden_dim)

gru_unit

paddle.v2.fluid.layers.gru_unit(input, hidden, size, weight=None, bias=None, activation='tanh', gate_activation='sigmoid')

GRU unit layer. The equation of a gru step is:

\[ \begin{align}\begin{aligned}u_t & = actGate(xu_{t} + W_u h_{t-1} + b_u)\\r_t & = actGate(xr_{t} + W_r h_{t-1} + b_r)\\m_t & = actNode(xm_t + W_c dot(r_t, h_{t-1}) + b_m)\\h_t & = dot((1-u_t), m_t) + dot(u_t, h_{t-1})\end{aligned}\end{align} \]

The inputs of gru unit includes \(z_t\), \(h_{t-1}\). In terms of the equation above, the \(z_t\) is split into 3 parts - \(xu_t\), \(xr_t\) and \(xm_t\). This means that in order to implement a full GRU unit operator for an input, a fully connected layer has to be applied, such that \(z_t = W_{fc}x_t\).

The terms \(u_t\) and \(r_t\) represent the update and reset gates of the GRU cell. Unlike LSTM, GRU has one lesser gate. However, there is an intermediate candidate hidden output, which is denoted by \(m_t\). This layer has three outputs \(h_t\), \(dot(r_t, h_{t-1})\) and concatenation of \(u_t\), \(r_t\) and \(m_t\).

Parameters:
  • input (Variable) – The fc transformed input value of current step.
  • hidden (Variable) – The hidden value of lstm unit from previous step.
  • size (integer) – The input dimension value.
  • weight (ParamAttr) – The weight parameters for gru unit. Default: None
  • bias (ParamAttr) – The bias parameters for gru unit. Default: None
  • activation (string) – The activation type for cell (actNode). Default: ‘tanh’
  • gate_activation (string) – The activation type for gates (actGate). Default: ‘sigmoid’
Returns:

The hidden value, reset-hidden value and gate values.

Return type:

tuple

Examples

# assuming we have x_t_data and prev_hidden of size=10
x_t = fluid.layers.fc(input=x_t_data, size=30)
hidden_val, r_h_val, gate_val = fluid.layers.gru_unit(input=x_t,
                                       hidden = prev_hidden)

linear_chain_crf

paddle.v2.fluid.layers.linear_chain_crf(input, label, param_attr=None)

crf_decoding

paddle.v2.fluid.layers.crf_decoding(input, param_attr, label=None)

cos_sim

paddle.v2.fluid.layers.cos_sim(X, Y)

This function performs the cosine similarity between two tensors X and Y and returns that as the output.

cross_entropy

paddle.v2.fluid.layers.cross_entropy(input, label, soft_label=False)

Cross Entropy Layer

This layer computes the cross entropy between input and label. It supports both standard cross-entropy and soft-label cross-entropy loss computation.

  1. One-hot cross-entropy:

    soft_label = False, Label[i, 0] indicates the class index for sample i:

    \[Y[i] = -\log(X[i, Label[i]])\]
  2. Soft-label cross-entropy:

    soft_label = True, Label[i, j] indicates the soft label of class j for sample i:

    \[Y[i] = \sum_j{-Label[i, j] * log(X[i, j])}\]

    Please make sure that in this case the summation of each row of label equals one.

  3. One-hot cross-entropy with vecterized label:

    As a special case of 2), when each row of ‘label’ has only one non-zero element which is equal to 1, soft-label cross-entropy degenerates to a one-hot cross-entropy with one-hot label representation.

Parameters:
  • input (Variable|list) – a 2-D tensor with shape [N x D], where N is the batch size and D is the number of classes. This input is a probability computed by the previous operator, which is almost always the result of a softmax operator.
  • label (Variable|list) – the ground truth which is a 2-D tensor. When soft_label is set to False, label is a tensor<int64> with shape [N x 1]. When soft_label is set to True, label is a tensor<float/double> with shape [N x D].
  • soft_label (bool) – a flag indicating whether to interpretate the given labels as soft labels, default False.
Returns:

A 2-D tensor with shape [N x 1], the cross entropy loss.

Raises:

ValueError – 1) the 1st dimension of input and label are not equal. 2) when soft_label == True, and the 2nd dimension of

input and label are not equal.

  1. when soft_label == False, and the 2nd dimension of label is not 1.

Examples

predict = fluid.layers.fc(input=net, size=classdim, act='softmax')
cost = fluid.layers.cross_entropy(input=predict, label=label)

square_error_cost

paddle.v2.fluid.layers.square_error_cost(input, label)

Square error cost layer

This layer accepts input predictions and target label and returns the squared error cost.

For predictions, \(X\), and target labels, \(Y\), the equation is:

\[Out = (X - Y)^2\]

In the above equation:

  • \(X\): Input predictions, a tensor.
  • \(Y\): Input labels, a tensor.
  • \(Out\): Output value, same shape with \(X\).
Parameters:
  • input (Variable) – Input tensor, has predictions.
  • label (Variable) – Label tensor, has target labels.
Returns:

The tensor variable storing the element-wise squared error difference of input and label.

Return type:

Variable

Examples

y = layers.data(name='y', shape=[1], dtype='float32')
y_predict = layers.data(name='y_predict', shape=[1], dtype='float32')
cost = layers.square_error_cost(input=y_predict, label=y)

accuracy

paddle.v2.fluid.layers.accuracy(input, label, k=1, correct=None, total=None)

This function computes the accuracy using the input and label. The output is the top_k inputs and their indices.

chunk_eval

paddle.v2.fluid.layers.chunk_eval(input, label, chunk_scheme, num_chunk_types, excluded_chunk_types=None)

This function computes and outputs the precision, recall and F1-score of chunk detection.

sequence_conv

paddle.v2.fluid.layers.sequence_conv(input, num_filters, filter_size=3, filter_stride=1, padding=None, bias_attr=None, param_attr=None, act=None)

This function creates the op for sequence_conv, using the inputs and other convolutional configurations for the filters and stride as given in the input parameters to the function.

conv2d

paddle.v2.fluid.layers.conv2d(input, num_filters, filter_size, stride=None, padding=None, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None)

Convlution2D Layer

The convolution2D layer calculates the output based on the input, filter and strides, paddings, dilations, groups parameters. Input(Input) and Output(Output) are in NCHW format. Where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. The details of convolution layer, please refer UFLDL’s convolution, . If bias attribution and activation type are provided, bias is added to the output of the convolution, and the corresponding activation function is applied to the final result.

For each input \(X\), the equation is:

\[Out = \sigma (W \ast X + b)\]

In the above equation:

  • \(X\): Input value, a tensor with NCHW format.
  • \(W\): Filter value, a tensor with MCHW format.
  • \(\ast\): Convolution operation.
  • \(b\): Bias value, a 2-D tensor with shape [M, 1].
  • \(\sigma\): Activation function.
  • \(Out\): Output value, the shape of \(Out\) and \(X\) may be
    different.

Example

  • Input:

    Input shape: $(N, C_{in}, H_{in}, W_{in})$

    Filter shape: $(C_{out}, C_{in}, H_f, W_f)$

  • Output: Output shape: $(N, C_{out}, H_{out}, W_{out})$

Where

\[\]

H_{out}&= frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \ W_{out}&= frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1

Parameters:
  • input (Variable) – The input image with [N, C, H, W] format.
  • num_filters (int) – The number of filter. It is as same as the output image channel.
  • filter_size (int|tuple|None) – The filter size. If filter_size is a tuple, it must contain two integers, (filter_size_H, filter_size_W). Otherwise, the filter will be a square.
  • stride (int|tuple) – The stride size. If stride is a tuple, it must contain two integers, (stride_H, stride_W). Otherwise, the stride_H = stride_W = stride. Default: stride = 1.
  • padding (int|tuple) – The padding size. If padding is a tuple, it must contain two integers, (padding_H, padding_W). Otherwise, the padding_H = padding_W = padding. Default: padding = 0.
  • groups (int) – The groups number of the Conv2d Layer. According to grouped convolution in Alex Krizhevsky’s Deep CNN paper: when group=2, the first half of the filters is only connected to the first half of the input channels, while the second half of the filters is only connected to the second half of the input channels. Default: groups=1
  • param_attr (ParamAttr) – The parameters to the Conv2d Layer. Default: None
  • bias_attr (ParamAttr) – Bias parameter for the Conv2d layer. Default: None
  • use_cudnn (bool) – Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: True
  • act (str) – Activation type. Default: None
Returns:

The tensor variable storing the convolution and non-linearity activation result.

Return type:

Variable

Raises:

ValueError – If the shapes of input, filter_size, stride, padding and groups mismatch.

Examples

data = fluid.layers.data(
    name='data', shape=[3, 32, 32], dtype='float32')
conv2d = fluid.layers.conv2d(
    input=data, num_filters=2, filter_size=3, act="relu")

sequence_pool

paddle.v2.fluid.layers.sequence_pool(input, pool_type)

This function add the operator for sequence pooling. It pools features of all time-steps of each instance, and is applied on top of the input using pool_type mentioned in the parameters.

It supports four pool_type:

  • average: \(Out[i] = \frac{\sum_i X_i}{N}\)
  • sum: \(Out[i] = \sum_jX_{ij}\)
  • sqrt: \(Out[i] = \frac{\sum_jX_{ij}}{\sqrt{len(X_i)}}\)
  • max: \(Out[i] = max(X_i)\)
x is a 1-level LoDTensor:
  x.lod = [[0, 2, 5, 7]]
  x.data = [1, 3, 2, 4, 6, 5, 1]
  x.dims = [7, 1]

then output is a Tensor:
  out.dim = [3, 1]
  with condition len(x.lod[-1]) - 1 == out.dims[0]

for different pool_type:
  average: out.data = [2, 4, 3], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2
  sum    : out.data = [4, 12, 6], where 4=1+3, 12=2+4+6, 6=5+1
  sqrt   : out.data = [2.82, 6.93, 4.24], where 2.82=(1+3)/sqrt(2),
             6.93=(2+4+6)/sqrt(3), 4.24=(5+1)/sqrt(2)
  max    : out.data = [3, 6, 5], where 3=max(1,3), 6=max(2,4,6), 5=max(5,1)
Parameters:
  • input (variable) – The input variable which is a LoDTensor.
  • pool_type (string) – The pooling type of sequence_pool. It supports average, sum, sqrt and max.
Returns:

The sequence pooling variable which is a Tensor.

Examples

x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
avg_x = fluid.layers.sequence_pool(input=x, pool_type='average')
sum_x = fluid.layers.sequence_pool(input=x, pool_type='sum')
sqrt_x = fluid.layers.sequence_pool(input=x, pool_type='sqrt')
max_x = fluid.layers.sequence_pool(input=x, pool_type='max')

pool2d

paddle.v2.fluid.layers.pool2d(input, pool_size, pool_type, pool_stride=None, pool_padding=None, global_pooling=False, use_cudnn=True, name=None)

This function adds the operator for pooling in 2 dimensions, using the pooling configurations mentioned in input parameters.

batch_norm

paddle.v2.fluid.layers.batch_norm(input, act=None, is_test=False, momentum=0.9, epsilon=1e-05, param_attr=None, bias_attr=None, data_layout='NCHW', name=None, moving_mean_name=None, moving_variance_name=None)

This function helps create an operator to implement the BatchNorm layer using the configurations from the input parameters.

layer_norm

paddle.v2.fluid.layers.layer_norm(input, scale=True, shift=True, begin_norm_axis=1, epsilon=1e-05, param_attr=None, bias_attr=None, act=None, name=None)

Layer Normalization

Assume feature vectors exist on dimensions begin_norm_axis ... rank(input) and calculate the moment statistics along these dimensions for each feature vector \(a\) with size \(H\), then normalize each feature vector using the corresponding statistics. After that, apply learnable gain and bias on the normalized tensor to scale and shift if scale and shift are set.

Refer to Layer Normalization

The formula is as follows:

\[ \begin{align}\begin{aligned}\mu & = \frac{1}{H}\sum_{i=1}^{H} a_i\\\sigma & = \sqrt{\frac{1}{H}\sum_{i=1}^{H}(a_i - \mu)^2}\\h & = f(\frac{g}{\sigma}(a - \mu) + b)\end{aligned}\end{align} \]
Parameters:
  • input (Variable) – The input tensor variable.
  • scale (bool) – Whether to learn the adaptive gain \(g\) after normalization.
  • shift (bool) – Whether to learn the adaptive bias \(b\) after normalization.
  • begin_norm_axis (bool) – The normalization will be performed along dimensions from begin_norm_axis to rank(input).
  • epsilon (float) – The small value added to the variance to prevent division by zero.
  • param_attr (ParamAttr|None) – The parameter attribute for the learnable gain \(g\).
  • bias_attr (ParamAttr|None) – The parameter attribute for the learnable bias \(b\).
  • act (str) – Activation to be applied to the output of layer normalizaiton.
Returns:

A tensor variable with the same shape as the input.

Return type:

Variable

Examples

data = fluid.layers.data(
  name='data', shape=[3, 32, 32], dtype='float32')
x = fluid.layers.layer_norm(input=data, begin_norm_axis=1)

beam_search_decode

paddle.v2.fluid.layers.beam_search_decode(ids, scores, name=None)

conv2d_transpose

paddle.v2.fluid.layers.conv2d_transpose(input, num_filters, output_size=None, filter_size=None, padding=None, stride=None, dilation=None, param_attr=None, use_cudnn=True, name=None)

Convlution2D transpose layer

The convolution2D transpose layer calculates the output based on the input, filter, and dilations, strides, paddings. Input(Input) and output(Output) are in NCHW format. Where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. Parameters(dilations, strides, paddings) are two elements. These two elements represent height and width, respectively. The details of convolution transpose layer, please refer to the following explanation and references therein.

For each input \(X\), the equation is:

\[Out = W \ast X\]

In the above equation:

  • \(X\): Input value, a tensor with NCHW format.
  • \(W\): Filter value, a tensor with MCHW format.
  • \(\ast\) : Convolution transpose operation.
  • \(Out\): Output value, the shape of \(Out\) and \(X\) may be
    different.

Example

  • Input:

    Input shape: $(N, C_{in}, H_{in}, W_{in})$

    Filter shape: $(C_{in}, C_{out}, H_f, W_f)$

  • Output:

    Output shape: $(N, C_{out}, H_{out}, W_{out})$

Where

\[\begin{split}H_{out} &= (H_{in} - 1) * strides[0] - 2 * paddings[0] + dilations[0] * (H_f - 1) + 1 \\ W_{out} &= (W_{in} - 1) * strides[1] - 2 * paddings[1] + dilations[1] * (W_f - 1) + 1\end{split}\]
Parameters:
  • input (Variable) – The input image with [N, C, H, W] format.
  • num_filters (int) – The number of the filter. It is as same as the output image channel.
  • output_size (int|tuple|None) – The output image size. If output size is a tuple, it must contain two integers, (image_H, image_W). This parameter only works when filter_size is None.
  • filter_size (int|tuple|None) – The filter size. If filter_size is a tuple, it must contain two integers, (filter_size_H, filter_size_W). Otherwise, the filter will be a square. None if use output size to calculate filter_size.
  • padding (int|tuple) – The padding size. If padding is a tuple, it must contain two integers, (padding_H, padding_W). Otherwise, the padding_H = padding_W = padding. Default: padding = 0.
  • stride (int|tuple) – The stride size. If stride is a tuple, it must contain two integers, (stride_H, stride_W). Otherwise, the stride_H = stride_W = stride. Default: stride = 1.
  • dilation (int|tuple) – The dilation size. If dilation is a tuple, it must contain two integers, (dilation_H, dilation_W). Otherwise, the dilation_H = dilation_W = dilation. Default: dilation = 1.
  • param_attr (ParamAttr) – The parameters to the Conv2d_transpose Layer. Default: None
  • use_cudnn (bool) – Use cudnn kernel or not, it is valid only when the cudnn library is installed. Default: True
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The tensor variable storing the convolution transpose result.

Return type:

Variable

Raises:

ValueError – If the shapes of input, filter_size, stride, padding and groups mismatch.

Examples

data = fluid.layers.data(
    name='data', shape=[3, 32, 32], dtype='float32')
conv2d_transpose = fluid.layers.conv2d_transpose(
    input=data, num_filters=2, filter_size=3)

sequence_expand

paddle.v2.fluid.layers.sequence_expand(x, y, name=None)

Sequence Expand Layer. This layer will expand the input variable x according to LoD information of y. And the following examples will explain how sequence_expand works:

* Case 1
    x is a LoDTensor:
        x.lod = [[0,       2, 3],
                 [0, 1,    3, 4]]
        x.data = [a, b, c, d]
        x.dims = [4, 1]

    y is a LoDTensor:
        y.lod = [[0,    2,    4],
                 [0, 3, 6, 7, 8]]

    with condition len(y.lod[-1]) - 1 == x.dims[0]

    then output is a 2-level LoDTensor:
        out.lod = [[0,                2,    4],
                   [0,       3,       6, 7, 8]]
        out.data = [a, a, a, b, b, b, c, d]
        out.dims = [8, 1]

* Case 2
    x is a Tensor:
        x.data = [a, b, c]
        x.dims = [3, 1]

    y is a LoDTensor:
        y.lod = [[0, 2, 3, 6]]

    with condition len(y.lod[-1]) - 1 == x.dims[0]

    then output is a 1-level LoDTensor:
        out.lod = [[0,    2, 3,      6]]
        out.data = [a, a, b, c, c, c]
        out.dims = [6, 1]
Parameters:
  • x (Variable) – The input variable which is a Tensor or LoDTensor.
  • y (Variable) – The input variable which is a LoDTensor.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The expanded variable which is a LoDTensor.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[10], dtype='float32')
y = fluid.layers.data(name='y', shape=[10, 20],
                 dtype='float32', lod_level=1)
out = layers.sequence_expand(x=x, y=y)

lstm_unit

paddle.v2.fluid.layers.lstm_unit(x_t, hidden_t_prev, cell_t_prev, forget_bias=0.0, param_attr=None, bias_attr=None, name=None)

Lstm unit layer. The equation of a lstm step is:

\[ \begin{align}\begin{aligned}i_t & = \sigma(W_{x_i}x_{t} + W_{h_i}h_{t-1} + b_i)\\f_t & = \sigma(W_{x_f}x_{t} + W_{h_f}h_{t-1} + b_f)\\c_t & = f_tc_{t-1} + i_t tanh (W_{x_c}x_t + W_{h_c}h_{t-1} + b_c)\\o_t & = \sigma(W_{x_o}x_{t} + W_{h_o}h_{t-1} + b_o)\\h_t & = o_t tanh(c_t)\end{aligned}\end{align} \]

The inputs of lstm unit include \(x_t\), \(h_{t-1}\) and \(c_{t-1}\). The 2nd dimensions of \(h_{t-1}\) and \(c_{t-1}\) should be same. The implementation separates the linear transformation and non-linear transformation apart. Here, we take \(i_t\) as an example. The linear transformation is applied by calling a fc layer and the equation is:

\[L_{i_t} = W_{x_i}x_{t} + W_{h_i}h_{t-1} + b_i\]

The non-linear transformation is applied by calling lstm_unit_op and the equation is:

\[i_t = \sigma(L_{i_t})\]

This layer has two outputs including \(h_t\) and \(o_t\).

Parameters:
  • x_t (Variable) – The input value of current step, a 2-D tensor with shape M x N, M for batch size and N for input size.
  • hidden_t_prev (Variable) – The hidden value of lstm unit, a 2-D tensor with shape M x S, M for batch size and S for size of lstm unit.
  • cell_t_prev (Variable) – The cell value of lstm unit, a 2-D tensor with shape M x S, M for batch size and S for size of lstm unit.
  • forget_bias (float) – The forget bias of lstm unit.
  • param_attr (ParamAttr) – The attributes of parameter weights, used to set initializer, name etc.
  • bias_attr (ParamAttr) – The attributes of bias weights, if not False, bias weights will be created and be set to default value.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The hidden value and cell value of lstm unit.

Return type:

tuple

Raises:

ValueError – The ranks of x_t, hidden_t_prev and cell_t_prev not be 2 or the 1st dimensions of x_t, hidden_t_prev and cell_t_prev not be the same or the 2nd dimensions of hidden_t_prev and cell_t_prev not be the same.

Examples

x_t = fluid.layers.fc(input=x_t_data, size=10)
prev_hidden = fluid.layers.fc(input=prev_hidden_data, size=30)
prev_cell = fluid.layers.fc(input=prev_cell_data, size=30)
hidden_value, cell_value = fluid.layers.lstm_unit(x_t=x_t,
                                       hidden_t_prev=prev_hidden,
                                       cell_t_prev=prev_cell)

reduce_sum

paddle.v2.fluid.layers.reduce_sum(input, dim=None, keep_dim=False, name=None)

Computes the sum of tensor elements over the given dimension.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • dim (int|None) – The dimension along which the sum is performed. If None, sum all elements of input and return a Tensor variable with a single element, otherwise must be in the range \([-rank(input), rank(input))\). If \(dim < 0\), the dimension to reduce is \(rank + dim\).
  • keep_dim (bool|False) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The reduced Tensor variable.

Return type:

Variable

Examples

# x is a Tensor variable with following elements:
#    [[0.2, 0.3, 0.5, 0.9]
#     [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the correspending output tensor.
fluid.layers.reduce_sum(x)  # [3.5]
fluid.layers.reduce_sum(x, dim=0)  # [0.3, 0.5, 1.1, 1.6]
fluid.layers.reduce_sum(x, dim=-1)  # [1.9, 1.6]
fluid.layers.reduce_sum(x, dim=1, keep_dim=True)  # [[1.9], [1.6]]

reduce_mean

paddle.v2.fluid.layers.reduce_mean(input, dim=None, keep_dim=False, name=None)

Computes the mean of tensor elements over the given dimension.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • dim (int|None) – The dimension along which the mean is computed. If None, compute the mean over all elements of input and return a Tensor variable with a single element, otherwise must be in the range \([-rank(input), rank(input))\). If \(dim < 0\), the dimension to reduce is \(rank + dim\).
  • keep_dim (bool) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The reduced Tensor variable.

Return type:

Variable

Examples

# x is a Tensor variable with following elements:
#    [[0.2, 0.3, 0.5, 0.9]
#     [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the correspending output tensor.
fluid.layers.reduce_mean(x)  # [0.4375]
fluid.layers.reduce_mean(x, dim=0)  # [0.15, 0.25, 0.55, 0.8]
fluid.layers.reduce_mean(x, dim=-1)  # [0.475, 0.4]
fluid.layers.reduce_mean(x, dim=1, keep_dim=True)  # [[0.475], [0.4]]

reduce_max

paddle.v2.fluid.layers.reduce_max(input, dim=None, keep_dim=False, name=None)

Computes the maximum of tensor elements over the given dimension.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • dim (int|None) – The dimension along which the maximum is computed. If None, compute the maximum over all elements of input and return a Tensor variable with a single element, otherwise must be in the range \([-rank(input), rank(input))\). If \(dim < 0\), the dimension to reduce is \(rank + dim\).
  • keep_dim (bool) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The reduced Tensor variable.

Return type:

Variable

Examples

# x is a Tensor variable with following elements:
#    [[0.2, 0.3, 0.5, 0.9]
#     [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the correspending output tensor.
fluid.layers.reduce_max(x)  # [0.9]
fluid.layers.reduce_max(x, dim=0)  # [0.2, 0.3, 0.6, 0.9]
fluid.layers.reduce_max(x, dim=-1)  # [0.9, 0.7]
fluid.layers.reduce_max(x, dim=1, keep_dim=True)  # [[0.9], [0.7]]

reduce_min

paddle.v2.fluid.layers.reduce_min(input, dim=None, keep_dim=False, name=None)

Computes the minimum of tensor elements over the given dimension.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • dim (int|None) – The dimension along which the minimum is computed. If None, compute the minimum over all elements of input and return a Tensor variable with a single element, otherwise must be in the range \([-rank(input), rank(input))\). If \(dim < 0\), the dimension to reduce is \(rank + dim\).
  • keep_dim (bool) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The reduced Tensor variable.

Return type:

Variable

Examples

# x is a Tensor variable with following elements:
#    [[0.2, 0.3, 0.5, 0.9]
#     [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the correspending output tensor.
fluid.layers.reduce_min(x)  # [0.1]
fluid.layers.reduce_min(x, dim=0)  # [0.1, 0.2, 0.5, 0.7]
fluid.layers.reduce_min(x, dim=-1)  # [0.2, 0.1]
fluid.layers.reduce_min(x, dim=1, keep_dim=True)  # [[0.2], [0.1]]

sequence_first_step

paddle.v2.fluid.layers.sequence_first_step(input)

This funciton get the first step of sequence.

x is a 1-level LoDTensor:
  x.lod = [[0, 2, 5, 7]]
  x.data = [1, 3, 2, 4, 6, 5, 1]
  x.dims = [7, 1]

then output is a Tensor:
  out.dim = [3, 1]
  with condition len(x.lod[-1]) - 1 == out.dims[0]
  out.data = [1, 2, 5], where 1=first(1,3), 2=first(2,4,6), 5=first(5,1)
Parameters:input (variable) – The input variable which is a LoDTensor.
Returns:The sequence’s first step variable which is a Tensor.

Examples

x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
x_first_step = fluid.layers.sequence_first_step(input=x)

sequence_last_step

paddle.v2.fluid.layers.sequence_last_step(input)

This funciton get the last step of sequence.

x is a 1-level LoDTensor:
  x.lod = [[0, 2, 5, 7]]
  x.data = [1, 3, 2, 4, 6, 5, 1]
  x.dims = [7, 1]

then output is a Tensor:
  out.dim = [3, 1]
  with condition len(x.lod[-1]) - 1 == out.dims[0]
  out.data = [3, 6, 1], where 3=last(1,3), 6=last(2,4,6), 1=last(5,1)
Parameters:input (variable) – The input variable which is a LoDTensor.
Returns:The sequence’s last step variable which is a Tensor.

Examples

x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
x_last_step = fluid.layers.sequence_last_step(input=x)

dropout

paddle.v2.fluid.layers.dropout(x, dropout_prob, is_test=False, seed=None)

Computes dropout.

Drop or keep each element of x independently. Dropout is a regularization technique for reducing overfitting by preventing neuron co-adaption during training. The dropout operator randomly set (according to the given dropout probability) the outputs of some units to zero, while others are remain unchanged.

Parameters:
  • x (variable) – The input tensor.
  • dropout_prob (float) – Probability of setting units to zero.
  • is_test (bool) – A flag indicating whether it is in test phrase or not.
  • seed (int) – A Python integer used to create random seeds. If this parameter is set to None, a random seed is used. NOTE: If an integer seed is given, always the same output units will be dropped. DO NOT use a fixed seed in training.
Returns:

A tensor variable.

Return type:

Variable

Examples

x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
droped = fluid.layers.dropout(input=x, dropout_rate=0.5)

split

paddle.v2.fluid.layers.split(input, num_or_sections, dim=-1, name=None)

Split the input tensor into multiple sub-tensors.

Parameters:
  • input (Variable) – The input variable which is a Tensor or LoDTensor.
  • num_or_sections (int|list) – If num_or_sections is an integer, then the integer indicates the number of equal sized sub-tensors that the tensor will be divided into. If num_or_sections is a list of integers, the length of list indicates the number of sub-tensors and the integers indicate the sizes of sub-tensors’ dim dimension orderly.
  • dim (int) – The dimension along which to split. If \(dim < 0\), the dimension to split along is \(rank(input) + dim\).
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The list of segmented tensor variables.

Return type:

List

Examples

# x is a Tensor variable with shape [3, 9, 5]:
x0, x1, x2 = fluid.layers.split(x, num_or_sections=3, dim=1)
x0.shape  # [3, 3, 5]
x1.shape  # [3, 3, 5]
x2.shape  # [3, 3, 5]
x0, x1, x2 = fluid.layers.split(x, num_or_sections=[2, 3, 4], dim=1)
x0.shape  # [3, 2, 5]
x1.shape  # [3, 3, 5]
x2.shape  # [3, 4, 5]

ctc_greedy_decoder

paddle.v2.fluid.layers.ctc_greedy_decoder(input, blank, name=None)

This op is used to decode sequences by greedy policy by below steps: 1. Get the indexes of max value for each row in input. a.k.a.

numpy.argmax(input, axis=0).
  1. For each sequence in result of step1, merge repeated tokens between two blanks and delete all blanks.

A simple example as below:

Given:

input.data = [[0.6, 0.1, 0.3, 0.1],
              [0.3, 0.2, 0.4, 0.1],
              [0.1, 0.5, 0.1, 0.3],
              [0.5, 0.1, 0.3, 0.1],

              [0.5, 0.1, 0.3, 0.1],
              [0.2, 0.2, 0.2, 0.4],
              [0.2, 0.2, 0.1, 0.5],
              [0.5, 0.1, 0.3, 0.1]]

input.lod = [[0, 4, 8]]

Then:

output.data = [[2],
               [1],
               [3]]

output.lod = [[0, 2, 3]]
Parameters:
  • input (Variable) – (LoDTensor<float>), the probabilities of variable-length sequences, which is a 2-D Tensor with LoD information. It’s shape is [Lp, num_classes + 1], where Lp is the sum of all input sequences’ length and num_classes is the true number of classes. (not including the blank label).
  • blank (int) – the blank label index of Connectionist Temporal Classification (CTC) loss, which is in thehalf-opened interval [0, num_classes + 1).
Returns:

CTC greedy decode result. If all the sequences in result were empty, the result LoDTensor will be [-1] with LoD [[0]] and dims [1, 1].

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[8], dtype='float32')

cost = fluid.layers.ctc_greedy_decoder(input=x, blank=0)

edit_distance

paddle.v2.fluid.layers.edit_distance(input, label, normalized=False, ignored_tokens=None, name=None)

EditDistance operator computes the edit distances between a batch of hypothesis strings and their references. Edit distance, also called Levenshtein distance, measures how dissimilar two strings are by counting the minimum number of operations to transform one string into anthor. Here the operations include insertion, deletion, and substitution.

For example, given hypothesis string A = “kitten” and reference B = “sitting”, the edit distance is 3 for A will be transformed into B at least after two substitutions and one insertion:

“kitten” -> “sitten” -> “sittin” -> “sitting”

Input(Hyps) is a LoDTensor consisting of all the hypothesis strings with the total number denoted by batch_size, and the separation is specified by the LoD information. And the batch_size reference strings are arranged in order in the same way in the LoDTensor Input(Refs).

Output(Out) contains the batch_size results and each stands for the edit distance for a pair of strings respectively. If Attr(normalized) is true, the edit distance will be divided by the length of reference string.

Parameters:
  • input (Variable) – The indices for hypothesis strings.
  • label (Variable) – The indices for reference strings.
  • normalized (bool) – Indicated whether to normalize the edit distance by the length of reference string.
  • ignored_tokens (list of int) – Tokens that should be removed before calculating edit distance.
Returns:

sequence-to-sequence edit distance in shape [batch_size, 1].

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[8], dtype='float32')
y = fluid.layers.data(name='y', shape=[7], dtype='float32')

cost = fluid.layers.edit_distance(input=x,label=y)

l2_normalize

paddle.v2.fluid.layers.l2_normalize(x, axis, epsilon=1e-12, name=None)

L2 normalize Layer

The l2 normalize layer normalizes x along dimension axis using an L2 norm. For a 1-D tensor (dim is fixed to 0), this layer computes

output = x / sqrt(max(sum(x**2), epsilon))

For x with more dimensions, this layer independently normalizes each 1-D slice along dimension axis.

Parameters:
  • x (Variable|list) – The input tensor to l2_normalize layer.
  • axis (int) – Dimension along which to normalize the input.
  • epsilon (float) – A lower bound value for x‘s l2 norm. sqrt(epsilon) will be used as the divisor if the l2 norm of x is less than sqrt(epsilon).
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The output tensor variable.

Return type:

Variable

Examples

data = fluid.layers.data(name="data",
                         shape=(3, 17, 13),
                         dtype="float32")
normed = fluid.layers.l2_normalize(x=data, axis=1)

matmul

paddle.v2.fluid.layers.matmul(x, y, transpose_x=False, transpose_y=False, name=None)

Applies matrix multiplication to two tensors.

Currently, the input tensors’ rank can be any, but when the rank of any inputs is bigger than 3, this two inputs’ rank should be equal.

The actual behavior depends on the shapes of \(x\), \(y\) and the flag values of transpose_x, transpose_y. Specifically:

  • If a transpose flag is specified, the last two dimensions of the tensor are transposed. If the tensor is rank-1 of shape \([D]\), then for \(x\) it is treated as \([1, D]\) in nontransposed form and as \([D, 1]\) in transposed form, whereas for \(y\) it is the opposite: It is treated as \([D, 1]\) in nontransposed form and as \([1, D]\) in transposed form.
  • After transpose, the two tensors are 2-D or n-D and matrix multiplication performs in the following way.
    • If both are 2-D, they are multiplied like conventional matrices.
    • If either is n-D, it is treated as a stack of matrices residing in the last two dimensions and a batched matrix multiply supporting broadcast applies on the two tensors.

Also note that if the raw tensor \(x\) or \(y\) is rank-1 and nontransposed, the prepended or appended dimension \(1\) will be removed after matrix multiplication.

Parameters:
  • x (Variable) – The input variable which is a Tensor or LoDTensor.
  • y (Variable) – The input variable which is a Tensor or LoDTensor.
  • transpose_x (bool) – Whether to transpose \(x\) before multiplication.
  • transpose_y (bool) – Whether to transpose \(y\) before multiplication.
  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
Returns:

The product Tensor variable.

Return type:

Variable

Examples

# Examples to clarify shapes of the inputs and output
# x: [B, ..., M, K], y: [B, ..., K, N]
fluid.layers.matmul(x, y)  # out: [B, ..., M, N]

# x: [B, M, K], y: [B, K, N]
fluid.layers.matmul(x, y)  # out: [B, M, N]

# x: [B, M, K], y: [K, N]
fluid.layers.matmul(x, y)  # out: [B, M, N]

# x: [M, K], y: [K, N]
fluid.layers.matmul(x, y)  # out: [M, N]

# x: [B, M, K], y: [K]
fluid.layers.matmul(x, y)  # out: [B, M]

# x: [K], y: [K]
fluid.layers.matmul(x, y)  # out: [1]

# x: [M], y: [N]
fluid.layers.matmul(x, y, True, True)  # out: [M, N]

warpctc

paddle.v2.fluid.layers.warpctc(input, label, blank=0, norm_by_times=False)

An operator integrating the open source Warp-CTC library (https://github.com/baidu-research/warp-ctc) to compute Connectionist Temporal Classification (CTC) loss. It can be aliased as softmax with CTC, since a native softmax activation is interated to the Warp-CTC library, to to normlize values for each row of the input tensor.

Parameters:
  • input (Variable) – (LodTensor, default: LoDTensor<float>), the unscaled probabilities of variable-length sequences, which is a 2-D Tensor with LoD information. It’s shape is [Lp, num_classes + 1], where Lp is the sum of all input sequences’ length and num_classes is the true number of classes. (not including the blank label).
  • label (Variable) – (LodTensor, default: LoDTensor<int>), the ground truth of variable-length sequence, which is a 2-D Tensor with LoD information. It is of the shape [Lg, 1], where Lg is th sum of all labels’ length.
  • blank – (int, default: 0), the blank label index of Connectionist Temporal Classification (CTC) loss, which is in the half-opened interval [0, num_classes + 1).
  • norm_by_times – (bool, default: false), whether to normalize
  • gradients by the number of time-step, which is also the (the) –
  • length. There is no need to normalize the gradients (sequence's) –
  • warpctc layer was follewed by a mean_op. (if) –
Returns:

The Connectionist Temporal Classification (CTC) loss, which is a 2-D Tensor of the shape [batch_size, 1].

Return type:

Variable

Examples

sequence_reshape

paddle.v2.fluid.layers.sequence_reshape(input, new_dim)

Sequence Reshape Layer

This layer will rearrange the input sequences. The new dimension is set by user. Length of each sequence is computed according to original length, original dimension and new dimension. The following example will help to illustrate the function of this layer:

x is a LoDTensor:
    x.lod  = [[0, 2, 6]]
    x.data = [[1, 2], [3, 4],
              [5, 6], [7, 8], [9, 10], [11, 12]]
    x.dims = [6, 2]

set new_dim = 4

then out is a LoDTensor:
    out.lod  = [[0, 1, 3]]
    out.data = [[1, 2, 3, 4],
                [5, 6, 7, 8], [9, 10, 11, 12]]
    out.dims = [3, 4]

Currently, only 1-level LoDTensor is supported and please make sure (original length * original dimension) can be divided by new dimension with no remainder for each sequence.

Parameters:
  • input (Variable) – (LodTensor, default: LoDTensor<float>), a 2-D LoDTensor with shape being [N, M] where M for dimension.
  • new_dim (int) – New dimension which the input LoDTensor is reshaped to.
Returns:

Reshaped LoDTensor according to new dimension.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[5, 20],
                  dtype='float32', lod_level=1)
x_reshaped = layers.sequence_reshape(input=x, new_dim=10)

transpose

paddle.v2.fluid.layers.transpose(x, perm, name=None)

transpose Layer

Permute the dimensions of input according to perm.

The i-th dimension of the returned tensor will correspond to the perm[i]-th dimension of input.

Parameters:
  • input (Variable) – (Tensor), A Tensor.
  • perm (list) – A permutation of the dimensions of input.
Returns:

A transposed Tensor.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[5, 10, 15], dtype='float32')
x_transposed = layers.transpose(x, perm=[1, 0, 2])

im2sequence

paddle.v2.fluid.layers.im2sequence(input, filter_size=1, stride=1, padding=0, name=None)

Extracts image patches from the input tensor to form a tensor of shape {input.batch_size * output_height * output_width, filter_size_H * filter_size_W * input.channels} which is similar with im2col. This op use filter / kernel to scan images and convert these images to sequences. After expanding, the number of time step are output_height * output_width for an image, in which output_height and output_width are calculated by below equation:

\[output\_size = 1 + (2 * padding + img\_size - block\_size + stride - 1) / stride\]

And the dimension of each time step is block_y * block_x * input.channels.

Parameters:
  • input (Variable) – The input should be a tensor in NCHW format.
  • filter_size (int|tuple|None) – The filter size. If filter_size is a tuple, it must contain two integers, (filter_size_H, filter_size_W). Otherwise, the filter will be a square.
  • stride (int|tuple) – The stride size. If stride is a tuple, it must contain two integers, (stride_H, stride_W). Otherwise, the stride_H = stride_W = stride. Default: stride = 1.
  • padding (int|tuple) – The padding size. If padding is a tuple, it can contain two integers like (padding_H, padding_W) which means padding_up = padding_down = padding_H and padding_left = padding_right = padding_W. Or it can use (padding_up, padding_left, padding_down, padding_right) to indicate paddings of four direction. Otherwise, a scalar padding means padding_up = padding_down = padding_left = padding_right = padding Default: padding = 0.
  • name (int) – The name of this layer. It is optional.
Returns:

The output is a LoDTensor with shape {input.batch_size * output_height * output_width, filter_size_H * filter_size_W * input.channels}. If we regard output as a matrix, each row of this matrix is a step of a sequence.

Return type:

output

Examples:

As an example:

Given:

x = [[[[ 6.  2.  1.]
       [ 8.  3.  5.]
       [ 0.  2.  6.]]

      [[ 2.  4.  4.]
       [ 6.  3.  0.]
       [ 6.  4.  7.]]]

     [[[ 6.  7.  1.]
       [ 5.  7.  9.]
       [ 2.  4.  8.]]

      [[ 1.  2.  1.]
       [ 1.  3.  5.]
       [ 9.  0.  8.]]]]

x.dims = {2, 2, 3, 3}

And:

filter = [2, 2]
stride = [1, 1]
padding = [0, 0]

Then:

output.data = [[ 6.  2.  8.  3.  2.  4.  6.  3.]
               [ 2.  1.  3.  5.  4.  4.  3.  0.]
               [ 8.  3.  0.  2.  6.  3.  6.  4.]
               [ 3.  5.  2.  6.  3.  0.  4.  7.]
               [ 6.  7.  5.  7.  1.  2.  1.  3.]
               [ 7.  1.  7.  9.  2.  1.  3.  5.]
               [ 5.  7.  2.  4.  1.  3.  9.  0.]
               [ 7.  9.  4.  8.  3.  5.  0.  8.]]

output.dims = {8, 9}

output.lod = [[0, 4, 8]]

The simple usage is:

output = fluid.layers.im2sequence(
    input=layer, stride=[1, 1], filter_size=[2, 2])

nce

paddle.v2.fluid.layers.nce(input, label, num_total_classes, sample_weight=None, param_attr=None, bias_attr=None, num_neg_samples=None)

Compute and return the noise-contrastive estimation training loss. See [Noise-contrastive estimation: A new estimation principle for unnormalized statistical models](http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf). By default this operator uses a uniform distribution for sampling.

Parameters:
  • input – (Tensor) A tensor of shape [batch_size, dim]. Duplicable: False Optional: False
  • label – (Tensor) A tensor of shape [batch_size, num_true_class]. ‘num_true_class’ is the number of target classes in each sample.The number of target classes per sample should be same. If you have a variable number of target classes, you can pad them out to a constant number by either repeating them or by padding with an otherwise unused class.) Duplicable: False Optional: False
  • weight – (Tensor) A tensor of shape [num_class, dim]. ‘num_class’ is the total number of class. Duplicable: False Optional: False
  • bias – (Tensor) A tensor of shape [num_class, 1]. ‘num_class’ is the total number of class. It is a dispensable input. Duplicable: False Optional: True
  • sample_weight – (Tensor) A tensor of shape [batch_size, 1] storing a weight for each sample. And it is a dispensable input. The default value of sample is 1. Duplicable: False Optional: True
  • num_total_classes (INT) – Total number of classes in all samples.
  • num_neg_samples (INT) – The number of negative classes. The default value is 10.
  • custom_neg_classes (INTS) – This attribute only be used in unitest. Classes in this list wiil be used as negative classes for every samples. Under normal conditions, user should avoid setting this attribute.
Returns:

(Tensor) A tensor of shape [batch_size, 1]. Cost of samples.

row_conv

paddle.v2.fluid.layers.row_conv(input, future_context_size, param_attr=None, act=None)

Row Conv Operator. This layer will apply lookahead convolution to input. The input variable should be a 2D LoDTensor with shape [T, D]. Parameters with shape [future_context_size + 1, D] will be created. The math equation of row convolution is as follows:

\[Out_{i} = \sum_{j = i} ^ {i + \tau} X_{j} \odot W_{i - j}\]

In the above equation:

  • \(Out_{i}\): The i-th row of output variable with shape [1, D].
  • \(\tau\): Future context size.
  • \(X_{j}\): The j-th row of input variable with shape [1, D].
  • \(W_{i-j}\): The (i-j)-th row of parameters with shape [1, D].

More details about row_conv please refer to the paper (http://www.cs.cmu.edu/~dyogatam/papers/wang+etal.iclrworkshop2016.pdf) and the design document (https://github.com/PaddlePaddle/Paddle/issues/2228#issuecomment-303903645).

Parameters:
  • input (Variable) – Input variable, a 2D LoDTensor with shape [T, D].
  • future_context_size (int) – Future context size. Please note, the shape of convolution kernel is [future_context_size + 1, D].
  • param_attr (ParamAttr) – Attributes of parameters, including name, initializer etc.
  • act (str) – Non-linear activation to be applied to output variable.
Returns:

The output tensor with same shape as input tensor.

Return type:

Variable

Examples

x = fluid.layers.data(name='x', shape=[16],
                dtype='float32', lod_level=1)
out = fluid.layers.row_conv(input=x, future_context_size=2)

multiplex

paddle.v2.fluid.layers.multiplex(inputs, index)

Multiplex Layer

Referring to the given index variable, this layer selects rows from the input variables to construct a multiplex variable. Assuming that there are \(m\) input variables and \(I_i\) represents the i-th input variable and \(i\) is in [0, \(m\)). All input variables are tensors with same shape [\(d_0\), \(d_1\), ..., \(d_R\)]. Please note that rank of the input tensor should be at least 2. Each input variable will be treated as a 2-D matrix with shape [\(M\), \(N\)] where \(M\) for \(d_0\) and \(N\) for \(d_1\) * \(d_2\) * ... * \(d_R\). Let \(I_i[j]\) be the j-th row of the i-th input variable. The given index variable should be a 2-D tensor with shape [\(M\), 1]. Let ID[i] be the i-th index value of the index variable. Then the output variable will be a tensor with shape [\(d_0\), \(d_1\), ..., \(d_R\)]. If we treat the output tensor as a 2-D matrix with shape [\(M\), \(N\)] and let \(O[i]\) be the i-th row of the matrix, then O[i] is equal to \(I_{ID[i]}[i]\).

Parameters:
  • inputs (list) – A list of variables to gather from. All variables have the same shape and the rank is at least 2.
  • index (Variable) – Tensor<int32>, index variable which is a 2-D tensor with shape [M, 1] where M is the batch size.
Returns:

Multiplex variable gathered from input variables.

Return type:

Variable

Examples

x1 = fluid.layers.data(name='x1', shape=[4], dtype='float32')
x2 = fluid.layers.data(name='x2', shape=[4], dtype='float32')
index = fluid.layers.data(name='index', shape=[1], dtype='int32')
out = fluid.layers.multiplex(inputs=[x1, x2], index=index)

ops

mean

paddle.v2.fluid.layers.mean(**kwargs)

Mean Operator.

Out is a scalar which is the mean of all elements in X.

Parameters:x – The input of mean op Duplicable: False Optional: False
Returns:The output of mean op

mul

paddle.v2.fluid.layers.mul(**kwargs)

Mul Operator.

This operator is used to perform matrix multiplication for input $X$ and $Y$.

The equation is:

$$Out = X * Y$$

Both the input $X$ and $Y$ can carry the LoD (Level of Details) information, or not. But the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of mul op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of mul op. Duplicable: False Optional: False
  • x_num_col_dims (INT) – (int, default 1), The mul_op can take tensors with more than two dimensions as its inputs. If the input $X$ is a tensor with more than two dimensions, $X$ will be flattened into a two-dimensional matrix first. The flattening rule is: the first num_col_dims will be flattened to form the first dimension of the final matrix (the height of the matrix), and the rest rank(X) - num_col_dims dimensions are flattened to form the second dimension of the final matrix (the width of the matrix). As a result, height of the flattened matrix is equal to the product of $X$’s first x_num_col_dims dimensions’ sizes, and width of the flattened matrix is equal to the product of $X$’s last rank(x) - num_col_dims dimensions’ size. For example, suppose $X$ is a 6-dimensional tensor with the shape [2, 3, 4, 5, 6], and x_num_col_dims = 3. Thus, the flattened matrix will have a shape [2 x 3 x 4, 5 x 6] = [24, 30].
  • y_num_col_dims (INT) – (int, default 1), The mul_op can take tensors with more than two, dimensions as its inputs. If the input $Y$ is a tensor with more than two dimensions, $Y$ will be flattened into a two-dimensional matrix first. The attribute y_num_col_dims determines how $Y$ is flattened. See comments of x_num_col_dims for more details.
Returns:

(Tensor), The output tensor of mul op.

reshape

paddle.v2.fluid.layers.reshape(**kwargs)

Reshape Operator.

Reshape Input(X) into the shape specified by Attr(shape).

An example: Given a 2-D tensor X with 2 rows and 2 columns : [[1, 2], [3, 4]]

and target shape = [1, 4], the reshape operator will transform the tensor X into a 2-D tensor: [[1, 2, 3, 4]]

One dimension in the target shape can be set -1, representing that its size is unknown. In this case, the real dimension will be infered from the original shape of Input(X) and other dimensions in the target shape.

Parameters:
  • x – The input tensor of reshape operator. Duplicable: False Optional: False
  • shape (INTS) – (vector<int>) Target shape of reshape operator.
Returns:

The output tensor of reshape operator.

scale

paddle.v2.fluid.layers.scale(**kwargs)

Scale operator

$$Out = scale*X$$

Parameters:
  • x – (Tensor) Input tensor of scale operator. Duplicable: False Optional: False
  • scale (FLOAT) – (float, default 1.0)The scaling factor of the scale operator.
Returns:

(Tensor) Output tensor of scale operator.

sigmoid_cross_entropy_with_logits

paddle.v2.fluid.layers.sigmoid_cross_entropy_with_logits(**kwargs)

SigmoidCrossEntropyWithLogits Operator.

This measures the element-wise probability error in classification tasks in which each class is independent. This can be thought of as predicting labels for a data-point, where labels are not mutually exclusive. For example, a news article can be about politics, technology or sports at the same time or none of these.

The logistic loss is given as follows:

$$loss = -Labels * log(sigma(X)) - (1 - Labels) * log(1 - sigma(X))$$

We know that $$sigma(X) = (1 / (1 + exp(-X)))$$. By substituting this we get:

$$loss = X - X * Labels + log(1 + exp(-X))$$

For stability and to prevent overflow of $$exp(-X)$$ when X < 0, we reformulate the loss as follows:

$$loss = max(X, 0) - X * Labels + log(1 + exp(-|X|))$$

Both the input X and Labels can carry the LoD (Level of Details) information. However the output only shares the LoD with input X.

Parameters:
  • x – (Tensor, default Tensor<float>), a 2-D tensor with shape N x D, where N is the batch size and D is the number of classes. This input is a tensor of logits computed by the previous operator. Logits are unscaled log probabilities given as log(p/(1-p)). Duplicable: False Optional: False
  • label – (Tensor, default Tensor<float>), a 2-D tensor of the same type and shape as X. This input is a tensor of probabalistic labels for each logit Duplicable: False Optional: False
Returns:

(Tensor, default Tensor<float>), a 2-D tensor with shape N x D of elementwise logistic losses.

elementwise_add

paddle.v2.fluid.layers.elementwise_add(**kwargs)

Limited Elementwise Add Operator.

The equation is:

$$Out = X + Y$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_div

paddle.v2.fluid.layers.elementwise_div(**kwargs)

Limited Elementwise Div Operator.

The equation is:

$$Out = X / Y$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_sub

paddle.v2.fluid.layers.elementwise_sub(**kwargs)

Limited Elementwise Sub Operator.

The equation is:

$$Out = X - Y$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_mul

paddle.v2.fluid.layers.elementwise_mul(**kwargs)

Limited Elementwise Mul Operator.

The equation is:

$$Out = X odotY$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_max

paddle.v2.fluid.layers.elementwise_max(**kwargs)

Limited Elementwise Max Operator.

The equation is:

$$Out = max(X, Y)$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_min

paddle.v2.fluid.layers.elementwise_min(**kwargs)

Limited Elementwise Max Operator.

The equation is:

$$Out = min(X, Y)$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

elementwise_pow

paddle.v2.fluid.layers.elementwise_pow(**kwargs)

Limited Elementwise Pow Operator.

The equation is:

$$Out = X ^ Y$$

$X$ is a tensor of any dimension and the dimensions of tensor $Y$ must be smaller than or equal to the dimensions of $X$.

There are two cases for this operator: 1. The shape of $Y$ is same with $X$; 2. The shape of $Y$ is a subset of $X$.

For case 2: $Y$ will be broadcasted to match the shape of $X$ and axis should be set to index of the start dimension to broadcast $Y$ onto $X$.

For example
shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5)
shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0

Either of the inputs $X$ and $Y$ or none can carry the LoD (Level of Details) information. However, the output only shares the LoD information with input $X$.

Parameters:
  • x – (Tensor), The first input tensor of elementwise op. Duplicable: False Optional: False
  • y – (Tensor), The second input tensor of elementwise op. Duplicable: False Optional: False
  • axis (INT) – (int, default -1). The start dimension index for broadcasting Y onto X.
Returns:

The output of elementwise op.

clip

paddle.v2.fluid.layers.clip(**kwargs)

Clip Operator.

The clip operator limits the value of given input within an interval. The interval is specified with arguments ‘min’ and ‘max’:

$$ Out = min(max(X, min), max) $$

Parameters:
  • x – (Tensor)The input of clip op.The number of dimensions must be between [1, 9]. Duplicable: False Optional: False
  • min (FLOAT) – (float)Minimum value, under which element is replaced by min.
  • max (FLOAT) – (float)Maximum value, above which element is replaced by max
Returns:

(Tensor)The output of clip op with shape as input(X)

clip_by_norm

paddle.v2.fluid.layers.clip_by_norm(**kwargs)

ClipByNorm Operator.

This operator limits the L2 norm of the input $X$ within $max_norm$. If the L2 norm of $X$ is less than or equal to $max_norm$, $Out$ will be the same as $X$. If the L2 norm of $X$ is greater than $max_norm$, $X$ will be linearly scaled to make the L2 norm of $Out$ equal to $max_norm$, as shown in the following formula:

$$ Out = frac{max_norm * X}{norm(X)}, $$

where $norm(X)$ represents the L2 norm of $X$.

Parameters:
  • x – (Tensor) The input of clip_by_norm op.The number of dimensions must be between [1, 9]. Duplicable: False Optional: False
  • max_norm (FLOAT) – (float) The maximum norm value.
Returns:

(Tensor) The output of clip_by_norm op with shape as input(X)

sequence_softmax

paddle.v2.fluid.layers.sequence_softmax(**kwargs)

Sequence Softmax Operator.

SequenceSoftmaxOp computes the softmax activation among all time-steps for each sequence. The dimension of each time-step should be 1. Thus, the shape of input Tensor can be either [N, 1] or [N], where N is the sum of the length of all sequences.

The algorithm works as follows:

for i-th sequence in a mini-batch:

$$ Out(X[lod[i]:lod[i+1]], :) = frac{exp(X[lod[i]:lod[i+1], :])} {sum(exp(X[lod[i]:lod[i+1], :]))} $$

For example, for a mini-batch of 3 sequences with variable-length, each containing 2, 3, 2 time-steps, the lod of which is [0, 2, 5, 7], then softmax will be computed among X[0:2, :], X[2:5, :], X[5:7, :] and N turns out to be 7.

Parameters:x – (LoDTensor) 1-D or 2-D input LoDTensor with the 2-nd dimension of length 1. Duplicable: False Optional: False
Returns:(LoDTensor) 1-D or 2-D output LoDTensor with the 2-nd dimension of length 1.

sigmoid

paddle.v2.fluid.layers.sigmoid(**kwargs)

Sigmoid Activation Operator

$$out = frac{1}{1 + e^{-x}}$$

Parameters:x – Input of Sigmoid operator Duplicable: False Optional: False
Returns:Output of Sigmoid operator

logsigmoid

paddle.v2.fluid.layers.logsigmoid(**kwargs)

Logsigmoid Activation Operator

$$out = log frac{1}{1 + e^{-x}}$$

Parameters:x – Input of LogSigmoid operator Duplicable: False Optional: False
Returns:Output of LogSigmoid operator

exp

paddle.v2.fluid.layers.exp(**kwargs)

Exp Activation Operator.

$out = e^x$

Parameters:x – Input of Exp operator Duplicable: False Optional: False
Returns:Output of Exp operator

relu

paddle.v2.fluid.layers.relu(**kwargs)

Relu Activation Operator.

$out = max(x, 0)$

Parameters:x – Input of Relu operator Duplicable: False Optional: False
Returns:Output of Relu operator

tanh

paddle.v2.fluid.layers.tanh(**kwargs)

Tanh Activation Operator.

$$out = frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$

Parameters:x – Input of Tanh operator Duplicable: False Optional: False
Returns:Output of Tanh operator

tanh_shrink

paddle.v2.fluid.layers.tanh_shrink(**kwargs)

TanhShrink Activation Operator.

$$out = x - frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$

Parameters:x – Input of TanhShrink operator Duplicable: False Optional: False
Returns:Output of TanhShrink operator

softshrink

paddle.v2.fluid.layers.softshrink(**kwargs)

Softshrink Activation Operator.

$$ out = begin{cases}

x - lambda, text{if } x > lambda \ x + lambda, text{if } x < -lambda \ 0, text{otherwise} end{cases}

$$

Parameters:
  • x – Input of Softshrink operator Duplicable: False Optional: False
  • lambda (FLOAT) – non-negative offset
Returns:

Output of Softshrink operator

sqrt

paddle.v2.fluid.layers.sqrt(**kwargs)

Sqrt Activation Operator.

$out = sqrt{x}$

Parameters:x – Input of Sqrt operator Duplicable: False Optional: False
Returns:Output of Sqrt operator

abs

paddle.v2.fluid.layers.abs(**kwargs)

Abs Activation Operator.

$out = |x|$

Parameters:x – Input of Abs operator Duplicable: False Optional: False
Returns:Output of Abs operator

ceil

paddle.v2.fluid.layers.ceil(**kwargs)

Ceil Activation Operator.

$out = ceil(x)$

Parameters:x – Input of Ceil operator Duplicable: False Optional: False
Returns:Output of Ceil operator

floor

paddle.v2.fluid.layers.floor(**kwargs)

Floor Activation Operator.

$out = floor(x)$

Parameters:x – Input of Floor operator Duplicable: False Optional: False
Returns:Output of Floor operator

round

paddle.v2.fluid.layers.round(**kwargs)

Round Activation Operator.

$out = [x]$

Parameters:x – Input of Round operator Duplicable: False Optional: False
Returns:Output of Round operator

reciprocal

paddle.v2.fluid.layers.reciprocal(**kwargs)

Reciprocal Activation Operator.

$$out = frac{1}{x}$$

Parameters:x – Input of Reciprocal operator Duplicable: False Optional: False
Returns:Output of Reciprocal operator

log

paddle.v2.fluid.layers.log(**kwargs)

Log Activation Operator.

$out = ln(x)$

Natural logarithm of x.

Parameters:x – Input of Log operator Duplicable: False Optional: False
Returns:Output of Log operator

square

paddle.v2.fluid.layers.square(**kwargs)

Square Activation Operator.

$out = x^2$

Parameters:x – Input of Square operator Duplicable: False Optional: False
Returns:Output of Square operator

softplus

paddle.v2.fluid.layers.softplus(**kwargs)

Softplus Activation Operator.

$out = ln(1 + e^{x})$

Parameters:x – Input of Softplus operator Duplicable: False Optional: False
Returns:Output of Softplus operator

softsign

paddle.v2.fluid.layers.softsign(**kwargs)

Softsign Activation Operator.

$$out = frac{x}{1 + |x|}$$

Parameters:x – Input of Softsign operator Duplicable: False Optional: False
Returns:Output of Softsign operator

brelu

paddle.v2.fluid.layers.brelu(**kwargs)

BRelu Activation Operator.

$out = max(min(x, t_{min}), t_{max})$

Parameters:
  • x – Input of BRelu operator Duplicable: False Optional: False
  • t_min (FLOAT) – The min marginal value of BRelu
  • t_max (FLOAT) – The max marginal value of BRelu
Returns:

Output of BRelu operator

leaky_relu

paddle.v2.fluid.layers.leaky_relu(**kwargs)

LeakyRelu Activation Operator.

$out = max(x, alpha * x)$

Parameters:
  • x – Input of LeakyRelu operator Duplicable: False Optional: False
  • alpha (FLOAT) – The small negative slope
Returns:

Output of LeakyRelu operator

soft_relu

paddle.v2.fluid.layers.soft_relu(**kwargs)

SoftRelu Activation Operator.

$out = ln(1 + exp(max(min(x, threshold), threshold))$

Parameters:
  • x – Input of SoftRelu operator Duplicable: False Optional: False
  • threshold (FLOAT) – The threshold value of SoftRelu
Returns:

Output of SoftRelu operator

elu

paddle.v2.fluid.layers.elu(**kwargs)

ELU Activation Operator.

Applies the following element-wise computation on the input according to https://arxiv.org/abs/1511.07289.

$out = max(0, x) + min(0, alpha * (e^x - 1))$

Parameters:
  • x – Input of ELU operator Duplicable: False Optional: False
  • alpha (FLOAT) – The alpha value of ELU
Returns:

Output of ELU operator

relu6

paddle.v2.fluid.layers.relu6(**kwargs)

Relu6 Activation Operator.

$out = min(max(0, x), 6)$

Parameters:
  • x – Input of Relu6 operator Duplicable: False Optional: False
  • threshold (FLOAT) – The threshold value of Relu6
Returns:

Output of Relu6 operator

pow

paddle.v2.fluid.layers.pow(**kwargs)

Pow Activation Operator.

$out = x^{factor}$

Parameters:
  • x – Input of Pow operator Duplicable: False Optional: False
  • factor (FLOAT) – The exponential factor of Pow
Returns:

Output of Pow operator

stanh

paddle.v2.fluid.layers.stanh(**kwargs)

STanh Activation Operator.

$$out = b * frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$

Parameters:
  • x – Input of STanh operator Duplicable: False Optional: False
  • scale_a (FLOAT) – The scale parameter of a for the input
  • scale_b (FLOAT) – The scale parameter of b for the input
Returns:

Output of STanh operator

hard_shrink

paddle.v2.fluid.layers.hard_shrink(**kwargs)

HardShrink Activation Operator.

$$ out = begin{cases}

x, text{if } x > lambda \ x, text{if } x < -lambda \ 0, text{otherwise} end{cases}

$$

Parameters:
  • x – Input of HardShrink operator Duplicable: False Optional: False
  • threshold (FLOAT) – The value of threshold for HardShrink
Returns:

Output of HardShrink operator

thresholded_relu

paddle.v2.fluid.layers.thresholded_relu(**kwargs)

ThresholdedRelu Activation Operator.

$$ out = begin{cases}

x, text{if } x > threshold \ 0, text{otherwise} end{cases}

$$

Parameters:
  • x – Input of ThresholdedRelu operator Duplicable: False Optional: False
  • threshold (FLOAT) – The threshold location of activation
Returns:

Output of ThresholdedRelu operator

hard_sigmoid

paddle.v2.fluid.layers.hard_sigmoid(**kwargs)

HardSigmoid Activation Operator.

Segment-wise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391), which is much faster than sigmoid.

$out = max(0, min(1, slope * x + shift))$

The slope should be positive. The offset can be either positive or negative. The default slope and shift are set according to the above reference. It is recommended to use the defaults for this activation.

Parameters:
  • x – Input of HardSigmoid operator Duplicable: False Optional: False
  • slope (FLOAT) – Slope for linear approximation of sigmoid
  • offset (FLOAT) – Offset for linear approximation of sigmoid
Returns:

Output of HardSigmoid operator

swish

paddle.v2.fluid.layers.swish(**kwargs)

Swish Activation Operator.

$$out = frac{x}{1 + e^{- beta x}}$$

Parameters:
  • x – Input of Swish operator Duplicable: False Optional: False
  • beta (FLOAT) – Constant beta of swish operator
Returns:

Output of Swish operator

tensor

create_tensor

paddle.v2.fluid.layers.create_tensor(dtype, name=None, persistable=False)

create_parameter

paddle.v2.fluid.layers.create_parameter(shape, dtype, name=None, attr=None, is_bias=False, default_initializer=None)

Create a parameter :param shape: shape of the parameter :type shape: list[int] :param dtype: element type of the parameter :type dtype: string :param attr: attributes of the parameter :type attr: ParamAttr :param is_bias: This can affect which default initializer is chosen

when default_initializer is None. If is_bias, initializer.Constant(0.0) will be used. Otherwise, Xavier() will be used.
Parameters:default_initializer (Initializer) – initializer for the parameter
Returns:the created parameter
Return type:Parameter

create_global_var

paddle.v2.fluid.layers.create_global_var(shape, value, dtype, persistable=False, force_cpu=False, name=None)

Create a global variable. such as global_step :param shape: shape of the variable :type shape: list[int] :param value: the value of the variable :type value: float :param dtype: element type of the parameter :type dtype: string :param persistable: if this variable is persistable :type persistable: bool :param force_cpu: force this variable to be on CPU :type force_cpu: bool

Returns:the created Variable
Return type:Variable

cast

paddle.v2.fluid.layers.cast(x, dtype)

This function takes in the input with input_dtype and casts it to the output_dtype as the output.

concat

paddle.v2.fluid.layers.concat(input, axis=0)

Concat

This function concatenates the input along the axis mentioned and returns that as the output.

Parameters:
  • input (list) – List of tensors to be concatenated
  • axis (int) – Integer axis along which the tensors will be concatenated
Returns:

Output variable of the concatenation

Return type:

Variable

Examples

sums

paddle.v2.fluid.layers.sums(input, out=None)

This function performs the sum operation on the input and returns the result as the output.

Parameters:input (Variable|list) – The input tensor that has the elements that need to be summed up.
Returns:
The tensor type variable that has the sum of input
written to it.
Return type:Variable

Examples

assign

paddle.v2.fluid.layers.assign(input, output)

Assign

This function copies the input Variable to the output Variable.

Parameters:
  • input (Variable|numpy.ndarray) – The source variable
  • output (Variable) – The destination variable
Returns:

The destination variable that was supplied as the output.

Return type:

Variable

Examples

fill_constant_batch_size_like

paddle.v2.fluid.layers.fill_constant_batch_size_like(input, shape, dtype, value, input_dim_idx=0, output_dim_idx=0)

fill_constant_batch_size_like

This function creates a tensor of specified shape, dtype and batch size, and initializes this with a constant supplied in value. The batch size is obtained from the input tensor.

It also sets stop_gradient to True.

Parameters:
  • input (Variable) – Tensor whose dimensions will be used to get batch size
  • shape (tuple|list|None) – Shape of output tensor
  • dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor
  • value (float) – Constant value to initialize the output tensor
  • input_dim_idx (int) – Index of input’s batch size dimension
  • output_dim_idx (int) – Index of output’s batch size dimension
Returns:

The tensor variable storing the output

Return type:

Variable

Examples

data = fluid.layers.fill_constant_batch_size_like(
    input=like, shape=[1], value=0, dtype='int64')

fill_constant

paddle.v2.fluid.layers.fill_constant(shape, dtype, value, force_cpu=False, out=None)

fill_constant

This function creates a tensor with specified shape and dtype, and initializes it with a constant specifed by value.

The attribute stop_gradient of the created tensor is set to True.

Parameters:
  • shape (tuple|list|None) – Shape of the output tensor.
  • dtype (np.dtype|core.VarDesc.VarType|str) – Data type of the output tensor.
  • value (float) – The constant value used to initialize the output tensor.
  • out (Variable) – The output tensor.
  • force_cpu (True|False) – data should be on CPU if set true.
Returns:

The tensor variable storing the output.

Return type:

Variable

Examples

data = fluid.layers.fill_constant(shape=[1], value=0, dtype='int64')

ones

paddle.v2.fluid.layers.ones(shape, dtype, force_cpu=False)

ones

This function creates a tensor of specified shape and dtype, and initializes this with 1.

It also sets stop_gradient to True.

Parameters:
  • shape (tuple|list|None) – Shape of output tensor
  • dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor
Returns:

The tensor variable storing the output

Return type:

Variable

Examples

data = fluid.layers.ones(shape=[1], dtype='int64')

zeros

paddle.v2.fluid.layers.zeros(shape, dtype, force_cpu=False)

zeros

This function creates a tensor of specified shape and dtype, and initializes this with 0.

It also sets stop_gradient to True.

Parameters:
  • shape (tuple|list|None) – Shape of output tensor
  • dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor
Returns:

The tensor variable storing the output

Return type:

Variable

Examples

data = fluid.layers.zeros(shape=[1], dtype='int64')