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)
相关推荐
pr_note44 分钟前
python|if判断语法对比
python
newxtc2 小时前
【昆明市不动产登记中心-注册安全分析报告】
人工智能·安全
techdashen2 小时前
圆桌讨论:Coding Agent or AI IDE 的现状和未来发展
ide·人工智能
CV实验室3 小时前
TIP 2025 | 哈工大&哈佛等提出 TripleMixer:攻克雨雪雾干扰的3D点云去噪网络!
人工智能·计算机视觉·3d·论文
apocelipes3 小时前
golang unique包和字符串内部化
java·python·性能优化·golang
Geoking.4 小时前
NumPy zeros() 函数详解
python·numpy
Full Stack Developme4 小时前
java.text 包详解
java·开发语言·python
余俊晖4 小时前
一套针对金融领域多模态问答的自适应多层级RAG框架-VeritasFi
人工智能·金融·rag
码农阿树4 小时前
视频解析转换耗时—OpenCV优化摸索路
人工智能·opencv·音视频
丁浩6665 小时前
Python机器学习---2.算法:逻辑回归
python·算法·机器学习