顾名思义:unsqueeze,扩展维度,返回一个新的张量,对输入的既定位置插入维度 1
squeeze,压缩维度,将输入张量形状中的1 去除并返回 。
torch.unsqueeze(input, dim)
torch.squeeze(input, dim)
tensor (Tensor)– 输入张量dim (int)– 插入/消除 维度的索引
压缩维度仅对(0,1)索引进行示例,(-1,-2)原理类似
import torchx = torch.Tensor([[1, 2, 3, 4],[5,6,7,8]])print('#' * 50)print(x)print(x.size())print(x.dim())##########print('#' * 50)print(torch.unsqueeze(x, 0))print(torch.unsqueeze(x, 0).size())print(torch.unsqueeze(x, 0).dim())m=torch.unsqueeze(x, 0)print(m.squeeze(0))n=m.squeeze(0)print(n.size())print(n.dim())##########print('#' * 50)print(torch.unsqueeze(x, 1))print(torch.unsqueeze(x, 1).size())print(torch.unsqueeze(x, 1).dim())a=torch.unsqueeze(x, 1)print(a.squeeze(1))b=a.squeeze(1)print(b.size())print(b.dim())##########print('#' * 50)print(torch.unsqueeze(x, -1))print(torch.unsqueeze(x, -1).size())print(torch.unsqueeze(x, 1).dim())##########print('#' * 50)print(torch.unsqueeze(x, -2))print(torch.unsqueeze(x, -2).size())print(torch.unsqueeze(x, -2).dim()) 相应结果:##################################################tensor([[1., 2., 3., 4.],[5., 6., 7., 8.]])torch.Size([2, 4])2##################################################tensor([[[1., 2., 3., 4.],[5., 6., 7., 8.]]])torch.Size([1, 2, 4])3tensor([[1., 2., 3., 4.],[5., 6., 7., 8.]])torch.Size([2, 4])2##################################################tensor([[[1., 2., 3., 4.]],[[5., 6., 7., 8.]]])torch.Size([2, 1, 4])3tensor([[1., 2., 3., 4.],[5., 6., 7., 8.]])torch.Size([2, 4])2##################################################tensor([[[1.],[2.],[3.],[4.]],[[5.],[6.],[7.],[8.]]])torch.Size([2, 4, 1])3##################################################tensor([[[1., 2., 3., 4.]],[[5., 6., 7., 8.]]])torch.Size([2, 1, 4])3- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
