Skip to content

Commit ef2908b

Browse files
committed
add docs example
1 parent f4cc809 commit ef2908b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

python/paddle/tensor/manipulation.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,8 @@ def stack(x, axis=0, name=None):
19621962
tensor is [N, A, B]; if ``axis == 1``, the shape of stacked
19631963
tensor is [A, N, B], etc.
19641964
1965+
It also supports the operation with zero-size tensors which contain 0 in their shape.
1966+
See the examples below.
19651967
19661968
.. code-block:: text
19671969
@@ -2005,6 +2007,38 @@ def stack(x, axis=0, name=None):
20052007
[3.0, 4.0]
20062008
[5.0, 6.0] ] ]
20072009
2010+
2011+
Case 3:
2012+
2013+
Input:
2014+
x[0].shape = [0, 1, 2]
2015+
x[0].data = []
2016+
x[1].shape = [0, 1, 2]
2017+
x[1].data = []
2018+
2019+
Attrs:
2020+
axis = 0
2021+
2022+
Output:
2023+
Out.shape = [2, 0, 1, 2]
2024+
Out.data = []
2025+
2026+
2027+
Case 4:
2028+
2029+
Input:
2030+
x[0].shape = [0, 1, 2]
2031+
x[0].data = []
2032+
x[1].shape = [0, 1, 2]
2033+
x[1].data = []
2034+
2035+
Attrs:
2036+
axis = 1
2037+
2038+
Output:
2039+
Out.shape = [0, 2, 1, 2]
2040+
Out.data = []
2041+
20082042
Args:
20092043
x (list[Tensor]|tuple[Tensor]): Input ``x`` can be a ``list`` or ``tuple`` of tensors, the Tensors in ``x``
20102044
must be of the same shape and dtype. Supported data types: float32, float64, int32, int64.
@@ -2042,6 +2076,25 @@ def stack(x, axis=0, name=None):
20422076
[[[1., 2.],
20432077
[3., 4.],
20442078
[5., 6.]]])
2079+
2080+
>>> # zero-size tensors
2081+
>>> x1 = paddle.ones([0, 1, 2])
2082+
>>> x2 = paddle.ones([0, 1, 2])
2083+
2084+
>>> out = paddle.stack([x1, x2], axis=0)
2085+
>>> print(out.shape)
2086+
[2, 0, 1, 2]
2087+
>>> print(out)
2088+
Tensor(shape=[2, 0, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
2089+
[[],
2090+
[]])
2091+
2092+
>>> out = paddle.stack([x1, x2], axis=1)
2093+
>>> print(out.shape)
2094+
[0, 2, 1, 2]
2095+
>>> print(out)
2096+
Tensor(shape=[0, 2, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
2097+
[])
20452098
"""
20462099
axis = 0 if axis is None else axis
20472100

0 commit comments

Comments
 (0)