下面我们来对pytorch中tensor的截取/裁剪操作来做实验
from pprint import pprint
x = torch.randn(2, 3, 4)
pprint(x)
tensor([[[ 1.4983, 0.6645, 1.8412, -1.0799],
[-0.0073, 0.4617, -1.9119, 0.0667],
[-0.6426, 0.0552, 0.0991, -0.4575]],
[[-1.7065, 0.4007, 0.5763, 1.4644],
[-0.0856, -0.1640, -1.8661, -1.3654],
[-0.2215, -1.6505, -0.0367, -0.7769]]])
截取
y = x[:,1,:]
pprint(y)
z = x[1,::]
print(z)
m = x[:1,1,1:]
print(m)
n = x[:1,:]
print(n)
l = x[:1,1,:]
print(l)
结果
tensor([[-0.0073, 0.4617, -1.9119, 0.0667],
[-0.0856, -0.1640, -1.8661, -1.3654]])
tensor([[-1.7065, 0.4007, 0.5763, 1.4644],
[-0.0856, -0.1640, -1.8661, -1.3654],
[-0.2215, -1.6505, -0.0367, -0.7769]])
tensor([[ 0.4617, -1.9119, 0.0667]])
tensor([[[ 1.4983, 0.6645, 1.8412, -1.0799],
[-0.0073, 0.4617, -1.9119, 0.0667],
[-0.6426, 0.0552, 0.0991, -0.4575]]])
tensor([[-0.0073, 0.4617, -1.9119, 0.0667]])
即如果只想截取中间一维度的某一个数字,用:,n,:
如果想截取第一维度的某一个数字,用n,:
如果官网中有说明文档,请小伙伴们告知
说明位置