pytorch合并与拆分

第一:
broading cast

a = torch.randn(4,3)

b = torch.rand(4,3)

c = a + b

c = torch.randn(1,3)

d = a+ c

结果:

a: tensor([[-0.9123, -0.3065, -0.5646],
        [ 1.4327,  2.1845, -1.6093],
        [ 0.9051, -1.2144, -1.6093],
        [-0.6509,  0.7420,  1.0669]])
c tensor([[-1.8778,  1.3724, -1.6494]])
d的维度 torch.Size([4, 3])
d: tensor([[-2.7901,  1.0659, -2.2140],
        [-0.4452,  3.5569, -3.2587],
        [-0.9727,  0.1580, -3.2587],
        [-2.5288,  2.1144, -0.5825]])

第二:
拼接1

a = torch.randn(5,32,48)

b = torch.randn(4,32,48)

c = torch.cat([a,b],dim=0)#在第0维度合并,如果a和b的尺寸除了在第一维度都相同的话,合并要将dim改为dim=1

print(c.shape)#torch.Size([9, 32, 48])

print(torch.cat([a,b]).shape)#默认拼接按照0维 

拼接2

a = torch.randn(32,8)

b = torch.randn(32,8)

d = torch.stack([a,b],dim=2)

print(d.shape)#结果为:torch.Size([32, 8, 2])

第三:
拆分split

a = torch.randn(2,32,8)

b = a.split(1,dim=0)#第0个维度拆分

print(type(b))

拆分按数量区分

a = torch.randn(2,32,8)

b = a.chunk(2,dim=0)#第0个维度拆分

print(type(b))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值