如何使用 PyTorch 从 3D 张量中删除元素?

How do I remove elements from a 3D tensor with PyTorch?

我有一个形状为 torch.Size([4, 161, 325]) 的张量。如何删除沿 dim=2 的第一个元素,以便生成的张量具有 torch.Size([4, 161, 324])?

的形状

你可以使用简单的切片,

>>>a = torch.randn(4, 161, 325)
>>>b = a[:, :, 1:]
>>>b.shape
torch.Size([4, 161, 324])

进行切片

t = torch.rand(4,161,325)

t = t[..., 1:]            # or t = t[Ellipsis, 1:] Here, Ellipsis indicate rest of dims
t.shape
torch.Size([4, 161, 324])