pytorch之tensor的基础操作(2)

在这里插入图片描述
torch.cat()

a = torch.zeros((2,4))
b = torch.ones((2,4))
out = torch.cat((a,b), dim=1)
print(out)

运行结果如下:
tensor([[0., 0., 0., 0., 1., 1., 1., 1.],
        [0., 0., 0., 0., 1., 1., 1., 1.]])

torch.stack():在新的维度进行拼接

a = torch.linspace(1,12,12).view(4,3)
b = torch.linspace(12,24,12).view(4,3)
print(a,b)
out = torch.stack((a,b), dim=1)
print(out)
print(out.shape)
print(out[:,0,:])  #a
print(out[:,1,:])  #b

运行结果如下:
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.],
        [10., 11., 12.]])
tensor([[12.0000, 13.0909, 14.1818],
        [15.2727, 16.3636, 17.4545],
        [18.5455, 19.6364, 20.7273],
        [21.8182, 22.9091, 24.0000]])
tensor([[[ 1.0000,  2.0000,  3.0000],
         [12.0000, 13.0909, 14.1818]],

        [[ 4.0000,  5.0000,  6.0000],
         [15.2727, 16.3636, 17.4545]],

        [[ 7.0000,  8.0000,  9.0000],
         [18.5455, 19.6364, 20.7273]],

        [[10.0000, 11.0000, 12.0000],
         [21.8182, 22.9091, 24.0000]]])
torch.Size([4, 2, 3])
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.],
        [10., 11., 12.]])
tensor([[12.0000, 13.0909, 14.1818],
        [15.2727, 16.3636, 17.4545],
        [18.5455, 19.6364, 20.7273],
        [21.8182, 22.9091, 24.0000]])


在这里插入图片描述
torch.chunk():无法平均的话最后一块小于平均值

a = torch.rand(3,4)
print(a)
out = torch.chunk(a, 2, dim = 0) #零维度上平均切成两块
print(out)
运行结果如下:
tensor([[0.8622, 0.8813, 0.8506, 0.4134],
        [0.0779, 0.7562, 0.1696, 0.2435],
        [0.4689, 0.9242, 0.3887, 0.0454]])
        
(tensor([[0.8622, 0.8813, 0.8506, 0.4134],
        [0.0779, 0.7562, 0.1696, 0.2435]]), tensor([[0.4689, 0.9242, 0.3887, 0.0454]]))

torch.split():两种切分方式如下

a = torch.rand(5,4)
out = torch.split(a, 2, dim=0) # 2组切一刀
print(out)
结果如下:
(tensor([[0.5005, 0.7890, 0.9882, 0.3623],
        [0.4507, 0.7575, 0.3662, 0.1380]]), tensor([[0.5239, 0.0081, 0.1808, 0.1356],
        [0.5778, 0.3228, 0.8367, 0.5824]]), tensor([[0.2967, 0.7542, 0.2384, 0.3248]]))

out = torch.split(a, [1,2,3], dim=0)   #按照list清单来切
结果如下:
(tensor([[0.3039, 0.0142, 0.6083, 0.0831]]), 
tensor([[0.2401, 0.0204, 0.2778, 0.6313]]), 
tensor([[0.1951, 0.9309, 0.3008, 0.0065],
        [0.2678, 0.8499, 0.7622, 0.3273],
        [0.1647, 0.1284, 0.4363, 0.3052]]))

在这里插入图片描述
以下是几个易混淆的实例

out = torch.unsqueeze(a,0) #-1表示把维度增加在最后面
out = torch.unbind(a, dim=2) #去除某个维度有点类似于在这个维度上切块操作
print(torch.flip(a, dims=[1,2])) #可以同时对多个维度进行翻转
print(torch.rot90(a,2,dims=[0,1])) #传入2表示逆时针2*90°的旋转,若负数表示顺时针的90°旋转

在这里插入图片描述
在这里插入图片描述
以下是把一张图片数据转换的实例:

im_data = cv2.imread("test.jpg")
print(im_data)
cv2.imshow("1", im_data)

out = torch.from_numpy(im_data) #把numpy转换成Tensor数据

print(out)
out = torch.flip(out, dims=[0]) #翻转0维度即图片的高
data = out.numpy()  #把tensor转换成numpy
cv2.imshow("2", data)
cv2.waitKey(0)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值