# stack¶

paddle. stack ( x, axis=0, name=None ) [源代码]

```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  # If axis = -2, axis = axis+ndim(x[0])+1 = -2+2+1 = 1.

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

## 参数¶

• x (list[Tensor]|tuple[Tensor]) – 输入 x 是多个 Tensor，且这些 Tensor 的维度和数据类型必须相同。支持的数据类型：float32、float64、int32、int64。

• axis (int，可选) – 指定对输入 Tensor 进行堆叠运算的轴，有效 axis 的范围是：[−(R+1),R+1)，R 是输入中第一个 Tensor 的维数。如果 axis < 0，则 axis=axis+R+1。默认值为 0。

• name (str，可选) - 具体用法请参见 Name，一般无需设置，默认值为 None。

## 代码示例¶

```>>> import paddle

>>> out = paddle.stack([x1, x2, x3], axis=0)
>>> print(out.shape)
[3, 1, 2]
>>> print(out)
Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 2.]],
[[3., 4.]],
[[5., 6.]]])

>>> out = paddle.stack([x1, x2, x3], axis=-2)
>>> print(out.shape)
[1, 3, 2]
>>> print(out)
Tensor(shape=[1, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 2.],
[3., 4.],
[5., 6.]]])
```