import torch
a=torch.rand(4,3,28,28)
结果:会是一个四维的空间
a.shape
结果:(torch.Size[4,3,28,28])
a[0].shape
结果:torch.Size([3,28,28])
a[0,0].shape
结果:torch.Size([28,28])
a[0,0,0].shape
结果:torch.Size([28])
a[0,0,3,6]
结果:tensor(0.1582)找到那个空间里的值
a.shape
a[:2].shape
结果:torch.Size([2,3,28,28])
a[2:].shape
结果torch.Size([2,3,28,28])
a[2:,:1,:,:].shape#在第一个维度上取0和1,在第二个维度上取1
结果:torch.Size([2,1,28,28])
a[2:, 1: ,: ,: ].shape#在第一个维度取2和3,第二个维度取1,2
结果:torch.Size([2,2,28,28])
#矩阵里的是数量,不是第几个
a[2:, -1: ,: ,:].shape
结果:torch.Size([2,1,28,28])
a[:, :, 0:28:2, 0:28:2].shape#隔行采用
结果:torch.Size([4,3,14,14])
a[:, :, ::2,::2].shape
结果:torch.Size([4,3,14,14])
a.index_select(0,torch.tensor([0,2])).shape#索引提取,对第一个维度选前俩
结果:torch.Size([2,3,28,28])
a.index_select(1,torch.tensor([0,2])).shape#索引提取,对第二个维度选前俩
结果:torch.Size([4,2,28,28])
a[:, 1, ...].shape#...任意多的维度
结果:torch.Size([4,28,28])
a[...,:2].shape
结果:torch.Size([4,3,28,2])
a[0,...,::2].shape#第一个是0,中间都选最后的间隔2
结果:torch.Size([3,28,14])
a.index_select(1,torch.torch.arange(2)).shape#索引提取
结果:torch.Size([4,2,28,28])
a.index_select(2,torch.torch.arange(3)).shape#索引提取
结果:torch.Size([4,3,3,28])
x = a.ge(0.5)#超过0.5的为True
x
结果:tensor([[True, True, False,False],
[False, False, False,False],
[False, False, False,False]])
a[x]
结果:tensor([1.6218, 0.9603])
a = torch.tensor([[3,7,2],[2,8,3]])
print(a)
结果:tensor([[3,7,2],
[2,8,3]])
print(torch.take(a, torch.tensor([0,1,5])))#按照编号取出数据
结果:tensor([3,7,3])