stack

paddle.fluid.layers.nn. stack ( x, axis=0, name=None ) [source]

This OP stacks all the inputs x along axis.

Case 1:

  Input:
    x[0].shape = [1, 2]
    x[0].data = [ [1.0 , 2.0 ] ]
    x[1].shape = [1, 2]
    x[1].data = [ [3.0 , 4.0 ] ]
    x[2].shape = [1, 2]
    x[2].data = [ [5.0 , 6.0 ] ]

  Attrs:
    axis = 0

  Output:
    Out.dims = [3, 1, 2]
    Out.data =[ [ [1.0, 2.0] ],
                [ [3.0, 4.0] ],
                [ [5.0, 6.0] ] ]


Case 2:


  Input:
    x[0].shape = [1, 2]
    x[0].data = [ [1.0 , 2.0 ] ]
    x[1].shape = [1, 2]
    x[1].data = [ [3.0 , 4.0 ] ]
    x[2].shape = [1, 2]
    x[2].data = [ [5.0 , 6.0 ] ]


  Attrs:
    axis = 1 or axis = -2

  Output:
    Out.shape = [1, 3, 2]
    Out.data =[ [ [1.0, 2.0]
                  [3.0, 4.0]
                  [5.0, 6.0] ] ]
Parameters
  • x (list(Variable)|tuple(Variable)) – Input x can be a list or tuple of Tensors, the shapes of all these Tensors must be the same. Supposing input is N dims Tensors \([d_0, d_1, ..., d_{n-1}]\), the output is N+1 dims Tensor \([d_0, d_1, d_{axis-1}, len(x), d_{axis}, ..., d_{n-1}]\). Supported data types: float32, float64, int32, int64.

  • axis (int, optional) – The axis along which all inputs are stacked. axis range is [-(R+1), R+1), where R is the number of dimensions of the first input tensor x[0]. If axis < 0, axis = axis+R+1. The default value of axis is 0.

  • name (str, optional) – Please refer to Name, Default None.

Returns

The stacked Tensor, has same data type with input Tensors. Output dim is \(rank(x[0])+1\).

Return type

Variable

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers
# set batch size=None
x1 = fluid.data(name='x1', shape=[None, 1, 2], dtype='int32')
x2 = fluid.data(name='x2', shape=[None, 1, 2], dtype='int32')
# stack Tensor list
data = layers.stack([x1,x2]) # stack according to axis 0, data.shape=[2, None, 1, 2]

data = layers.stack([x1,x2], axis=1) # stack according to axis 1, data.shape=[None, 2, 1, 2]