torch.cat()函数

本文介绍了PyTorch中的torch.cat()函数,用于在指定维度上连接多个张量。该函数可以看作是torch.split()和torch.chunk()的逆操作。参数包括要连接的张量序列和连接维度,并可选地提供输出张量。示例展示了在不同维度上拼接张量的效果,强调了张量形状匹配的重要性。
摘要由CSDN通过智能技术生成

Concatenates the given sequence of tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.seq

torch.cat() can be seen as an inverse operation for torch.split() and torch.chunk().

torch.cat() can be best understood via examples.

Parameters

  • tensors (sequence of Tensors) – any python sequence of tensors of the same type. Non-empty tensors provided must have the same shape, except in the cat dimension.

  • dim (intoptional) – the dimension over which the tensors are concatenated

Keyword Arguments

out (Tensoroptional) – the output tensor.

Example:

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614,  0.6580, -1.0969, -0.4614,  0.6580,
         -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497, -0.1034, -0.5790,  0.1497, -0.1034,
         -0.5790,  0.1497]])

cat函数总结:

1.若tensor向量x是[C,H,W]三维,则:

1.dim=0,代表从通道上拼接各个tensor

2.dim=1,代表从列上拼接各个tensor

3.dim=2,代表从行上拼接各个tensor

2.而对于普通的tensor向量x[H,W],则:

1.dim=0,代表从列上拼接各个tensor

2.dim=1,代表从行上拼接各个tensor。

如下所示:

tips:按照行或者列拼接的时候,必须相互对应,不能大小不同。否则会报错,如下所示:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one-over

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值