torch.squeeze
可以将张量的形状转换为尺寸不为 1 的张量。
我想在除一维之外的所有维度上挤压张量(在本例中,不是挤压 dim=0
)。
我在文档中只能看到
dim (int, optional) – if given, the input will be squeezed only in this dimension
我想要相反的:
t = torch.zeros(5, 1, 6, 1, 7, 1)
squeezed = torch.magic_squeeze(keep_dim=3)
assert squeezed == (5, 6, 1, 7)
这可以做到吗?
最佳答案
Reshape 会让您完成您想做的事情:
import torch
t = torch.zeros(5, 1, 6, 1, 7, 1)
t = t.reshape((5, 6, 1, 7))
>>> torch.Size([5, 6, 1, 7])
关于python - 如何挤压除一支 torch 之外的所有 torch ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66226505/