从零开始深度学习Pytorch笔记(5)——张量的索引与变换
小黄用python
共 1631字,需浏览 4分钟
· 2020-01-19
前文传送门:从零开始深度学习Pytorch笔记(1)——安装Pytorch从零开始深度学习Pytorch笔记(2)——张量的创建(上)从零开始深度学习Pytorch笔记(3)——张量的创建(下)从零开始深度学习Pytorch笔记(4)——张量的拼接与切分
在该系列的上一篇,我们介绍了更多Pytorch中的张量的拼接与切分,本文研究张量的索引与变换。张量的索引
import torch使用torch.masked_select()索引
torch.masked_select(input, mask, out=None)其中:
input: 要索引的张量mask: 与input同形状的布尔类型张量
t = torch.randint(0,12,size=(4,3))
mask = t.ge(6) #ge是greater than or equal ,gt是greater than , le lt
t_select = torch.masked_select(t,mask)
print(t,'\n',mask,'\n',t_select)#将大于等于6的数据挑出来,返回一维张量
![b9b114bad4006dd4d451d98c5d2a334f.webp](https://filescdn.proginn.com/b8c0c4e412dc82500260ac438901d1c0/b9b114bad4006dd4d451d98c5d2a334f.webp)
使用torch.reshape()变换变换张量的形状torch.reshape(input, shape)参数:input: 要变换的张量shape: 新张量的形状
t = torch.randperm(10)
t1 = torch.reshape(t,(2,5))
print(t,'\n',t1)
![cfd7e6565756b0c2ea1f52840d153a1f.webp](https://filescdn.proginn.com/2f501c67c18b08503eaa1b194f343826/cfd7e6565756b0c2ea1f52840d153a1f.webp)
t1 = torch.reshape(t,(-1,5))# -1代表根据其他维度计算得到
print(t,'\n',t1)
![d53efdf020207fa0c51b9b9a149b208e.webp](https://filescdn.proginn.com/e55c19ac1c18563261540962f3e935d0/d53efdf020207fa0c51b9b9a149b208e.webp)
t = torch.randperm(10)
t[0] = 1024
print(t,'\n',t1)
print(id(t.data),id(t1.data))
#共享内存,id相同
![b8f386602d188892089c895a947ae834.webp](https://filescdn.proginn.com/0727de8ffefc1a9fbf94b2d3b5a4bbc0/b8f386602d188892089c895a947ae834.webp)
torch.transpose(input, dim0, dim1)参数:
input:要变换的张量dim0:要交换的维度dim1:要交换的维度
t = torch.rand((4,3,2))
t1 = torch.transpose(t,dim0=0,dim1=1)#交换他们的第0,1维度
print(t.shape,t1.shape)
![a3875c500fbbba1794c36b175bbd757d.webp](https://filescdn.proginn.com/21acef0bc27e5cd855aff74c31fba674/a3875c500fbbba1794c36b175bbd757d.webp)
torch.t(input)参数:input:要变换的张量
x = torch.randn(3,2)
print(x)
torch.t(x)
![c6ab98a3d979e5b04ea0e49872f78ffc.webp](https://filescdn.proginn.com/6516b5e329464096e182f4955cc38c37/c6ab98a3d979e5b04ea0e49872f78ffc.webp)
torch.squeeze(input, dim=None, out=None)参数:dim: 若为None,移除所有长度为1的轴;若指定维度,当且仅当该轴长度为1时,可以被移除。
t = torch.rand((1,2,1,1))
t1 = torch.squeeze(t)
t2 = torch.squeeze(t,dim=0)
t3 = torch.squeeze(t,dim=1)#指定的轴长度不为1,不能移除
print(t.shape,'\n',t1.shape,t2.shape,t3.shape)
![ac50d6f8cb0841bc4a33bd20ee18d89f.webp](https://filescdn.proginn.com/2c881f07bc301aea37a82895a8636742/ac50d6f8cb0841bc4a33bd20ee18d89f.webp)
torch.unsqueeze(input, dim, out=None)参数:dim:扩展的维度
x = torch.tensor([1, 2, 3, 4, 5])
torch.unsqueeze(x, 0)
![20f0103a741425bf054427ce92de9775.webp](https://filescdn.proginn.com/d182ed842e621624349f88f15643b57b/20f0103a741425bf054427ce92de9775.webp)
torch.unsqueeze(x, 1)
![044a13ddc956db247e614549aab2b762.webp](https://filescdn.proginn.com/482a2d43dc65cf4e3619596af3de5441/044a13ddc956db247e614549aab2b762.webp)
![29ef171bd2efd5601324b21351989805.webp](https://filescdn.proginn.com/35f11ca5b35a2804eae88f15226475ce/29ef171bd2efd5601324b21351989805.webp)
评论
Ooh I Love You Rakeem (Baggin' Ladies Instrumental)
歌手RZAOohILoveYouRakeem(Baggin'LadiesInstrumental)1605186657发布日期1991-07-01ISRCUSTB11900426时长4.30分钟流派说
Ooh I Love You Rakeem (Baggin' Ladies Instrumental)
0