stack

paddle.fluid.layers.stack(x, axis=0)[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 (Variable|list(Variable)) – Input x can be a single Tensor, a list of Tensors. If x is a list, 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}]\). Support data types: float32, float64, int32, int64.

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

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]

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