Pytorch阅读文档之torch.cat ,torch.split() and torch.chunk(),torch.stack()函数

Pytorch阅读文档之torch.cat (),torch.split() and torch.chunk()函数

torch.cat ()函数

torch.cat(tensors, dim=0, out=None) → Tensor
#在给定维度上连接给定序列张量的序列。
#所有张量必须具有相同的形状(在连接尺寸中除外)或为空。
#torch.cat()可以看作是torch.split()和torch.chunk()的逆运算。
#通过示例可以更好地了解torch.cat()
#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]])
>>> x = torch.randn(2, 3, 3)
>>> torch.cat((x, x, x), -3).size()
torch.Size([6, 3, 3])
>>> torch.cat((x, x, x), 0).size()
torch.Size([6, 3, 3])
>>> torch.cat((x, x, x), -2).size()
torch.Size([2, 9, 3])
>>> torch.cat((x, x, x), 1).size()
torch.Size([2, 9, 3])

torch.split()函数

torch.split(tensor, split_size_or_sections, dim=0)
#将张量拆分为多个块。
#如果split_size_or_sections是整数类型,则张量将被分成相等大小的块(如果可能)。
#如果沿着给定维度dim的张量大小不能被split_size整除,则最后一个块会更小。 
#如果split_size_or_sections是一个列表,则张量将根据split_size_or_sections分为len(split_size_or_sections)个块,其大小为dim。
#tensor (Tensor) – tensor to split.
#split_size_or_sections (int) or (list(int)) – 单个块的大小或每个块的大小列表
#dim (int) – 分割张量的尺寸

torch.chunk()函数

torch.chunk(input, chunks, dim=0) → List of Tensors
#将张量拆分为特定数量的块。 
#如果沿着给定维度dim的张量大小无法被块整除,则最后一个块将更小。
#input (Tensor) – the tensor to split
#chunks (int) – 要返回的块数
#dim (int) – 分割张量的尺寸

torch.stack()函数

#将张量的序列沿新维度连接起来。 所有张量都必须具有相同的大小。
#example
>>> x = torch.randn(2, 3)
>>> torch.stack((x, x, x), 0).size()
torch.Size([3, 2, 3])
>>> torch.stack((x, x, x), 1).size()
torch.Size([2, 3, 3])
>>> torch.stack((x, x, x), 2).size()
torch.Size([2, 3, 3])
>>> torch.stack((x, x, x), 3).size()
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值