请我喝杯咖啡☕
*我的帖子解释了squeeze()。
unsqueeze() 可以从零个或多个元素的 0D 或多个 D 张量中获取附加维度为 1 的 0 个或多个元素的 1D 或多个 D 张量,如下所示:
*备忘录:
import torch my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8], [10, 11, 12]]) torch.unsqueeze(input=my_tensor, dim=0) my_tensor.unsqueeze(dim=0) torch.unsqueeze(input=my_tensor, dim=-3) # tensor([[[0, 1, 2], # [3, 4, 5], # [6, 7, 8] # [10, 11, 12]]]) torch.unsqueeze(input=my_tensor, dim=1) torch.unsqueeze(input=my_tensor, dim=-2) # tensor([[[0, 1, 2]], # [[3, 4, 5]], # [[6, 7, 8]] # [[10, 11, 12]]]) torch.unsqueeze(input=my_tensor, dim=2) torch.unsqueeze(input=my_tensor, dim=-1) # tensor([[[0], [1], [2]], # [[3], [4], [5]], # [[6], [7], [8]], # [[10], [11], [12]]]) torch.unsqueeze(input=my_tensor, dim=3) torch.unsqueeze(input=my_tensor, dim=-1) # tensor([[[[0], [1], [2], [3]], [[4], [5], [6], [7]]], # [[[8], [9], [10], [11]], [[12], [13], [14], [15]]], # [[[16], [17], [18], [19]], [[20], [21], [22], [23]]]]) my_tensor = torch.tensor([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.], [10., 11., 12.]]) torch.unsqueeze(input=my_tensor, dim=0) # tensor([[[0., 1., 2.], # [3., 4., 5.], # [6., 7., 8.], # [10., 11., 12.]]]) my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j, 5.+0.j], [6.+0.j, 7.+0.j, 8.+0.j], [10.+0.j, 11.+0.j, 12.+0.j]]) torch.unsqueeze(input=my_tensor, dim=0) # tensor([[[0.+0.j, 1.+0.j, 2.+0.j], # [3.+0.j, 4.+0.j, 5.+0.j], # [6.+0.j, 7.+0.j, 8.+0.j], # [10.+0.j, 11.+0.j, 12.+0.j]]]) my_tensor = torch.tensor([[True, False, True], [False, True, False], [True, False, True], [False, True, False]]) torch.unsqueeze(input=my_tensor, dim=0) # tensor([[[True, False, True], # [False, True, False], # [True, False, True], # [False, True, False]]])
以上是在 PyTorch 中解压的详细内容。更多信息请关注PHP中文网其他相关文章!