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)
相关推荐
Zucker n13 分钟前
学会python——用python制作一个登录和注册窗口(python实例十八)
开发语言·python
pictoexcel22 分钟前
名片批量转换为结构化Excel注意的事项
人工智能·深度学习·神经网络·机器学习·ocr·excel
Eiceblue27 分钟前
Python 插入、替换、提取、或删除Excel中的图片
开发语言·vscode·python·pycharm·excel
神奇夜光杯29 分钟前
Python酷库之旅-第三方库Pandas(003)
开发语言·ide·python·pandas·基础知识·学习和成长·标准库及第三方库
Leon哉36 分钟前
PyCharm中如何将某个文件设置为默认运行文件
ide·python·pycharm
三花AI40 分钟前
对标 GPT-4o 的开源实时语音多模态模型:Moshi
人工智能·gpt·语言模型·语音识别
Hugo_Hoo1 小时前
构建LangChain应用程序的示例代码:53、利用多模态大型语言模型在RAG应用中处理混合文档的示例
人工智能·langchain·ai编程
wenpingyang1 小时前
python学习2-数据结构与算法-链表
python·学习·链表
孤寒者1 小时前
(三十一)Flask之wtforms库【剖析源码下篇】
后端·python·flask·源码剖析·wtforms
milong5211 小时前
Flask自定义命令
后端·python·flask