hstack

paddle. hstack ( x, name=None ) [源代码]

沿水平轴堆叠输入 x 中的所有张量。所有张量必须具有相同的数据类型。

参数

  • x (list[Tensor]|tuple[Tensor]) - 输入 x 可以是张量的 list 或 tuple, x 中张量的数据类型必须相同。支持的数据类型: float16float32float64int32int64bfloat16

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

返回

Tensor,与输入数据类型相同的堆叠张量。

代码示例

>>> import paddle

>>> # hstack with 0-D tensors
>>> x1 = paddle.to_tensor(1.0)
>>> x2 = paddle.to_tensor(2.0)
>>> out = paddle.hstack((x1, x2))
>>> print(out)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[1., 2.])

>>> # hstack with 1-D tensors
>>> x1 = paddle.to_tensor([1.0, 2.0])
>>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
>>> out = paddle.hstack((x1, x2))
>>> print(out)
Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[1., 2., 3., 4., 5.])

>>> # hstack mix with 0-D & 1-D tensors
>>> x1 = paddle.to_tensor(1.0)
>>> x2 = paddle.to_tensor([3.0, 4.0, 5.0])
>>> out = paddle.hstack((x1, x2))
>>> print(out)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[1., 3., 4., 5.])

>>> # hstack with 2-D tensors
>>> x1 = paddle.to_tensor([[1.0, 2.0]])
>>> x2 = paddle.to_tensor([[3.0, 4.0, 5.0]])
>>> out = paddle.hstack((x1, x2))
>>> print(out)
Tensor(shape=[1, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 2., 3., 4., 5.]])